Stripe “No signatures found matching the expected signature for payload” – raw body and the right secret

By Abheesht Tripathi · Published

StripeSignatureVerificationError: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?

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

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 else

Fix: 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

How to verify

stripe listen --forward-to localhost:3000/api/webhooks/stripe   # use the whsec_ it prints
stripe trigger checkout.session.completed

The 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

More in Edge Functions, webhooks and APIs · all guides