How do webhooks work?
When a customer pays through Stripe, several things need to happen in the app that sold them something: a receipt goes to their email, the sale is recorded in the database, and the customer gets access to what they bought. None of that can start until the app knows the payment went through. A webhook is how that information gets there: Stripe sends an HTTP request to a URL on your server the moment the payment succeeds. The idea is simple. Getting it right in production depends on a handful of details around that one request.
Polling and webhooks
There are two ways for a server to learn that something happened somewhere else. The first is polling: the server asks the other service on a schedule whether anything has changed. Most of those requests come back empty, and when something has changed, the server only finds out on the next check.
A webhook reverses the direction. You register a URL with the service once. When an event happens, the service sends a request to that URL. The information is the same; the difference is who starts the conversation.
What a webhook request contains
A webhook is an ordinary HTTP POST with a JSON body. In Stripe's case the body is an event object. It has an id starting with evt_, a type such as payment_intent.succeeded, and a data.object field holding the object as it was when the event happened.
The event id is the most useful field in the payload. It is how a handler tells a new event apart from a repeat delivery of one it has already seen.
Other services follow the same pattern with different payloads. RevenueCat sends events for subscription changes. Supabase database webhooks send a payload with the event type (INSERT, UPDATE or DELETE), the table, the schema, the new record and the old_record.
Verifying the signature, and why the raw body matters
A webhook endpoint is a public URL. Anyone can send a POST request to it. If the handler marks orders as paid whenever it sees a payment event, then without verification anyone could mark an order as paid.
Stripe solves this by signing every request. The Stripe-Signature header contains a timestamp (t=) and a signature (v1=). The signature is an HMAC using SHA-256, computed over the timestamp, a full stop, and the request body, keyed with the endpoint's signing secret (the value starting whsec_). The receiving server repeats the calculation with its copy of the secret and compares the results.
The calculation runs over the exact bytes Stripe sent. Many frameworks parse JSON bodies automatically before the handler runs. Once the body has been parsed and turned back into a string, whitespace and key order can change, and the signature no longer matches. Stripe's documentation is explicit that any manipulation of the raw body makes verification fail. In Express, this means the webhook route is registered with a raw body parser before express.json() is applied.
The timestamp is part of the signed content, so an attacker cannot change it without breaking the signature. Stripe's libraries reject events whose timestamp is more than five minutes from the current time by default, which stops an intercepted request from being replayed later.
Retries, duplicates and order
If the handler does not respond with a 2xx status code, the sender treats the delivery as failed. For Stripe this includes timeouts, server errors and redirects. Stripe then retries. In live mode it attempts delivery for up to three days with exponential backoff. In a sandbox it retries three times over a few hours.
These numbers are Stripe's. Other providers choose their own. RevenueCat retries up to five times, after 5, 10, 20, 40 and 80 minutes, and then stops. Supabase database webhooks are built on the pg_net extension, and its documentation does not describe automatic retries.
Retries create duplicates. If a handler finishes its work but the response is lost or arrives too late, the sender has no way to know it succeeded and sends the event again. Stripe's documentation states that endpoints might occasionally receive the same event more than once, and that events are not guaranteed to arrive in the order they were generated.
Idempotent handlers
A handler is idempotent when processing the same event twice has the same effect as processing it once. The usual approach is to store the IDs of processed events in a table with a unique constraint. When an event arrives, the handler tries to insert its ID. If the insert succeeds, the event is new and the work goes ahead. If it conflicts, the event has already been handled and the handler returns 200 without doing anything else.
Stripe notes one further case: occasionally two separate event objects are generated for the same change. For those, the object ID in data.object combined with the event type identifies the duplicate.
The same idea applies in the other direction. Stripe accepts an Idempotency-Key header on POST requests. It saves the status code and body of the first request made with a given key and returns that saved result for any later request with the same key. Keys can be removed once they are at least 24 hours old.
On my delivery app, an idempotency key was built from the user id, the vendor id and the amount, rather than from an order id. This failed in two ways: a request with the same total but a different parameter was rejected outright, since Stripe will not accept a reused key with mismatched parameters, and an identical repeat order within 24 hours returned the result of the earlier payment. The key has to identify exactly one operation. The same rule holds for event IDs on the receiving side.
Respond first, work after
Stripe's documentation says the endpoint should return a 2xx status quickly, before any complex logic that could cause a timeout. A slow handler produces timeouts, and timeouts produce retries.
A common structure is to verify the signature, record the event, place the work in a jobs table or queue, and return 200. A separate worker then does the slower parts: updating several tables, sending receipts, calling other APIs. RevenueCat gives the same advice and disconnects if a response takes longer than 60 seconds.
Testing webhooks locally
A development machine is not reachable from the public internet, so Stripe cannot deliver to it directly. The Stripe CLI handles this. stripe listen --forward-to localhost:4242/webhook receives events from Stripe and forwards them to the local server, and stripe trigger payment_intent.succeeded creates a test event.
The CLI prints its own signing secret when stripe listen starts. It is different from the secret of any endpoint registered in the Dashboard, so the local environment uses the CLI's secret.