Fix guides

Stripe webhook to a Supabase Edge Function fails with 401 "Missing authorization header" or "Invalid JWT"

Payments go through but nothing unlocks, and Stripe shows every delivery to your Supabase Edge Function failing with 401. Why the platform rejects Stripe before your code runs, the one-function config change, and the signature check you must add in its place.

Free · no card

Paste your error. Get a free diagnosis by email.

Tell us what broke and we’ll email you a short diagnosis: the likely cause and what to try first. Usually within a day.

No passwords or secret keys, please. We only use your email to reply about this.

What you are seeing

Why it happens

  1. Supabase Edge Functions run a built-in JWT check (verify_jwt) before your code, and it is on by default. Stripe does not send a Supabase JWT, so the platform answers 401 and your handler never executes. Supabase's docs call out Stripe webhooks as the standard case for turning that check off.
  2. AI builders often generate the function and deploy it with the defaults, or a later redeploy from the editor or CLI put verify_jwt back on because supabase/config.toml never had the setting for that function.
  3. Once the check is off, the code has to verify Stripe's signature itself. That check fails if the body was parsed with req.json() first (Stripe signs the raw body), if the whsec_ secret belongs to a different endpoint or to test mode instead of live, or if the code uses the synchronous constructEvent(), which does not work in Deno. Supabase's example uses constructEventAsync() with Stripe's Web Crypto provider.

How to fix it

1. Confirm the 401 comes from the platform, not your code

curl -s -i -X POST \
  https://YOUR_PROJECT.supabase.co/functions/v1/stripe-webhook \
  -H 'Content-Type: application/json' -d '{}'

# 401 + "Missing authorization header" = the platform's JWT check (this guide)
# 400 + your own error text = your code ran; skip to the signature step
# In Stripe: Workbench > Webhooks > your endpoint > failed event > Response

2. Turn off the JWT check for the webhook function only

# supabase/config.toml  (use your function's folder name)
[functions.stripe-webhook]
verify_jwt = false

# then redeploy it
supabase functions deploy stripe-webhook --no-verify-jwt

# No CLI? Dashboard > Edge Functions > stripe-webhook > Details tab,
# toggle off JWT verification and save.

3. Verify Stripe's signature inside the function

import Stripe from 'npm:stripe'

const stripe = new Stripe(Deno.env.get('STRIPE_SECRET_KEY')!)
const cryptoProvider = Stripe.createSubtleCryptoProvider() // needed in Deno

Deno.serve(async (req) => {
  const signature = req.headers.get('Stripe-Signature') ?? ''
  const body = await req.text() // raw body, NOT req.json()
  let event
  try {
    event = await stripe.webhooks.constructEventAsync(
      body, signature, Deno.env.get('STRIPE_WEBHOOK_SIGNING_SECRET')!,
      undefined, cryptoProvider
    )
  } catch (err) {
    console.error('Stripe signature check failed:', err)
    return new Response('bad signature', { status: 400 })
  }
  // only now act on event.type, e.g. checkout.session.completed
  return Response.json({ received: true })
})

4. Use the signing secret for this exact endpoint

# Stripe makes a different whsec_ secret per endpoint, and test and live
# mode endpoints have different secrets. Copy it from the endpoint itself.
supabase secrets set STRIPE_WEBHOOK_SIGNING_SECRET=whsec_...
supabase secrets set STRIPE_SECRET_KEY=sk_live_...

# Never paste either value into the function code or the repo.

5. Keep every other function protected

Only the webhook should skip the platform check. Functions your signed-in users call (create checkout, read profile, call an AI model) should keep verify_jwt on, or check the user in code as Supabase's Securing Edge Functions guide shows. If your AI tool turned the check off for every function while chasing this error, turn it back on for the others.

6. Replay the failed events

In Stripe, open each failed event on the endpoint and click Resend, or send a test checkout.session.completed event from the Stripe CLI (its trigger command) while watching the function logs. Then check that the users who paid while it was broken now have what they paid for.

How to know it is fixed

A resent event shows 200 in Stripe's delivery log, the function's logs show the invocation and the event type, and the user's plan or order row is updated in your database. A request with no or a fake Stripe-Signature header gets 400, not 200. Sources: Supabase: Function configuration (verify_jwt for Stripe webhooks), Supabase: Edge Function 401 error response, Supabase: Stripe webhooks example, Supabase: Securing Edge Functions, and Stripe: Receive webhook events (raw body, per-endpoint secrets). Related: Stripe checkout works but the user is still on the free plan and Stripe webhook fails with a redirect.