Supabase Middleware
Write per-request logic once and run it on any runtime that has fetch.
Stage:
General Availability
Available on self-hosted:
Yes
@supabase/middleware is composable, type-safe middleware for Web Fetch handlers. You write per-request logic once, and the same middleware runs on any runtime that has fetch, or inside a framework through a short bridge you copy. Available now.
Key benefits
- Write once, run on any fetch runtime: Supabase Edge Functions, Vercel Functions, Cloudflare Workers, Deno, Bun, and Node 22+.
- Use it inside your framework: copy a short, typechecked bridge for Hono, H3, Elysia, NestJS, or TanStack Start. It adds no dependency.
- Typed shared context: each middleware adds one typed key to
ctx, and your handler sees everything upstream, typed. - Mistakes fail early: duplicate keys, and a middleware placed before the one it needs, fail to compile on TypeScript 5.4+.
- Stop or change a request anywhere: any middleware can return a
Responseand stop the chain, or change the response on the way back. - Supabase middleware included: claims, Supabase and admin clients, Postgres clients (one with RLS applied, one that bypasses RLS), and
withOAuthProtectedResourcefor your app's own MCP server, from@supabase/server. The engine also shipswithCorsandwithFeatureFlag. The Postgres clients don't run on Cloudflare Workers, becausepgneeds a TCP socket. - Open source and portable: it's built on the Web Fetch standard. If you leave Supabase, you keep the engine and what you wrote with it.
Supabase Middleware is valuable for:
- Verifying the caller and scoping a database connection to them without rewriting it in every function
- Giving your app its own MCP server that an agent calls as the signed-in user
- Keeping CORS, auth, and flag checks the same across many functions and projects
- Moving logic between Edge Functions and a framework like Hono without a rewrite
- Publishing shared per-request logic as a package for other projects