What it means
Stripe signs every webhook: the Stripe-Signature header holds a timestamp and an
HMAC-SHA256 of timestamp.body, made with your endpoint's signing secret (whsec_…).
stripe.webhooks.constructEvent() recomputes it from the body and secret you pass. This error means
the two don't match. Only two things go into that calculation, so it is always one of them: the body you passed
is not byte-for-byte what Stripe sent, or the secret is not this endpoint's secret.
Cause 1: the body was parsed and re-serialised
If anything turned the body into an object first (express.json(), Next.js
req.json(), a body parser in a framework or proxy), then JSON.stringify produces
different whitespace, line endings or character escaping, and the signature no longer matches. Passing the parsed object itself
gives a different message: “Webhook payload must be provided as a string or a Buffer … representing the
_raw_ request body”.
Cause 2: the wrong signing secret
stripe listenprints its ownwhsec_secret, different from the Dashboard endpoint's secret.- Test mode and live mode endpoints have different secrets, and every endpoint has its own.
- The secret is the
whsec_…value, not yoursk_API key. - Extra spaces or quotes in the environment variable.
Fix: Next.js App Router
// app/api/webhooks/stripe/route.ts
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST(req: Request) {
const body = await req.text(); // raw body: never req.json() here
const signature = req.headers.get('stripe-signature') ?? '';
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(body, signature, process.env.STRIPE_WEBHOOK_SECRET!);
} catch (err) {
return new Response(`Webhook Error: ${(err as Error).message}`, { status: 400 });
}
if (event.type === 'checkout.session.completed') {
// fulfil the order; make this idempotent on event.id
}
return new Response('ok');
}Fix: Express
Give the webhook route a raw body parser, and register it before any global express.json():
app.post('/webhooks/stripe', express.raw({ type: 'application/json' }), (req, res) => {
let event;
try {
event = stripe.webhooks.constructEvent(req.body, req.headers['stripe-signature'], process.env.STRIPE_WEBHOOK_SECRET);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
res.json({ received: true });
});
app.use(express.json()); // everything elseFix: Supabase Edge Functions (Deno)
Deno uses the Web Crypto API, which is asynchronous, so use constructEventAsync with the SubtleCrypto provider:
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();
let event: Stripe.Event;
try {
event = await stripe.webhooks.constructEventAsync(
body, signature, Deno.env.get('STRIPE_WEBHOOK_SIGNING_SECRET')!, undefined, cryptoProvider,
);
} catch (err) {
return new Response((err as Error).message, { status: 400 });
}
console.log(event.type);
return new Response(JSON.stringify({ ok: true }), { status: 200 });
});supabase secrets set STRIPE_SECRET_KEY=sk_... STRIPE_WEBHOOK_SIGNING_SECRET=whsec_...
supabase functions deploy stripe-webhook --no-verify-jwt--no-verify-jwt is needed because Stripe doesn't send a Supabase JWT. The Stripe signature check is your authentication, so never skip it.
Two related messages
Timestamp outside the tolerance zone: the signed timestamp is more than 300 seconds (the default tolerance) away from your server's clock. Check the clock, and don't replay old events through your own scripts.Unable to extract timestamp and signatures from header: theStripe-Signatureheader is missing or mangled, often a proxy or a test request without it.
How to verify
stripe listen --forward-to localhost:3000/api/webhooks/stripe # use the whsec_ it prints
stripe trigger checkout.session.completedThe CLI shows [200] for the event. A request with a modified body or a wrong signature (for example a plain curl POST) gets 400. In the Dashboard, the endpoint's recent deliveries show 200.
Related guides
- Supabase Edge Function 401 “Invalid JWT” or “Missing authorization header”
- Supabase Database Webhooks and pg_net requests that never arrive – how to debug them
- Rate limiting an API route – return 429 Too Many Requests before abuse costs you
- Supabase Edge Function returns 404 NOT_FOUND on the CORS preflight
More in Edge Functions, webhooks and APIs · all guides