Webhooks
Receiving and sending HTTP callbacks safely: signature checks on the raw body, an inbox, dedupe, fast 2xx and retries.
The doorbell and the post box. Outside businesses ring us, and we ring them.
That is a webhook. Instead of asking a provider every minute whether anything happened, you give them an address. When something happens, they knock on it.
How it works
A webhook is an HTTP callback. When an event happens in system A, A sends a POST to a URL registered by system B. Payment providers, phone carriers and messaging platforms talk to apps this way, and an app may send webhooks to its own customers.
Receiving
POST /webhooks/provider
→ read the raw body → verify signature and timestamp → reject if bad
→ insert into inbox, unique on provider + event id → on conflict: already seen
→ return 2xx immediately
→ background job processes the row → mark processed or failed
- Verify on the raw body, before parsing JSON. Re-serialised JSON breaks the signature.
- Return 2xx fast, well inside the sender's timeout. Do the work later.
- Deduplicate by event id. Senders retry, so duplicates are normal.
- Expect disorder. Events can arrive out of order. Re-read the current state from the source or compare versions, instead of applying events blindly.
- Keep the raw payload for replay and debugging, and alert on failed rows.
Sending
- Sign each payload with a per-endpoint secret and include a timestamp.
- Include a unique event id so receivers can deduplicate.
- Retry with exponential backoff and jitter, for hours or days. Disable endpoints that keep failing and tell the owner.
- Keep a delivery log with request, response code and latency, and a manual resend.
- Send from a queue, never inside the business transaction. An outbox fits here. See asynchronous processing.
- Guard against SSRF. Customer-supplied URLs must not be able to reach private address ranges.
Why the inbox matters
The inbox is a table that records every event before anything acts on it. It gives you deduplication through a unique key, a replay history, and a place to see what failed. Because the handler only stores the event and returns, a slow downstream step cannot cause the sender to time out and retry.
Testing a webhook flow
Test the awkward cases, not just the happy one. Send the same event twice and confirm only one effect happens. Send events out of order. Send a payload with a bad signature and one with an old timestamp. Make the downstream step fail and confirm the event stays in the inbox for replay. Providers usually offer a way to resend events or a local forwarding tool, which makes these checks cheap. A receiver that passes them will cope with the real internet, which duplicates, delays and reorders without warning.
Secrets and rotation
Every signed webhook relies on a shared secret. Store it like any other credential, never in the repository, and give each endpoint or provider its own. Plan rotation in advance: during a change, accept both the old and the new secret for a short window, so events in flight are not rejected. Log signature failures with the provider name, since a sudden burst of them usually means a rotated secret, a clock problem or someone probing the endpoint.
Where I used it
I haven't shipped this in a public project yet.
My case studies describe an outbox and idempotency keys for outgoing fiscal submissions, which are ideas this note shares, but they do not describe a webhook receiver or sender. So I will not claim one. This note is the design I would follow.
Trade-offs
| Benefit | Cost |
|---|---|
| Near real time, no polling | At-least-once delivery means dedupe work |
| Simple HTTP | Ordering is not guaranteed |
| Decouples systems | Public endpoints need signature checks and limits |
- Not for streams. For real-time two-way traffic, use WebSockets or a streaming API.
- Receivers must be reachable. If the other side cannot expose a public endpoint, offer polling or an events API.
- Secrets rotate. Signature secrets need a rotation plan that does not drop events.
A useful rule: verify, store, acknowledge, then process, and assume every event will arrive twice.