Supabase edge function returns 401 "Invalid JWT" after switching to the new API keys: the safe fix
You switched your app from the old eyJ… anon key to the new sb_publishable_… key (Supabase retires the old keys by the end of 2026), and now your edge functions answer every call with:
401 {"message":"Invalid JWT"}
Your function code never ran. The 401 comes from the platform, before your code.
Why it happens
Supabase's migration guide explains it: if the new key is sent on the Authorization: Bearer header, "the platform tries to parse it as a JWT and rejects the request with Invalid JWT. The platform's built-in verify_jwt check only understands the legacy JWT-based keys, so set verify_jwt = false for these functions and authorize the request in your own code."
The 401 troubleshooting page says the same: "Supabase now recommends turning off this built-in check and managing authentication directly in your function code, giving you more control over access."
The trap: the official fix only does half the job
Every fix you'll find says: set verify_jwt = false. That makes the 401 go away. It also removes the only thing that was stopping strangers from calling your function. Supabase's own words (Securing Edge Functions): turning it off "to clear a 401 during development removes the platform's check, leaving withSupabase as the only thing that verifies the caller."
If your function has no check of its own (very common in code written by Lovable, Bolt or an AI agent), it is now public. Anyone with the URL can run it: send email through your account, spend your OpenAI or Lovable AI credit, or write to your database with the service key.
So the fix is two steps, always together.
Step 1: turn off the legacy check, for that function only
In supabase/config.toml:
[functions.my-function]
verify_jwt = false
(or the toggle on the function's page in the Supabase dashboard). Don't turn it off for functions you haven't done Step 2 on.
Step 2: check the caller inside the function
Supabase's recommended way is the @supabase/server SDK. The migration guide: "Keep verify_jwt = false so the SDK does the authorization."
import { withSupabase } from 'npm:@supabase/server@1'
Deno.serve(
withSupabase({ auth: 'user' }, async (_req, ctx) => {
// ctx.supabase is scoped to the signed-in user (RLS applies)
return Response.json({ email: ctx.userClaims?.email })
})
)
Pick the mode per function:
| Who calls it | auth mode | Note |
|---|---|---|
| Your signed-in users (from the app) | 'user' | Most app functions. Rejects anyone not logged in. |
| A cron job, another function, your backend | 'secret' | Secret key on the apikey header. Never in the browser. |
| Anyone with your app (public data) | 'publishable' | The publishable key is public, so this does not stop strangers. Add a rate limit. |
| Stripe or another webhook | 'none' + verify the signature | Check stripe-signature with constructEventAsync before doing anything. |
Not on the SDK? The older pattern still works: create a client with the caller's Authorization header, call supabase.auth.getUser(), and return 401 if there's no user.
Also fix the client call
The new keys aren't JWTs, so send them on the apikey header, not Authorization: Bearer. supabase.functions.invoke() from a signed-in session sends the user's JWT for you. Supabase's guide warns that a function passing the platform check "doesn't authenticate a caller that sends only an API key, so authorize the key in your handler."
Check you didn't open anything
- Search
supabase/config.tomlforverify_jwt = false. For each function listed, open itsindex.tsand confirm one of:withSupabase({ auth: 'user' | 'secret' }),auth.getUser(), or a webhook signature check before any work. - Call it with no headers at all:
curl -i -X POST https://YOUR-PROJECT.supabase.co/functions/v1/my-function - Or run the free Ship-Ready quick scan (offline, reads files only). Check AUTH-010 flags any function with
verify_jwt = falseand no caller check:python3 skills/ship-ready-quick-scan/scripts/quickscan.py path/to/app
Catch it in the repo
The free Ship-Ready quick scan runs offline on your code and flags edge functions with verify_jwt = false and no caller check, plus leaked keys, RLS turned off and using (true) policies. The Crew (59-check scan, $39) adds more checks and a two-user test template.
Related: Supabase is retiring the eyJ keys: the safe migration · Stripe webhook not verified · is my anon key a problem?