Full Byte Developer.

Six checks before your Stripe webhook goes live

Your webhook URL is public. A fake payment succeeded ships an order, and a real retry ships it twice.

Published here , tested on stripe-node 23.1.0, Express, dummy secret, no account · 1 source

Your webhook URL is public. A fake "payment succeeded" can ship an order. A real retry can ship it twice. Six checks before you go live, from Stripe's webhook docs (checked Nov 8).

1. Verify the signature on the raw body

Parse the signature before JSON parsing. Express parses JSON first by default, which alters the body and breaks verification [1].

Put the webhook route with raw parsing before app.use(express.json()):

javascript
app.post(
  "/webhook",
  express.raw({ type: "application/json" }),
  handleWebhook
);
app.use(express.json());

Stripe also lists IP allowlisting [1]. Use both.

2. Reject old timestamps

Stripe's libraries default to a 5-minute tolerance. Never set tolerance to 0: it turns the recency check off [1]. Keep your server clock on NTP.

3. Store processed event IDs and skip repeats

Sometimes two separate events carry the same payload. Dedupe on data.object.id plus event.type, not just the event ID [1].

javascript
const key = `${event.type}:${event.data.object.id}`;
if (processed.has(key)) {
  return res.sendStatus(200);
}
processed.add(key);

4. Never assume order

Events can arrive in any order. Fetch the object from the API when you need its current state. Do not use created to order or dedupe events [1].

5. Return 2xx fast, do the work in a queue

Return 200 before marking an invoice paid. Slow work belongs in a background queue.

javascript
queue.add({ eventId: event.id, type: event.type });
res.sendStatus(200);

6. Subscribe only to the event types you handle

Fewer events means fewer retries and less noise in your logs [1].

In my test

Five requests hit the handler: no signature header, guessed secret, signed but 10 minutes old, real signed event, and the same event again. Three returned 400. One order queued. One duplicate skipped. One order from five requests.

Send this to the friend about to put their Stripe webhook live this week.

Keep this guide

By Full Byte Developer

Filed under Dev tools, Security

Questions about this guide

Does this apply to webhooks from other services?

Signature, timestamp and idempotency apply broadly. Stripe-specific libraries handle most of it for you. Other services have their own signing methods.

Why return 200 before marking the invoice paid?

The sender times out or retries if you take too long. Return 2xx, then do the work in a queue where slow steps cannot block the response.

Sources

  1. Stripe docs: webhooksdocs.stripe.com

Next in this stack