Stripe webhook returns 401 from a Supabase Edge Function (Missing authorization header / Invalid JWT)
Published
The error
Stripe Dashboard > Webhooks shows every delivery to https://<project-ref>.supabase.co/functions/v1/stripe-webhook failing with 401 / response body {"code":401,"message":"Missing authorization header"} or "Invalid JWT" or "Invalid Token or Protected Header formatting" / the Edge Function logs show no console output at all / payment succeeds but the order, credits or subscription never update in the appWhy it happens
Supabase Edge Functions have a built-in JWT check (verify_jwt) that runs before your code. It is on by default, and Supabase's 401 troubleshooting guide says that when it fails your function never executes and the platform answers 401 with 'Invalid JWT' or 'Missing authorization header' itself. That is fine when your own app calls the function, because supabase.functions.invoke() sends the signed-in user's token. Stripe does not. Supabase's Function Configuration guide says it plainly: Stripe webhooks need to be publicly accessible because Stripe doesn't have your user tokens. Stripe signs each request with your whsec_ signing secret in the Stripe-Signature header instead. So when Lovable, Bolt, Cursor, Replit or v0 writes and deploys a stripe-webhook function with the default settings, every Stripe delivery is rejected at the door, your handler (and its console.log lines) never runs, and Stripe's Event deliveries list shows 401, which Stripe's docs describe as the destination URL having access restrictions. The same 401 can also come from projects using Supabase's newer API keys, because Supabase says the legacy check only understands the legacy key format. Stripe keeps retrying failed live deliveries for up to three days with exponential back off (sandbox events are retried three times over a few hours), so nothing is lost yet if you fix it quickly, but paid customers will not get what they paid for until you do. The wrong fixes AI builders often suggest are putting your anon or service_role key into the webhook URL (Stripe only sends its own Stripe-Signature header, and a secret key must never sit in a URL) or turning verify_jwt off for every function.
How to fix it
- Confirm it is the platform check. In Stripe, open Workbench > Webhooks, pick your endpoint and open a failed delivery: a 401 with a body of {"code":401,"message":"Missing authorization header"} or 'Invalid JWT' comes from Supabase, not your code. In Supabase, the function's Logs show the 401 invocation but none of your console.log output. If the body is your own message, your code returned the 401 itself, so search the function for status: 401.
- Turn off the JWT check for the webhook function only. Add this to supabase/config.toml: [functions.stripe-webhook] with verify_jwt = false (use your function's real folder name). Or redeploy with: supabase functions deploy stripe-webhook --no-verify-jwt. Or, if you never use the CLI, open the function in the Supabase Dashboard (Edge Functions > your function > Details) and switch off JWT verification. Leave it on for functions your signed-in users call.
- Make the config stick. The CLI verifies JWTs by default when it deploys a function without that setting, so if your AI builder redeploys without the config.toml block or the flag, the check can come back on and the 401s return. Keep the config.toml entry committed in the repo instead of relying on a one-off flag.
- Now verify Stripe's signature inside the function, because with the check off anyone can call the URL. Read the raw body with await req.text() (never req.json() first), take the stripe-signature header, and call await stripe.webhooks.constructEventAsync(body, signature, Deno.env.get('STRIPE_WEBHOOK_SECRET'), undefined, Stripe.createSubtleCryptoProvider()). Supabase notes the synchronous constructEvent() fails on the Deno runtime. Return 400 if verification throws and do no database work.
- Store the secrets as Edge Function secrets, not in code: supabase secrets set STRIPE_SECRET_KEY=sk_test_... STRIPE_WEBHOOK_SECRET=whsec_... The whsec_ value comes from the endpoint's page in Stripe (Reveal secret). Each endpoint has its own secret, test and live endpoints are separate, and stripe listen prints a different one.
- Return 2xx quickly. Stripe says your endpoint must return a successful status before any slow work. Verify, record the event id so retries are not processed twice, update the order with a service-role client inside the function, then return 200 { received: true }.
- Replay what was missed. In Stripe, open a failed event and click Resend (the Dashboard allows this for up to 15 days after the event), or run stripe events resend <event_id> --webhook-endpoint=<endpoint_id> (up to 30 days). Then check that every paid customer now has their order, credits or plan.
Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)
Stripe webhooks to my Supabase Edge Function <FUNCTION_NAME> all fail with 401 'Missing authorization header' or 'Invalid JWT', because Stripe does not send a Supabase JWT. Fix it like this and show me every file you change: 1) In supabase/config.toml add [functions.<FUNCTION_NAME>] verify_jwt = false for this function ONLY; keep JWT verification on for every other function. 2) Do not put any Supabase key (anon, publishable, service_role or sb_secret_) in the webhook URL, in Stripe, or in frontend code. 3) In the function, read the raw body with await req.text(), get the stripe-signature header and verify it with await stripe.webhooks.constructEventAsync(body, signature, Deno.env.get('STRIPE_WEBHOOK_SECRET'), undefined, Stripe.createSubtleCryptoProvider()); return 400 without touching the database if it throws. 4) Read STRIPE_SECRET_KEY and STRIPE_WEBHOOK_SECRET from Edge Function secrets and tell me the supabase secrets set command to run. 5) Store each processed event.id and skip duplicates, do the database update with a server-side client, and return 200 quickly. 6) Tell me the exact deploy command and how to resend the failed events from the Stripe Dashboard.
Error codes this fixes
401 Missing authorization header / Invalid JWT on a Stripe webhook(Supabase Edge Functions)
FAQ
Why does my Stripe webhook get 401 from Supabase?
Supabase Edge Functions check for a valid Supabase JWT before your code runs, and that check is on by default. Stripe does not have your users' tokens, so every delivery fails with 'Missing authorization header' or 'Invalid JWT'. Disable verify_jwt for the webhook function and verify the Stripe signature in code instead.
Is it safe to set verify_jwt = false?
For a webhook, yes, if your code verifies the Stripe-Signature header with your whsec_ secret and rejects anything that fails. Supabase's docs recommend exactly this for external webhooks. Without signature verification, anyone could call the URL and fake a payment, so never skip that step.
Can I add my anon or service_role key to the Stripe webhook instead?
No. Stripe proves who it is with its own Stripe-Signature header, not your Supabase keys, and putting a service_role or secret key in a URL leaks full database access to anyone who sees it. Turn the JWT check off for that function and use Stripe's signature.
Will I lose the payments that failed while it returned 401?
Not if you act within the retry window. Stripe retries live deliveries for up to three days, and you can resend an event from the Dashboard for up to 15 days or with the Stripe CLI for up to 30 days. After fixing, resend the failed events and check each customer's account.
I turned it off but still get 401. Why?
Either a later deploy turned the check back on (keep verify_jwt = false in supabase/config.toml), you changed a different function than the one in Stripe's endpoint URL, or the 401 now comes from your own code. Check the response body: a Supabase message means the platform check, your own message means your code.
Sources
Based on the official documentation:
- Supabase: Edge Function 401 error response
- Supabase: Function Configuration (verify_jwt for Stripe webhooks)
- Supabase: Securing Edge Functions (external webhooks)
- Supabase: Handling Stripe Webhooks with Edge Functions
- Stripe: Receive webhook events (status codes, retries, resend)
- Stripe: Verify webhook signatures
Related fixes
- Stripe webhook: No signatures found matching the expected signature
- Payment succeeds but the user isn't upgraded
- Supabase: Edge Function returned a non-2xx status code (how to find the real error and fix it)
- Stripe email: Your webhook endpoint is failing and will be disabled. How to fix and re-enable it
- Visiting the success page unlocks Pro without paying: grant access from the Stripe webhook, not the redirect
- Stripe webhook runs twice: duplicate orders, credits or double charges
- Supabase Edge Function callable by anyone (the anon key passes verify_jwt)
Get one new fix a week
Free newsletter: the newest breakages in Lovable, Bolt, Cursor, Replit and v0 apps, with paste-in fixes.
We only use your email to send the newsletter. Unsubscribe any time. See privacy.