Your site calls a Supabase Edge Function to send an email, create a checkout, talk to an AI model or save something on the server, and the call fails with 401. The browser console shows one of these:
{"code":401,"message":"Invalid JWT"}{"code":401,"message":"Missing authorization header"}Invalid Token or Protected Header formattingFunctionsHttpError: Edge Function returned a non-2xx status code
Visitors just see a button that does nothing, a spinner that never stops or a generic "Something went wrong". Because you're usually signed in when you test, it can work for you and fail for everyone else.
Edge Function 401s are common on Lovable, Bolt, v0 and Cursor builds, because the builder writes the function and the call for you, and a later change (a new key, a re-deploy, a webhook) breaks the handshake between them. There are only two places a 401 can come from, so start by finding out which one you have.
Step 1: who sent the 401?
A 401 from an Edge Function comes from either:
- Supabase's built-in JWT check, which runs before your code. When it fails, your function never runs at all. The body is one of the platform messages above ("Invalid JWT", "Missing authorization header", "Invalid Token or Protected Header formatting").
- Your own function code, which ran and chose to return 401 (or passed on a 401 from another service it called). The body is whatever your code wrote, or empty.
How to tell:
- Open your live site in a private window, open DevTools (F12), go to Network, and trigger the action. Click the request to
/functions/v1/<function-name>and read the Response tab. - In the Supabase dashboard, open Edge Functions → your function → Logs (or Invocations). If there's no log line from your code for that request, the platform check stopped it. If you see your own
console.logoutput, your code ran and returned the 401 itself. - Supabase's own troubleshooting page also gives a SQL query for the logs that sorts recent 401s into "your code returned 401", "incompatible keys", "invalid key" and "missing auth header". If you're comfortable in the SQL editor, that's the quickest way to get an answer for many requests at once.
Read the error properly on the client too. supabase.functions.invoke doesn't throw the function's message; it returns an error (a FunctionsHttpError) whose context is the raw response. Log await error.context.text() (or .json()) to see the real message instead of "non-2xx status code".
Step 2: if the platform check sent it
By default every Edge Function has JWT verification turned on (verify_jwt = true). The platform then expects an Authorization: Bearer <token> header holding a valid JWT for your project, and rejects anything else. There are four common reasons that fails.
A. Your project uses the new API keys
Supabase is moving projects from the old anon and service_role keys (which are JWTs) to publishable (sb_publishable_...) and secret (sb_secret_...) keys, which are not JWTs. Supabase says the old keys are being deprecated by the end of 2026.
The legacy JWT check only understands the old format. So if your site was switched to a publishable key, or your builder created a new project that only has the new keys, and the client sends that key as Authorization: Bearer sb_publishable_..., the platform tries to read it as a JWT and answers "Invalid JWT". Supabase's migration guide warns about exactly this: many clients put the key on the Authorization header by default.
Fix (what Supabase recommends now): turn the built-in check off for that function and check access in your own code.
- Dashboard: Edge Functions → the function → details → turn off Verify JWT.
- CLI:
supabase functions deploy your-function --no-verify-jwt - Config file (so it stays off on every deploy):
[functions.your-function]
verify_jwt = false
Then add your own check at the top of the function (Step 4). Turning the check off without adding your own makes the function open to anyone who finds its address.
B. The key you send isn't from this project
The check is on, and the token is a JWT, but not one your project accepts. Typical causes:
- the site is still built with keys from an old or test project (a remix, a fresh Supabase connection, or a copied
.env) - you rotated the project's JWT secret or keys, and the live site still has the old ones baked in
- the environment variable is empty or misspelled on the live host, so the client sends
Bearer undefined
Compare the Supabase URL and key your live site actually uses (you can see them in the Network tab request headers) with Project Settings → API Keys in the right project. Re-deploy after fixing the environment variables; most hosts only read them at build time.
C. No Authorization header at all ("Missing authorization header")
The Supabase client libraries add the header for you. You get this message when the function is called some other way:
- a plain
fetch()oraxioscall tohttps://<project>.supabase.co/functions/v1/...with no headers - a form, link or third-party tool (Stripe, a webhook service, a cron job, Zapier) calling the URL directly
- a server route that forwards the request but drops the headers
Either use supabase.functions.invoke(...) from the site, or add the header yourself, or (for anything that isn't your own site) turn the check off and verify the caller another way (Step 3).
D. The user's session token is expired or missing
For functions meant only for signed-in people, the header should carry the user's access token, not the public key. If your code calls the function before the session has loaded (on page load, or right after a refresh), or after the session expired and wasn't refreshed, the client sends the public key or an old token. The platform rejects the old token; your own check rejects the public key.
Wait for the session before calling (for example, call after getSession() resolves or inside an onAuthStateChange handler), and don't keep a copy of the token in your own state where it can go stale; let the Supabase client attach a fresh one. If users are being signed out on reload too, that's a separate session problem (see Supabase auth broke after deploy).
Step 3: webhooks and other outside callers
Stripe, Resend, GitHub, Twilio and most other services that call your function don't send a Supabase JWT. With the check on, every delivery gets a 401 and your function never runs, so payments, emails or syncs quietly stop.
For these functions:
- Turn Verify JWT off for that function only (the config file entry above, with its own name).
- Verify the request yourself with the sender's signature, for example Stripe's signing secret. Never leave a webhook function open with no check.
The Lovable Stripe checkout guide walks through the Stripe webhook case step by step.
Step 4: add your own check after turning verify_jwt off
Supabase's "Securing Edge Functions" docs describe doing the check in code, with four modes:
| Who should be able to call it | What to check |
|---|---|
| Only signed-in users | a valid user JWT on Authorization |
| Only your servers | your secret key on the apikey header |
| Your public site, no sign-in | your publishable key on the apikey header |
| Signed webhooks | nothing from Supabase; verify the sender's signature |
The @supabase/server package wraps this (withSupabase({ auth: 'user' }) and so on) and gives you a client that respects your row-level security for that user. If you prefer to do it by hand, read the token from the Authorization header and call supabase.auth.getUser(token) (or check the claims) before doing any work, and return 401 yourself if it fails.
Two rules from Supabase's migration guide worth following:
- Send publishable and secret keys on the
apikeyheader, not asAuthorization: Bearer. - Secret keys must never be in your site's front-end code. Supabase rejects secret keys used from a browser with 401, which is a protection, not a bug: move that call to a server or an Edge Function.
Step 5: if your own code sent the 401
If the logs show your code ran, the 401 is coming from inside the function:
- Search the function for
401and find every place that returns it. - Check whether you're passing on a 401 from another service. A wrong or expired API key for an email provider, AI model or payment service often comes back as 401, and your function forwards it as is. Check the function's Secrets (Edge Functions → Secrets) for missing or old keys; secrets added in a builder's own settings don't always reach the live project.
- Add a log line before each
returnwith a reason, so the next failure explains itself:
console.error('Returning 401:', reason)
A 401 that is really something else
- CORS error in the console with a 401 next to it. The browser may report a CORS failure when the real problem is the 401 response (error responses from your code often lack the CORS headers). Fix the 401 first, then make sure every response, including errors and the
OPTIONSpreflight, returns your CORS headers. The CORS allowed-origins post covers the rest. - It only fails on the live domain. Usually different environment variables on the live host than in preview, which brings you back to Step 2B.
- Every request fails, not just functions. If your whole backend is unreachable, check whether the project was paused (see Site loads but data is gone? Your free backend may be paused).
Quick checklist
- Read the response body and the function logs: platform check or your code?
- Platform "Invalid JWT" with
sb_publishable_/sb_secret_keys: turn Verify JWT off for that function and add your own check. - "Invalid JWT" with old-style keys: make sure the live site uses this project's current keys, then re-deploy.
- "Missing authorization header": call through
supabase.functions.invokeor add the header. - Signed-in-only functions: wait for the session before calling.
- Webhooks: Verify JWT off for that function only, verify the sender's signature.
- Your own 401: log the reason, check the function's secrets.
- Test again in a private window, signed out and then signed in as a normal test user.
These failures hide behind buttons, so a quick look at the homepage won't show them. The free 30-second health check shows which pages on your live site fail to load, which links are broken and what errors appear in the console. For ongoing coverage, FlowCanary offers Daily monitoring of your signup, login and checkout (in development; founding spots $19/month, price locked for life, full refund on request any time before it launches).
