Webhooks: sending and receiving
A payment provider has to tell your shop that a card payment went through. It sends an HTTP request to a URL you gave it. That URL is public, so anyone can call it, and the real sender will sometimes call it twice. Here is how both sides make that safe.
01 Don’t poll, get called
Without webhooks, your shop asks the provider “has pay_7 settled yet?” every minute, for every open payment. Most answers are “no”. A webhook turns that around: the provider sends one POST to your URL when something happens.
The sender is the one with news: a payment provider, a code host, a chat app. Your receiver is just an HTTP route. The hard part is that your route cannot tell a real call from a fake one, or a first call from a repeat, unless the request carries proof.
02 What a webhook request carries
This tutorial uses a simple scheme close to what most providers do. Names differ: Stripe sends Stripe-Signature: t=…,v1=…, GitHub sends X-Hub-Signature-256: sha256=…. The ideas are the same.
| Part | Lets the receiver |
|---|---|
| Webhook-Id | Spot a delivery it has already handled |
| Webhook-Timestamp | Refuse requests signed long ago |
| Webhook-Signature | Check that the sender made exactly these bytes |
| Body | Learn what happened; often just ids, to fetch the details |
The signature is an HMAC-SHA256 of id.timestamp.body, made with a secret that only the sender and you know. The provider shows it once when you register the URL.
03 Sending: queue, sign, retry
A sender that just calls fetch from its payment code loses events whenever the receiver is down. Real senders put each event on a queue and let workers deliver it, retrying until they get a 2xx.
- just happened
- being sent
- delivered
- attempt failed
A customer’s card payment pay_7 succeeds at the payment provider. Your shop has asked to be told. Nothing is queued yet.
The last line matters most. A timeout tells the sender nothing about what your code did, so retries are designed in. Every sender that retries delivers at least once, which means sometimes more than once.
04 Receiving: verify, check the time, dedupe
The receiver runs three checks in order. Each one stops a different problem.
- just arrived or decided
- passes, but isn’t enough
- what settles it
- rejected
Your receiver holds the shared secret and an empty store of handled event ids. It accepts signatures up to 5 minutes old.
Keep handled ids for at least as long as the sender keeps retrying, which for many providers is days. A database table with a unique key on the id does the job and survives restarts.
05 A receiver in plain Node
This receiver uses only Node’s built-in modules. It reads the raw body, rejects stale or unsigned requests, and ignores ids it has handled.
To test it, sign requests the way a sender would. This script uses openssl and curl.
That is a real run with Node 24 and OpenSSL 3. Change one character of the body after signing and the answer is bad signature. The Set is only for the demo: it forgets everything on restart.
Verify the raw bytes you received, before any JSON parsing. A framework that parses the body and you then re-serialise it may reorder keys or change spacing, and the HMAC will never match. In Express, use express.raw() on the webhook route.
06 Answer fast, work later
Senders wait only briefly; GitHub, for example, treats a delivery as failed if you take more than 10 seconds. If your handler sends emails or calls other services before answering, slow days turn into timeouts, and timeouts into retries of work that already ran.
app.post('/webhooks', async (req, res) => {verify(req);await fulfilOrder(req.body); // secondsawait sendReceiptEmail(req.body);res.sendStatus(200);});
app.post('/webhooks', async (req, res) => {verify(req);await saveEventOnce(req.body); // insert, unique idres.sendStatus(200);// a background worker fulfils the order});
Events can also arrive out of order: a retry of an older event may land after a newer one. Treat the event as a hint and read the current state from the sender’s API, or compare timestamps before you apply it.
07 When it bites
JSON.stringify(req.body)Signing the parsed bodyThe re-serialised JSON is not the bytes the sender signed. Keep the raw body for the check.sig === expectedA plain string compareIt can return as soon as one character differs, which leaks timing. Use crypto.timingSafeEqual or hmac.compare_digest.HMAC(body) onlyNo timestamp in the signatureA captured request stays valid forever. Sign the time with the body and enforce a window of a few minutes.500 on unknown typeFailing on events you don’t useThe sender retries them for days. Answer 200 to event types you ignore.url = customer inputSending to any URL (sender side)A customer can register http://169.254.169.254/ or an internal host. Resolve the address and refuse private ranges before each delivery.