Deploy and manage Supabase Edge Functions
# Using npm npm install -g supabase # Using yarn yarn global add supabase # Verify installation supabase --version
# Create new edge functions directory supabase functions new my-function # Project structure my-function/ ├── .env ├── index.ts └── supabase.ts
// index.ts import { serve } from 'https://deno.land/std@0.168.0/http/server.ts' import { createClient } from 'https://esm.sh/@supabase/supabase-js@2' serve(async (req) => { try { const supabase = createClient( Deno.env.get('SUPABASE_URL') ?? '', Deno.env.get('SUPABASE_ANON_KEY') ?? '' ) // Your function logic here const { data, error } = await supabase .from('your_table') .select('*') .limit(10) return new Response( JSON.stringify({ data, error }), { headers: { 'Content-Type': 'application/json' } } ) } catch (error) { return new Response( JSON.stringify({ error: error.message }), { status: 500, headers: { 'Content-Type': 'application/json' } } ) } })
# Deploy to production supabase functions deploy my-function # Deploy with secrets supabase secrets set MY_API_KEY=secret123
import { supabase } from '@/lib/supabase'; const invokeFunction = async () => { try { const { data, error } = await supabase.functions.invoke('my-function', { body: { name: 'World' }, }); if (error) throw error; console.log('Response:', data); } catch (error) { console.error('Function error:', error); } };
try { // Your function logic } catch (error) { return new Response( JSON.stringify({ error: error.message, stack: process.env.NODE_ENV === 'development' ? error.stack : undefined }), { status: 500 } ) }
// Access environment variables const apiKey = Deno.env.get('MY_API_KEY') if (!apiKey) throw new Error('MY_API_KEY is required')
const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'POST', 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', }
supabase functions serve
curl