Skip to content

Verify Stripe webhooks

Verify that a webhook really came from Stripe before acting on it.

Identifier 'stripe'
Import import { verifyStripe } from 'verihook/stripe'
Headers stripe-signature
Signature HMAC-SHA256 of <timestamp>.<raw body>, sent as t=<timestamp>,v1=<hex>.
Replay window 300 seconds (options.tolerance)
result.eventType the body’s type, e.g. payment_intent.succeeded

The endpoint’s signing secret (whsec_...). In the Stripe Dashboard, open Developers → Webhooks, select the endpoint and reveal the signing secret. When you forward events with stripe listen, use the whsec_... value the CLI prints instead.

Store it in an environment variable (STRIPE_WEBHOOK_SECRET below) and never commit it.

import { verifyStripe } from 'verihook/stripe';
export async function POST(request: Request) {
const result = await verifyStripe(request, process.env.STRIPE_WEBHOOK_SECRET!);
if (!result.valid) {
console.warn(result.code, result.reason, result.hint);
return new Response('Invalid signature', { status: 401 });
}
console.log(result.eventType, result.event);
return Response.json({ received: true });
}

request can be a Fetch Request or { headers, body, url? } with the raw body. verifyWebhook('stripe', request, secret) from verihook does the same. Read why the raw body matters if verification fails behind a body parser.

app/api/webhooks/stripe/route.ts
import { createWebhookHandler } from 'verihook/next';
export const POST = createWebhookHandler('stripe', process.env.STRIPE_WEBHOOK_SECRET!, async (payload, result) => {
// Runs only for a valid signature
console.log(result.eventType, result.event);
});

Each adapter responds 401 to an invalid signature, 413 to a body over maxBodySize (2 MB by default) and 200 to a duplicate when you pass a dedupeStore.

  • Each endpoint has its own signing secret, and test mode and live mode secrets differ.
  • A Stripe API key (sk_..., rk_...) is not a webhook secret. verihook’s result.hint points this out.
  • Pass Stripe.Event as a type argument (verifyStripe<Stripe.Event>(...)) for the official SDK’s types.

signWebhook from verihook/testing builds a correctly signed Stripe request for your tests:

import { signWebhook } from 'verihook/testing';
import { verifyStripe } from 'verihook/stripe';
const hook = await signWebhook('stripe', {
secret: 'whsec_test_secret',
payload: { id: 'evt_test' },
});
const result = await verifyStripe(
{ headers: hook.headers, body: hook.body, url: hook.url },
hook.secret,
);
// result.valid === true

From the command line, npx verihook simulate stripe --url http://localhost:3000/webhooks/stripe sends one to your local server.

See Testing your handlers for supertest and Fetch Request examples.

Failed results carry a code and often a hint with the likely cause. See Troubleshooting for each error code.

Stripe’s webhook documentation