Skip to main content
All posts

How do you verify a Stripe webhook signature?

Stripe signs every webhook. Check the Stripe-Signature header against the raw body before you act, or anyone can fake a paid order. Next.js and Supabase code.

5 min readStripeWebhooksSecuritySupabase

Call stripe.webhooks.constructEvent with three things: the raw request body, the Stripe-Signature header, and your endpoint's whsec_ secret. If it throws, return 400 and do nothing else. Skip this step and anyone who finds your URL can post a fake "payment succeeded" event and get a paid order for free.

What is the Stripe-Signature header?

Stripe adds a Stripe-Signature header to every webhook it sends. The header holds a timestamp (t) and one or more signatures (v1). Each v1 value is an HMAC with SHA-256 over the timestamp, a dot, and the exact request body, keyed with your endpoint's signing secret, as the Stripe webhook docs describe.

Only Stripe and you know that secret. So a request with a valid signature came from someone who holds it. A request without one came from someone else.

Why does an unverified endpoint matter?

A webhook endpoint is a public URL. It has no login. The only thing that tells a real event from a forged one is the signature.

Stripe's best practices say that without verification, an attacker could send fake events to trigger actions like fulfilling orders, granting account access, or modifying records. That is the exact job most AI-written handlers do: flip paid to true, open the plan, send the download link.

The usual cause is simple. You asked an AI tool for a webhook handler. It wrote const event = await req.json() and a switch on event.type. The handler works in the demo, because the demo sends real events. It also works for an attacker, who sends fake ones. Nothing in the app tells you the check is missing.

Why does verification need the raw body?

The signature covers the body byte for byte. Stripe's docs say the body must be the exact string Stripe sent. If a framework reorders keys, strips whitespace or re-encodes the text before your code sees it, verification fails.

That is what await req.json() does. You parse the JSON, then stringify it again to pass it in, and the bytes are no longer the same. The fix is to read the body once as text, verify it, and only then parse it. In a Next.js App Router route that means await req.text(). In Express it means putting your webhook route before express.json(), as the same docs explain.

If you see "No signatures found matching the expected signature for payload", check three things in this order: the secret (a CLI secret from stripe listen differs from a Dashboard endpoint secret), the body, and the header.

What stops replays and duplicates?

A replay is a real, correctly signed request sent a second time by someone who captured it. The timestamp in the header is part of what is signed, so an attacker cannot change it. Stripe's libraries reject an event whose timestamp is too old. According to the Stripe webhook docs, the default tolerance is 5 minutes, and a tolerance of 0 turns the recency check off, so do not set it. Keep your server clock in sync with NTP.

A valid signature does not mean "first time". The same docs say endpoints might receive the same event more than once, and that Stripe retries failed deliveries for up to three days. Log the event IDs you have handled and skip repeats. Sometimes two separate events describe the same thing, so Stripe suggests matching on data.object ID plus event.type.

Stripe makes no promise about event order. Do not rely on created to decide what came first.

How can you check your endpoint without code?

  1. Open your project and search for constructEvent or stripe-signature. If neither appears in your webhook route, the check is probably missing.
  2. In the Stripe Dashboard, open Workbench, then Webhooks, and look at your endpoint. Note which URL it posts to and whether you use one secret for test mode and another for live mode.
  3. Ask your AI tool: "Does my Stripe webhook handler verify the signature on the raw body before it does anything else?" Then read the answer against the code.
  4. Send your own endpoint a made-up POST with a junk body and no real signature. A handler that verifies should return an error, and the Stripe examples return 400. If you get 200, an attacker gets the same answer. Only test endpoints you own.
  5. On Supabase, check that the function skips the JWT check (Stripe cannot send one) and does its own signature check instead. See the next section.

For developers: how do you fix it?

Next.js App Router

ts
// app/api/stripe/webhook/route.ts
import Stripe from "stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function POST(req: Request) {
  const signature = req.headers.get("stripe-signature");
  const secret = process.env.STRIPE_WEBHOOK_SECRET;
  if (!signature || !secret) {
    return new Response("Missing signature or secret", { status: 400 });
  }

  const body = await req.text(); // raw text, not req.json()

  let event: Stripe.Event;
  try {
    event = stripe.webhooks.constructEvent(body, signature, secret);
  } catch (err) {
    console.error("Stripe signature check failed", err);
    return new Response("Invalid signature", { status: 400 });
  }

  if (await alreadyHandled(event.id)) {
    return new Response("ok", { status: 200 });
  }

  switch (event.type) {
    case "checkout.session.completed":
      // mark the order paid here
      break;
    default:
      break;
  }

  return new Response("ok", { status: 200 });
}

