Stripe webhook not working in a Supabase Edge Function: 401s, signature errors and missing upgrades
By AI App Medic · Updated · Stripe · Supabase · Payments
The classic symptom: Stripe shows the payment as succeeded, but the user is still on the free plan. In Lovable and Bolt apps the webhook usually lives in a Supabase Edge Function, and it fails for one of a few predictable reasons. Start in Stripe: Developers > Webhooks > your endpoint > event deliveries shows the HTTP status your function returned for each event.
Status 401: JWT verification is blocking Stripe
Edge Functions require a valid Supabase JWT by default. Stripe cannot send one, so Supabase rejects the call before your code runs. Turn verification off for this one function and rely on Stripe's signature check instead:
supabase functions deploy stripe-webhook --no-verify-jwt
Or in supabase/config.toml:
[functions.stripe-webhook]
verify_jwt = false
Status 400: signature verification fails
Verify against the raw body and use the async verifier, because Edge Functions run on Deno with Web Crypto:
import Stripe from 'npm:stripe';
const stripe = new Stripe(Deno.env.get('STRIPE_SECRET_KEY')!);
const cryptoProvider = Stripe.createSubtleCryptoProvider();
Deno.serve(async (req) => {
const signature = req.headers.get('Stripe-Signature');
const body = await req.text(); // raw text, not req.json()
let event;
try {
event = await stripe.webhooks.constructEventAsync(
body, signature!, Deno.env.get('STRIPE_WEBHOOK_SECRET')!, undefined, cryptoProvider
);
} catch (err) {
return new Response(`Webhook signature error: ${err.message}`, { status: 400 });
}
// handle event.type here
return new Response(JSON.stringify({ received: true }), { status: 200 });
});
If verification still fails, check the secret:
- Each webhook endpoint has its own signing secret (
whsec_...), shown on the endpoint's page in Stripe. - Test mode and live mode endpoints have different secrets.
stripe listen(Stripe CLI) prints a different secret that only works for local forwarding.- Set it with
supabase secrets set STRIPE_WEBHOOK_SECRET=whsec_...and redeploy.
Status 200 but nobody gets upgraded
- Wrong events. Subscribe the endpoint to
checkout.session.completed(and thecustomer.subscription.*andinvoice.*events if you sell subscriptions). - No link to your user. When creating the Checkout Session, pass
client_reference_id: user.idormetadata. With Payment Links you can append?client_reference_id=...to the link URL. Then read it from the session in the webhook. - The database update is blocked by RLS. The webhook has no user session, so it must use the secret/service key, which is available only inside the function. Never use that key in the frontend.
- Errors are swallowed. Log the event id and the result of your database update, and return a non-2xx status on failure so Stripe retries.
A few extra checks
- Stripe can deliver the same event more than once. Store
event.idand skip duplicates. - Return 2xx quickly; do slow work after recording the event.
- Do not unlock access from the success redirect page alone. Anyone can open that URL. The webhook is the source of truth.
- If the browser cannot call a related function and the console says CORS or Failed to fetch, see Edge Function CORS errors. Webhooks themselves should not be called from the browser.
FAQ
Why does my Stripe webhook get a 401 from Supabase?
Supabase Edge Functions verify a Supabase JWT by default. Stripe does not send one, so the request is rejected before your code runs. Deploy the webhook function with JWT verification turned off and verify Stripe's signature instead.
What causes 'No signatures found matching the expected signature for payload'?
Usually the wrong webhook secret (test vs live, or the Stripe CLI secret in production), or the body was parsed and re-serialized before verification. Verify against the raw request text.
How do I know which user paid?
Pass your user id when creating the Checkout Session (client_reference_id or metadata), or match on the customer email. Then read it from the checkout.session.completed event.
Related guides
- Supabase Edge Function returns 401 "Invalid JWT": find who rejected the call and fix it
- Lovable + Supabase Edge Function CORS error: why the browser says failed to fetch
- Lovable + Supabase RLS not working: why your data is empty (and how to fix it)
- Next.js environment variables undefined on Vercel: NEXT_PUBLIC_, redeploys and "supabaseUrl is required"
- Supabase auth email not sending in a Lovable app: the SMTP setup that actually works