Idempotency for WhatsApp webhooks: deduplication
Webhooks arrive at least once, not exactly once. Learn which identifier to deduplicate on, where to store it and how not to lose events when discarding.
A reliable webhook is not one that arrives exactly once; it is one that, arriving two, three or five times, produces the same result as arriving once. That fancy word, idempotency, is what separates a bot that replies once from one that sends the customer the same answer three times. This article shows where duplicates come from, which identifier to use and a simple implementation pattern.
Why the same event arrives more than once
It arrives duplicated because delivery is, by design, at least once. Meta documents that if your endpoint responds with anything other than 200 or delivery fails for another reason, it retries for up to 7 days, and warns that those retries can produce duplicate notifications. Its recommendation matches this article: your server should deduplicate.
CRPRO Hub adds a second layer. A delivery that fails (timeout, non-2xx response, connection error) is resent up to nine times, for ten attempts in about four days. And there is the manual replay, which the operator triggers after fixing a problem. An event can therefore arrive by three paths: the first delivery, the retry and the replay.
There is also the duplicate you create yourself: your handler processes the event, takes too long to reply, the delivery times out and comes back. The work was done, the acknowledgement did not arrive, and the event reappears. It is the most common cause of duplicates in production, and the reason to reply fast and process later.
Which identifier to use
Use the event identifier, not the delivery identifier. In the Hub, on an endpoint with the normalized payload, the id field of the body repeats on retry and on replay; it is the right key. The X-Hub-Delivery-Id header is the same across all attempts of one delivery, but changes on a replay, so anyone deduplicating only on it processes the replay again.
| Situation | X-Hub-Delivery-Id | event id in the body |
|---|---|---|
| Automatic retry | same | same |
| Manual replay | new | same |
| Different event | different | different |
On endpoints with Meta's raw payload (meta_raw mode) the body has no top-level id. The key is then the message identifier, the wamid, which comes inside Meta's payload. For received messages it is in messages; for state updates, the id of the item in statuses is the wamid of the message you sent, and several different statuses share the same id. So for statuses the key must combine the wamid with the state: wamid plus sent, delivered or read.
A concrete example: a table of events already seen
The most robust pattern is a table with a primary key on the identifier and an insert that fails silently if it already exists. Deduplication stops depending on process memory and is guaranteed by the database, which matters when several workers consume the queue in parallel.
-- table of events already seen
CREATE TABLE eventos_processados (
evento_id text PRIMARY KEY,
recebido_em timestamptz NOT NULL DEFAULT now()
);
-- in the worker: try to insert; if it already existed, do nothing
INSERT INTO eventos_processados (evento_id)
VALUES ($1)
ON CONFLICT (evento_id) DO NOTHING
RETURNING evento_id;
-- row returned = first time, process it. No row = duplicate, discard.The subtle point is atomicity. If you run a SELECT to see whether the event exists and then an INSERT, two workers can pass the SELECT at the same time and process twice. With ON CONFLICT the decision happens in a single statement: only whoever inserted processes.
There is a second concern, about ordering. If you mark the event as seen and processing then fails, it will never be redone, because the retry will be discarded as a duplicate. Two outs work: mark and process in the same database transaction, or record a state (received, completed) and discard only when completed. Which to choose depends on whether your side effect is transactional.
Idempotency also applies to what you send
Deduplicating input is not enough if output can repeat. When your handler replies to the customer, a retry of the handler itself may send the message twice. The Hub's send API accepts an Idempotency-Key header for exactly this, and the receiving guide's example derives the key from the identifier of the received message, not from a random value. That way the same source message always yields the same key, and the second attempt does not become a second send.
The general rule: every idempotency key must be deterministic from the fact that caused it. A random key generated on each attempt protects nothing, because every attempt looks new.
How long to keep the identifiers
Keep them longer than the longest retry window you expect. Meta retries for up to 7 days and the Hub for about four, and a manual replay can happen after that. A retention of a few weeks covers normal cases with room to spare and costs little, because a row holds only an identifier and a timestamp.
Cleaning the table periodically is safe as long as the period exceeds any possible retry. Remember too that a replay of an already successful delivery will be dropped by your deduplication, and that is the right behavior; if you truly need to reprocess, the way is to replay a delivery that ended up dead, or to delete the event record first. Replay details are in the Hub webhooks reference.
Frequent pitfalls
The most common failures are deduplicating on the wrong identifier, deduplicating in memory and forgetting atomicity. The first lets replays through; the second loses history on every deploy or process crash; the third creates a race between workers. Some people deduplicate on message content, and that discards legitimate messages: a customer who writes "ok" twice is not duplicating anything.
- Wrong key: use the event id, or wamid plus state, never the delivery id alone.
- In-memory dedupe: use a database or shared persistent cache.
- SELECT followed by INSERT: use an atomic conditional insert.
- Marking seen before completing: record a state or use a transaction.
- Comparing by text: never discard on identical content.
To start sending authenticated calls from your handler, see the Hub quickstart. If duplicates show up and you do not know where from, check the delivery list of your endpoint: a high attempts count with a 5xx status points at your own receiver.
Frequently asked questions
Can I trust that each webhook arrives only once?
No. Delivery is at least once. Meta warns that retries can produce duplicates, and the Hub also retries and allows replay.
Should I deduplicate on X-Hub-Delivery-Id?
Not on its own. It repeats on retry but changes on a manual replay. Use the event id in the body.
What about meta_raw mode, which has no top-level id?
Use the message id (wamid) from Meta's payload. For statuses, combine the wamid with the state, because several states share the same id.
How long should I keep the ids I have seen?
Longer than the longest retry window you expect: 7 days at Meta, about four at the Hub. A few weeks is a comfortable margin.
Fontes
Documentação oficial da Meta, conferida em 31 de outubro de 2026.