Webhook Integration
Intro
Webhooks deliver at least once, in no particular order, over a
network that fails. A correct webhook integration verifies HMAC
signatures on the raw body, deduplicates by event ID, returns 2xx
immediately and processes asynchronously, and retries with
exponential backoff into a dead-letter queue.
Overview
Payload envelope
{
"id": "evt_a1b2c3d4",
"type": "order.shipped",
"created_at": "2026-03-22T10:30:00Z",
"data": { "order_id": "ord_xyz789", "carrier": "ups" }
}
- A unique event
id for idempotency.
- A dotted
type of the form resource.action
(order.shipped, invoice.payment_failed).
- ISO 8601
created_at for ordering and replay-window enforcement.
- Domain payload nested under
data so the envelope stays stable as
the domain model evolves.
Signature verification (HMAC-SHA256)
Senders compute an HMAC over the raw request body using a shared
secret and send it in a header (e.g. X-Webhook-Signature: sha256=<hex>).
Consumers recompute and compare in constant time:
expected = hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
if not hmac.compare_digest(expected, received_signature):
return 401
Three things matter:
- Sign the raw body, not parsed JSON. Re-serializing reorders
keys and breaks the signature.
- Constant-time comparison (
hmac.compare_digest) — never
== — to avoid timing attacks.
- Replay protection. Include a timestamp in the signed payload
or as a separate signed header, and reject anything older than
~5 minutes.
Support multiple active secrets so you can rotate without downtime.
Idempotency
Webhook delivery is at-least-once. Consumers must dedupe:
- Store processed event IDs in a database or cache with a unique
constraint.
- Check before processing —
INSERT ... ON CONFLICT DO NOTHING is
the canonical pattern.
- TTL the idempotency store (e.g. 7 days) to bound storage. Senders
should not retry beyond that window.
Retry handling
As a sender: retry on transient failure with exponential backoff
(1 min, 5 min, 30 min, 2 hr, 8 hr, 24 hr is a typical schedule).
Treat 2xx as success, 5xx and timeouts as transient, and 4xx (except
429) as permanent failure. Include X-Webhook-Retry-Count so
consumers can log it.
As a consumer: verify the signature, enqueue the event for
asynchronous processing, and return 200/202 immediately. Never do
heavy work in the HTTP handler — the sender will time out and retry,
amplifying load.
Dead-letter queues
Events that exhaust the retry schedule move to a DLQ with full
context: payload, headers, last error, attempt count. Provide
operator tooling to inspect and replay. Alert when the DLQ grows
unexpectedly. Retain DLQ entries for at least 30 days so you can
recover from outages discovered after the fact.
Security
- TLS only. Never accept webhooks over plain HTTP.
- Always verify HMAC signatures, even if the source IP is
allowlisted.
- Restrict by sender IP ranges when the source publishes them
(Stripe, GitHub, etc.).
- Enforce a payload size limit (1 MB is typical) and rate-limit the
consumer endpoint.
- Store secrets in a vault (AWS Secrets Manager, Vault, etc.), not
in code or environment variables checked into source control.
Gotchas
Agent-specific failure modes — provider-neutral pause-and-self-check items:
- Verifying the HMAC signature against parsed and re-serialized JSON instead of the raw body. Re-serializing parsed JSON reorders keys, collapses whitespace, and changes the byte sequence — the computed HMAC will not match the sender's signature. Always compute the HMAC against the raw request body bytes, before any parsing.
- Non-constant-time signature comparison. Using
== to compare HMAC values allows a timing side-channel: an attacker can observe that the comparison returns faster when fewer bytes match, and iteratively guess the correct signature. Always use a constant-time comparison function such as hmac.compare_digest.
- No idempotency check — processing the same event twice. Webhook delivery is at-least-once. A network timeout or a 5xx response from your endpoint will cause the sender to retry, delivering the same event multiple times. A consumer that sends an email, charges a payment, or creates a database record without deduplicating by event ID will do so multiple times. Store processed event IDs and check before processing.
- Doing heavy processing in the HTTP handler instead of enqueuing. A handler that makes database queries, calls external APIs, or runs business logic before returning will frequently exceed the sender's request timeout (typically 5–30 seconds), causing the sender to retry — amplifying the work. Return 200 or 202 immediately after signature verification; enqueue the event payload for asynchronous processing.
- No replay-window check on the timestamp. A webhook signature protects the payload in transit but does not prevent replay attacks — a captured request can be replayed weeks later with a valid signature. Include a timestamp in the signed payload (or in a signed header), and reject events older than ~5 minutes to bound the replay window.
- Single signing secret with no rotation path. When a secret is leaked or an employee leaves, the secret must be rotated. If the consumer only accepts one active secret, rotation requires simultaneous updates to both the sender and consumer — a coordination window where webhooks either fail or are unverified. Support multiple active secrets so old events can still be verified during rotation.
- Returning 200 from a handler that errored internally. A handler that catches its own internal error and returns 200 tells the sender "delivery succeeded" — the sender will not retry, and the event is lost. Return 500 (after signature verification) so the sender retries. Reserve 4xx for cases where retrying will never succeed (malformed event, unsupported event type).
Full reference
Ordering
Webhooks do not guarantee delivery order. A consumer that assumes
"shipped" must arrive after "created" will eventually be wrong. Two
options:
- Use
created_at to detect out-of-order events and ignore the
stale one.
- Embed a monotonic version or sequence number on stateful
resources, then discard any event whose version is older than
what's already applied.
Both work; pick the one that fits the resource model.
Testing webhooks locally
- Tunnels:
ngrok and cloudflared tunnel expose a local
endpoint to a public URL so the sender can reach it.
- Inspection:
webhook.site and requestbin capture raw
requests for offline debugging when the sender doesn't expose a
test mode.
- Automated tests must cover: invalid signature (reject), valid
signature (accept), duplicate event ID (idempotent — process
once), out-of-order events, malformed JSON, oversized payload,
expired timestamp.
Failure modes worth designing for
| Symptom |
Likely cause |
| Events processed twice |
No idempotency check |
| Sender reports timeouts |
Synchronous heavy processing |
| Signature verification fails |
Parsed body re-serialized |
| Random 401s after secret rotation |
Old secret no longer accepted |
| State diverges from sender |
Out-of-order delivery ignored |
| DLQ grows unexpectedly |
Downstream system degraded |
The fix for the first two is the same on every platform: dedupe by
event ID with a unique constraint, and move processing to a
background queue so the HTTP handler returns in milliseconds.
Webhook registration API (when designing your own)
If you're publishing webhooks rather than consuming them, expose a
simple registration API:
POST /webhooks — register a url, an events[] filter, and
receive a generated signing secret in the response (shown once).
GET /webhooks/{id}/deliveries — recent delivery attempts with
status, latency, and response body for debugging.
POST /webhooks/{id}/deliveries/{delivery_id}/replay — manual
replay for ops recovery.
- Admin dashboard with delivery success rate, latency percentiles,
and DLQ depth per subscription.
Anti-patterns to avoid
- Synchronous processing in the HTTP handler. Always enqueue and
return immediately.
- Verifying the signature against parsed JSON. Sign and verify
the raw bytes the sender produced.
== for signature comparison. Use a constant-time function.
- No idempotency store. "It probably won't happen twice" is
wrong on the day it matters.
- Returning 200 from a handler that errored internally. The
sender will not retry. Return 500 so retry happens, after the
signature has been verified.
- No replay-window check. A leaked recording can be replayed
weeks later.
- Single signing secret with no rotation path. Plan rotation
before you need it.
1---2name: webhook-integration3description: |4 Webhook design and consumption — payload format, HMAC signatures, idempotency, retries, dead-letter queues, security. Use when implementing a webhook consumer, designing an event-notification system, adding signature verification, or debugging duplicate or failed webhook deliveries.5metadata:6 source: https://skillmd.com/skills/projectious-work/webhook-integration7 processkit:8 apiVersion: processkit.projectious.work/v19 id: SKILL-webhook-integration10 version: "1.1.0"11 created: 2026-04-06T00:00:00Z12 category: engineering13---1415# Webhook Integration1617## Intro1819Webhooks deliver at least once, in no particular order, over a20network that fails. A correct webhook integration verifies HMAC21signatures on the raw body, deduplicates by event ID, returns 2xx22immediately and processes asynchronously, and retries with23exponential backoff into a dead-letter queue.2425## Overview2627### Payload envelope2829```json30{31 "id": "evt_a1b2c3d4",32 "type": "order.shipped",33 "created_at": "2026-03-22T10:30:00Z",34 "data": { "order_id": "ord_xyz789", "carrier": "ups" }35}36```3738- A unique event `id` for idempotency.39- A dotted `type` of the form `resource.action`40 (`order.shipped`, `invoice.payment_failed`).41- ISO 8601 `created_at` for ordering and replay-window enforcement.42- Domain payload nested under `data` so the envelope stays stable as43 the domain model evolves.4445### Signature verification (HMAC-SHA256)4647Senders compute an HMAC over the **raw request body** using a shared48secret and send it in a header (e.g. `X-Webhook-Signature: sha256=<hex>`).49Consumers recompute and compare in constant time:5051```python52expected = hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()53if not hmac.compare_digest(expected, received_signature):54 return 40155```5657Three things matter:58591. **Sign the raw body**, not parsed JSON. Re-serializing reorders60 keys and breaks the signature.612. **Constant-time comparison** (`hmac.compare_digest`) — never62 `==` — to avoid timing attacks.633. **Replay protection.** Include a timestamp in the signed payload64 or as a separate signed header, and reject anything older than65 ~5 minutes.6667Support multiple active secrets so you can rotate without downtime.6869### Idempotency7071Webhook delivery is at-least-once. Consumers must dedupe:7273- Store processed event IDs in a database or cache with a unique74 constraint.75- Check before processing — `INSERT ... ON CONFLICT DO NOTHING` is76 the canonical pattern.77- TTL the idempotency store (e.g. 7 days) to bound storage. Senders78 should not retry beyond that window.7980### Retry handling8182**As a sender:** retry on transient failure with exponential backoff83(1 min, 5 min, 30 min, 2 hr, 8 hr, 24 hr is a typical schedule).84Treat 2xx as success, 5xx and timeouts as transient, and 4xx (except85429) as permanent failure. Include `X-Webhook-Retry-Count` so86consumers can log it.8788**As a consumer:** verify the signature, enqueue the event for89asynchronous processing, and return 200/202 immediately. Never do90heavy work in the HTTP handler — the sender will time out and retry,91amplifying load.9293### Dead-letter queues9495Events that exhaust the retry schedule move to a DLQ with full96context: payload, headers, last error, attempt count. Provide97operator tooling to inspect and replay. Alert when the DLQ grows98unexpectedly. Retain DLQ entries for at least 30 days so you can99recover from outages discovered after the fact.100101### Security102103- TLS only. Never accept webhooks over plain HTTP.104- Always verify HMAC signatures, even if the source IP is105 allowlisted.106- Restrict by sender IP ranges when the source publishes them107 (Stripe, GitHub, etc.).108- Enforce a payload size limit (1 MB is typical) and rate-limit the109 consumer endpoint.110- Store secrets in a vault (AWS Secrets Manager, Vault, etc.), not111 in code or environment variables checked into source control.112113## Gotchas114115Agent-specific failure modes — provider-neutral pause-and-self-check items:116117- **Verifying the HMAC signature against parsed and re-serialized JSON instead of the raw body.** Re-serializing parsed JSON reorders keys, collapses whitespace, and changes the byte sequence — the computed HMAC will not match the sender's signature. Always compute the HMAC against the raw request body bytes, before any parsing.118- **Non-constant-time signature comparison.** Using `==` to compare HMAC values allows a timing side-channel: an attacker can observe that the comparison returns faster when fewer bytes match, and iteratively guess the correct signature. Always use a constant-time comparison function such as `hmac.compare_digest`.119- **No idempotency check — processing the same event twice.** Webhook delivery is at-least-once. A network timeout or a 5xx response from your endpoint will cause the sender to retry, delivering the same event multiple times. A consumer that sends an email, charges a payment, or creates a database record without deduplicating by event ID will do so multiple times. Store processed event IDs and check before processing.120- **Doing heavy processing in the HTTP handler instead of enqueuing.** A handler that makes database queries, calls external APIs, or runs business logic before returning will frequently exceed the sender's request timeout (typically 5–30 seconds), causing the sender to retry — amplifying the work. Return 200 or 202 immediately after signature verification; enqueue the event payload for asynchronous processing.121- **No replay-window check on the timestamp.** A webhook signature protects the payload in transit but does not prevent replay attacks — a captured request can be replayed weeks later with a valid signature. Include a timestamp in the signed payload (or in a signed header), and reject events older than ~5 minutes to bound the replay window.122- **Single signing secret with no rotation path.** When a secret is leaked or an employee leaves, the secret must be rotated. If the consumer only accepts one active secret, rotation requires simultaneous updates to both the sender and consumer — a coordination window where webhooks either fail or are unverified. Support multiple active secrets so old events can still be verified during rotation.123- **Returning 200 from a handler that errored internally.** A handler that catches its own internal error and returns 200 tells the sender "delivery succeeded" — the sender will not retry, and the event is lost. Return 500 (after signature verification) so the sender retries. Reserve 4xx for cases where retrying will never succeed (malformed event, unsupported event type).124125## Full reference126127### Ordering128129Webhooks do **not** guarantee delivery order. A consumer that assumes130"shipped" must arrive after "created" will eventually be wrong. Two131options:1321331. **Use `created_at`** to detect out-of-order events and ignore the134 stale one.1352. **Embed a monotonic version or sequence number** on stateful136 resources, then discard any event whose version is older than137 what's already applied.138139Both work; pick the one that fits the resource model.140141### Testing webhooks locally142143- **Tunnels:** `ngrok` and `cloudflared tunnel` expose a local144 endpoint to a public URL so the sender can reach it.145- **Inspection:** `webhook.site` and `requestbin` capture raw146 requests for offline debugging when the sender doesn't expose a147 test mode.148- **Automated tests** must cover: invalid signature (reject), valid149 signature (accept), duplicate event ID (idempotent — process150 once), out-of-order events, malformed JSON, oversized payload,151 expired timestamp.152153### Failure modes worth designing for154155| Symptom | Likely cause |156|------------------------------------|--------------------------------|157| Events processed twice | No idempotency check |158| Sender reports timeouts | Synchronous heavy processing |159| Signature verification fails | Parsed body re-serialized |160| Random 401s after secret rotation | Old secret no longer accepted |161| State diverges from sender | Out-of-order delivery ignored |162| DLQ grows unexpectedly | Downstream system degraded |163164The fix for the first two is the same on every platform: dedupe by165event ID with a unique constraint, and move processing to a166background queue so the HTTP handler returns in milliseconds.167168### Webhook registration API (when designing your own)169170If you're publishing webhooks rather than consuming them, expose a171simple registration API:172173- `POST /webhooks` — register a `url`, an `events[]` filter, and174 receive a generated signing secret in the response (shown once).175- `GET /webhooks/{id}/deliveries` — recent delivery attempts with176 status, latency, and response body for debugging.177- `POST /webhooks/{id}/deliveries/{delivery_id}/replay` — manual178 replay for ops recovery.179- Admin dashboard with delivery success rate, latency percentiles,180 and DLQ depth per subscription.181182### Anti-patterns to avoid183184- **Synchronous processing in the HTTP handler.** Always enqueue and185 return immediately.186- **Verifying the signature against parsed JSON.** Sign and verify187 the raw bytes the sender produced.188- **`==` for signature comparison.** Use a constant-time function.189- **No idempotency store.** "It probably won't happen twice" is190 wrong on the day it matters.191- **Returning 200 from a handler that errored internally.** The192 sender will not retry. Return 500 so retry happens, after the193 signature has been verified.194- **No replay-window check.** A leaked recording can be replayed195 weeks later.196- **Single signing secret with no rotation path.** Plan rotation197 before you need it.