8. Edge Functions: Serverless Backend Logic
Your Serverless Backend, Globally Distributed
Supabase Edge Functions are serverless TypeScript/JavaScript functions powered by Deno and deployed to the global edge network (Deno Deploy). They run in under 50ms cold start time, can connect to your database, call external APIs, and run anywhere in the world close to your users — without you managing any infrastructure.
Edge Functions are the missing piece of the Supabase ecosystem. They give you a place to run custom server-side logic that can access your database securely. Use them for webhooks, authentication callbacks, image processing, AI inference, and any logic that shouldn't run on the client. Edge Functions are deployed to the edge — meaning they run in data centers close to your users. This reduces latency dramatically compared to traditional serverless functions running in a single region. The Deno runtime is secure by default — no filesystem or network access without explicit permission. This prevents accidental data leaks and security vulnerabilities. Edge Functions are an essential tool for building production applications.
Deno & The Edge Runtime
Deno is a secure runtime for JavaScript and TypeScript, created by the original author of Node.js. It's designed with modern web standards in mind. Edge Functions use Deno's HTTP server API — the same standard Request/Response API as the browser. There's no Express or other framework needed. TypeScript is supported natively — no compilation step required. Deno's security model is permission-based — you explicitly grant filesystem and network access. This prevents malicious code from accessing sensitive resources. NPM packages are imported via the npm: prefix, and the Deno team maintains compatibility with the Node.js ecosystem. Top-level await is supported natively. This makes Edge Functions simple to write and deploy. The cold start time is under 50ms, making them viable for latency-sensitive operations.
No compilation step. Write .ts files and deploy. Deno supports top-level await, standard library, and native TypeScript.
No filesystem or network access by default. You explicitly opt into permissions. No accidental data leaks.
Uses fetch, Request, Response, Headers — the same APIs as the browser. No Node.js-specific APIs.
Import npm packages via esm.sh or deno.land/x without a package.json. E.g. import Stripe from npm:stripe.
import { createClient } from 'npm:@supabase/supabase-js@2'; Deno.serve(async (req: Request) => { // Verify the user is authenticated 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 } } = await supabase.auth.getUser(); if (!user) return new Response('Unauthorized', { status: 401 }); return new Response(JSON.stringify({ message: `Hello, ${user.email}!` })); });
Deploying & Invoking Functions
The Supabase CLI simplifies Edge Function development. Create a new function with supabase functions new. Write your TypeScript code. Deploy to production with supabase functions deploy. Set secrets with supabase secrets set — these are encrypted and accessible via Deno.env.get(). Never hardcode API keys in your function code. Test locally with supabase functions serve. The serve command runs a local server that mimics the edge runtime. Invoke functions from the client with supabase.functions.invoke() or via fetch with the appropriate Authorization header. Functions are versioned and can be rolled back. The deployment process is fast and reliable. Use environment variables for configuration that differs between environments.
# Create a new function supabase functions new send-welcome-email # Deploy to production supabase functions deploy send-welcome-email # Set secrets supabase secrets set STRIPE_SECRET_KEY=sk_live_...
Real-World Patterns
Edge Functions excel at several common patterns. Webhooks — receive Stripe/GitHub/Twilio webhooks, validate signatures, write to your database. Scheduled Jobs — use pg_cron to trigger a function on a schedule via a database webhook. Email on Sign-up — database webhook on auth.users INSERT calls an Edge Function that sends a welcome email via SendGrid or Resend. AI Inference — call OpenAI/Anthropic APIs with your API keys stored as secrets. Image Processing — resize uploaded images using Sharp (via npm:sharp) before saving to storage. These patterns cover most serverless use cases. Edge Functions combine the convenience of serverless with the performance of edge computing. They're the perfect place for business logic that needs database access but shouldn't run on the client.
- Webhooks: Receive Stripe/GitHub/Twilio webhooks, validate signatures, write to your database.
- Scheduled Jobs: Use pg_cron to trigger a function on a schedule via a database webhook.
- Email on Sign-up: Database webhook on auth.users INSERT → Edge Function → SendGrid/Resend.
- AI Inference: Call OpenAI/Anthropic APIs with your API keys stored as secrets.
- Image Processing: Resize uploaded images using Sharp (via npm:sharp) before saving to storage.
Knowledge Check
Ready to test your understanding of 8. Edge Functions: Serverless Backend Logic?