Supabase Edge Functions are globally distributed TypeScript functions running on Deno Deploy. Unlike centralized servers or standard AWS Lambda functions running in a single region, Edge Functions deploy to dozens of global points of presence (PoPs), executing close to the user with minimal cold-start times.
Edge Function Architecture
[ Client / Webhook ] ──(HTTPS)──> [ Edge PoP nearest to user ]
|
[ Deno V8 Isolate Sandbox ]
- TypeScript executed natively
- Web standards (fetch, Request, Response)
- Zero local disk state
|
v
[ Direct or Pooled Postgres Query ]
Authoring an Edge Function
Create an Edge Function using the Supabase CLI:
supabase functions new stripe-checkout
Implement the function handling a checkout session:
// supabase/functions/stripe-checkout/index.ts
import { serve } from 'https://deno.land/std@0.168.0/http/server.ts';
import Stripe from 'https://esm.sh/stripe@12.0.0?target=deno';
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2';
const stripe = new Stripe(Deno.env.get('STRIPE_SECRET_KEY')!, {
apiVersion: '2022-11-15',
httpClient: Stripe.createFetchHttpClient(),
});
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
};
serve(async (req: Request) => {
// Handle CORS preflight
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders });
}
try {
// Authenticate the caller using their JWT
const authHeader = req.headers.get('Authorization')!;
const supabase = createClient(
Deno.env.get('SUPABASE_URL')!,
Deno.env.get('SUPABASE_ANON_KEY')!,
{ global: { headers: { Authorization: authHeader } } }
);
const { data: { user }, error: authError } = await supabase.auth.getUser();
if (authError || !user) {
return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401, headers: corsHeaders });
}
const { priceId } = await req.json();
// Create Stripe Checkout Session
const session = await stripe.checkout.sessions.create({
customer_email: user.email,
payment_method_types: ['card'],
line_items: [{ price: priceId, quantity: 1 }],
mode: 'subscription',
success_url: `${req.headers.get('origin')}/success?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${req.headers.get('origin')}/pricing`,
});
return new Response(JSON.stringify({ url: session.url }), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 200,
});
} catch (err: any) {
return new Response(JSON.stringify({ error: err.message }), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 400,
});
}
});
Invoking Edge Functions from the Client SDK
const { data, error } = await supabase.functions.invoke('stripe-checkout', {
body: { priceId: 'price_1N23456789' }
});
if (data?.url) {
window.location.href = data.url; // Redirect to Stripe payment page
}
Exercises
- Create an Edge Function that receives a Stripe webhook, validates the webhook cryptographic signature, and updates the customer's subscription status in the database.
- Deploy an Edge Function with the Supabase CLI passing environment secrets via
supabase secrets set. - Explain the cold start characteristics of V8 Isolates in Deno compared to container-based serverless runtimes.