Files
supabase/apps/docs/content/guides/functions.mdx
2026-06-01 15:55:06 +02:00

336 lines
13 KiB
Plaintext

---
id: 'functions'
title: 'Edge Functions'
description: 'Globally distributed TypeScript functions.'
subtitle: 'Globally distributed TypeScript functions.'
sidebar_label: 'Overview'
tocVideo: 'za_loEtS4gs'
---
Edge Functions are server-side TypeScript functions, distributed globally at the edge—close to your users. They can be used for listening to webhooks or integrating your Supabase project with third-parties [like Stripe](https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/stripe-webhooks). Edge Functions are developed using [Deno](https://deno.com), which offers a few benefits to you as a developer:
- It is open source.
- It is portable. Supabase Edge Functions run locally, and on any other Deno-compatible platform (including self-hosted infrastructure).
- It is TypeScript first and supports WASM.
- Edge Functions are globally distributed for low-latency.
## How it works
- **Request enters an edge gateway (relay)** — the gateway routes traffic, handles auth headers/JWT validation, and applies routing/traffic rules.
- **Auth & policies are applied** — the gateway (or your function) can validate Supabase JWTs, apply rate-limits, and centralize security checks before executing code.
- **[Edge runtime](https://github.com/supabase/edge-runtime) executes your function** — the function runs on a regionally-distributed Edge Runtime node closest to the user for minimal latency.
- **Integrations & data access** — functions commonly call Supabase APIs (Auth, Postgres, Storage) or third-party APIs. For Postgres, prefer connection strategies suited for edge/serverless environments (see the `connect-to-postgres` guide).
- **Observability and logs** — invocations emit logs and metrics you can explore in the dashboard or downstream monitoring (Sentry, etc.).
- **Response returns via the gateway** — the gateway forwards the response back to the client and records request metadata.
## Quick technical notes
- **Runtime:** Supabase Edge Runtime (Deno compatible runtime with TypeScript first). Functions are simple `.ts` files that export a handler.
- **Local dev parity:** Use Supabase CLI for a local runtime similar to production for faster iteration (`supabase functions serve` command).
- **Global deployment:** Deploy your Edge Functions via Supabase Dashboard, CLI or MCP.
- **Cold starts & concurrency:** cold starts are possible — design for short-lived, idempotent operations. Heavy long-running jobs should be moved to [background workers](/docs/guides/functions/background-tasks).
- **Database connections:** treat Postgres like a remote, pooled service — use connection pools or serverless-friendly drivers.
- **Secrets:** store credentials in Supabase [project secrets](/docs/reference/cli/supabase-secrets) and access them via environment variables.
## When to use Edge Functions
- Authenticated or public HTTP endpoints that need low latency.
- Webhook receivers (Stripe, GitHub, etc.).
- On-demand image or Open Graph generation.
- Small AI inference tasks or orchestrating calls to external LLM APIs (like OpenAI)
- Sending transactional emails.
- Building messaging bots for Slack, Discord, etc.
<div className="not-prose">
<Button size="medium" asChild>
<a href="/docs/guides/functions/quickstart">Get started</a>
</Button>
</div>
## Examples
Check out the [Edge Function Examples](https://github.com/supabase/supabase/tree/master/examples/edge-functions) in our GitHub repository.
<div className="grid md:grid-cols-12 gap-4 not-prose">
<div className="col-span-4">
<Link href="/guides/functions/auth" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="With supabase-js">
Use the Supabase client inside your Edge Function.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/kysely-postgres" passHref>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Type-Safe SQL with Kysely"
>
Combining Kysely with Deno Postgres gives you a convenient developer experience for
interacting directly with your Postgres database.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/sentry-monitoring" passHref>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Monitoring with Sentry"
>
Monitor Edge Functions with the Sentry Deno SDK.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/cors" passHref>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="With CORS headers"
>
Send CORS headers for invoking from the browser.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link
href="https://github.com/supabase-community/expo-stripe-payments-with-supabase-functions"
passHref
>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="React Native with Stripe"
>
Full example for using Supabase and Stripe, with Expo.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link
href="https://github.com/supabase-community/flutter-stripe-payments-with-supabase-functions"
passHref
>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Flutter with Stripe"
>
Full example for using Supabase and Stripe, with Flutter.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link
href="https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/restful-tasks/index.ts"
passHref
>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Building a RESTful Service API"
>
Learn how to use HTTP methods and paths to build a RESTful service for managing tasks.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link
href="https://github.com/supabase/supabase/blob/master/examples/edge-functions/supabase/functions/read-storage/index.ts"
passHref
>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Working with Supabase Storage"
>
An example on reading a file from Supabase Storage.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/og-image" passHref>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Open Graph Image Generation"
>
Generate Open Graph images with Deno and Supabase Edge Functions.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link
href="https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/og-image-with-storage-cdn"
passHref
>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="OG Image Generation & Storage CDN Caching"
>
Cache generated images with Supabase Storage CDN.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link
href="https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/location"
passHref
>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Get User Location"
>
Get user location data from user's IP address.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/cloudflare-turnstile" passHref>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Cloudflare Turnstile"
>
Protecting Forms with Cloudflare Turnstile.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/connect-to-postgres" passHref>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Connect to Postgres"
>
Connecting to Postgres from Edge Functions.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/github-actions" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="GitHub Actions">
Deploying Edge Functions with GitHub Actions.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link
href="https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/oak-server"
passHref
>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Oak Server Middleware"
>
Request Routing with Oak server middleware.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/ai/examples/huggingface-image-captioning" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Hugging Face">
Access 100,000+ Machine Learning models.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/amazon-bedrock-image-generator" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Amazon Bedrock">
Amazon Bedrock Image Generator
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/ai/examples/openai" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="OpenAI">
Using OpenAI in Edge Functions.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/stripe-webhooks" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Stripe Webhooks">
Handling signed Stripe Webhooks with Edge Functions.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/send-emails" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Send emails">
Send emails in Edge Functions with Resend.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link
href="https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/streams"
passHref
>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Web Stream">
Server-Sent Events in Edge Functions.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/screenshots" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Puppeteer">
Generate screenshots with Puppeteer.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/discord-bot" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Discord Bot">
Building a Slash Command Discord Bot with Edge Functions.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/telegram-bot" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Telegram Bot">
Building a Telegram Bot with Edge Functions.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link
href="https://github.com/supabase/supabase/tree/master/examples/edge-functions/supabase/functions/file-upload-storage"
passHref
>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Upload File">
Process multipart/form-data.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/upstash-redis" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Upstash Redis">
Build an Edge Functions Counter with Upstash Redis.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/rate-limiting" passHref>
<GlassPanel icon={'/docs/img/icons/github-icon'} hasLightIcon={true} title="Rate Limiting">
Rate Limiting Edge Functions with Upstash Redis.
</GlassPanel>
</Link>
</div>
<div className="col-span-4">
<Link href="/guides/functions/examples/slack-bot-mention" passHref>
<GlassPanel
icon={'/docs/img/icons/github-icon'}
hasLightIcon={true}
title="Slack Bot Mention Edge Function"
>
Slack Bot handling Slack mentions in Edge Function
</GlassPanel>
</Link>
</div>
</div>