The stripe-node Next.js example takes the same approach.

Supabase Edge Function

Deno has no Node crypto, so use the async call with a SubtleCrypto provider. The Deno Stripe example says to verify with constructEventAsync, which uses Web Crypto instead of Node APIs.

ts
// supabase/functions/stripe-webhook/index.ts
import Stripe from "npm:stripe";

const stripe = new Stripe(Deno.env.get("STRIPE_API_KEY")!, {
  httpClient: Stripe.createFetchHttpClient(),
});
const cryptoProvider = Stripe.createSubtleCryptoProvider();

Deno.serve(async (req) => {
  const signature = req.headers.get("Stripe-Signature");
  if (!signature) return new Response("Missing signature", { status: 400 });

  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, // tolerance: keep the default
      cryptoProvider,
    );
  } catch (err) {
    return new Response("Invalid signature", { status: 400 });
  }

  // skip event.id values you already handled, then act on event.type
  return new Response(JSON.stringify({ ok: true }), { status: 200 });
});

Stripe cannot send a Supabase JWT. The Supabase auth guide says to keep verify_jwt = false for external webhooks and verify the provider's signature in your handler. That page also warns that with the platform check off, your handler alone authenticates the caller.

toml
# supabase/config.toml
[functions.stripe-webhook]
verify_jwt = false

Skip duplicate events

Store each event ID in a table with a unique key and treat a failed insert as "already handled".

sql
create table processed_stripe_events (
  event_id text primary key,
  received_at timestamptz not null default now()
);

Insert the ID first. If the insert fails on the key, return 200 and stop. That also stops the retries.

Sources

  1. Stripe, "Receive Stripe events in your webhook endpoint": https://docs.stripe.com/webhooks (read 2026-11-28)
  2. Stripe, "Manage webhook endpoints": https://docs.stripe.com/events/manage-webhook-endpoints (read 2026-11-28)
  3. stripe-node, Next.js App Router webhook example: https://github.com/stripe/stripe-node/blob/master/examples/webhook-signing/nextjs/app/api/webhooks/route.ts (read 2026-11-28)
  4. Deno, "Stripe Checkout" example: https://docs.deno.com/examples/stripe_checkout/ (read 2026-11-28)
  5. Supabase, "Handling Stripe Webhooks": https://supabase.com/docs/guides/functions/examples/stripe-webhooks (read 2026-11-28)
  6. Supabase, "Securing Edge Functions": https://supabase.com/docs/guides/functions/auth (read 2026-11-28)

Questions and answers

Do I need to verify the signature if my endpoint URL is secret?
Yes. A URL is not a secret you can rely on. It shows up in logs, in the Stripe Dashboard, in browser tools and in repos. Stripe's docs say to use signature verification and, on top of that, to allow only Stripe's published IP addresses. The signature is the check that ties a request to your signing secret.
Why does constructEvent say no signatures found matching the expected signature?
One of three inputs is wrong. Most often it is the secret: the `stripe listen` secret and the Dashboard endpoint secret differ, and test and live mode differ too. Next most often it is the body, changed by `req.json()` or by middleware. Last, check that you read the header named `stripe-signature` and pass it unmodified.
What is the difference between constructEvent and constructEventAsync?
Both do the same check. `constructEvent` uses Node crypto and runs synchronously. `constructEventAsync` uses Web Crypto and returns a promise, which is what Deno and edge runtimes need. On Supabase Edge Functions, call `constructEventAsync` and pass a SubtleCrypto provider. In a Node-based Next.js route, `constructEvent` works. Pick the one that matches your runtime, and keep the argument order the same.
Should I return 200 for an event I already handled?
Yes. Verify the signature first, then check whether you handled the event. If you did, skip the work and return 200. Stripe's docs say a manual resend does not cancel automatic retries, so a quick 200 on a repeat is how you stop them. A 400 on a repeat keeps Stripe retrying.
Does a valid signature mean the payment is real?
A valid signature means Stripe sent the event and nobody changed the body. It does not check your own logic. Match the event to an order you created, check the amount and currency against what you expected, and fetch the object from the Stripe API if you need its latest state. If you want a second look at your project, ShipSafe can scan it: [ship-safe.co/scan](/scan).

You just read what AI builders ship. Now check yours.

Paste your GitHub URL. 2 minutes. You get the findings in plain English, and the report says what it could not check. Free, no card.

Scan My App Free