# Duplicate WhatsApp webhooks in n8n: dedupe by message id

> Why WhatsApp providers deliver the same webhook twice, and an n8n pattern with the Webhook, Postgres and If nodes that processes each message once.

Published 2026-10-28. Z-API Omni team.

Respond 200 before anything else runs. Then insert the message id into a table with a unique constraint and continue only when the insert returns a row. Read-then-write still duplicates under load; the wiring below hands the decision to Postgres.

## Why the same webhook arrives twice

Providers retry when your endpoint does not answer 200 fast enough. Meta documents this for the WhatsApp Cloud API. When the endpoint returns a non-200 status, or delivery fails for another reason, Meta retries "with decreasing frequency until the request succeeds, for up to 7 days". The same page warns: "These retries can result in duplicate webhook notifications" ([Cloud API webhooks](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks)). The general Graph API guide puts the burden on you: "Your server should handle deduplication in these cases" ([Graph API webhooks](https://developers.facebook.com/docs/graph-api/webhooks/getting-started)). QR-connection providers retry on the same trigger. Read your provider's webhook page for its schedule.

n8n produces that timeout whenever the response waits on the workflow. With Respond set to When Last Node Finishes, the HTTP response waits for the CRM call, the model call and the reply to complete ([Webhook node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.webhook/)). On n8n Cloud, Cloudflare returns a 524 when the webhook has not responded within 100 seconds ([Webhook node common issues](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.webhook/common-issues/)). A slow CRM turns one message into a retried message, and a workflow that creates a contact on every run creates the contact twice.

## The message id is the idempotency key

An idempotency key identifies one event so that processing it a second time changes nothing. For inbound WhatsApp messages the key already exists. In Meta's Cloud API payload it is the `id` inside the `messages` array, a string that begins with `wamid.` ([payload examples](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples)). On a QR connection, the field name and format come from your provider's webhook documentation; for Z-API Omni, start at the [developer docs](https://developer.omni.z-api.io/en). This post calls the field `message_id` and reads it from the webhook body. Replace that path with the one your provider documents.

Two cautions on the key. Status webhooks carry the id of the outgoing message, and Meta states that one message can trigger up to three of them: sent, delivered and read ([payload examples](https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples)). If you dedupe status events, key on the id plus the status, or the delivered event is dropped because the sent event arrived first. And do not build a key from the sender's number and a timestamp. Two messages from one contact in the same second would collapse into one.

A third caution applies to numbers in coexistence with the WhatsApp Business app. Meta can first send an `unsupported` message with error `131060`, which it says typically resolves within a few seconds. A [Chatwoot bug report](https://github.com/chatwoot/chatwoot/issues/16680) shows the real message then arriving under the same `wamid`, sometimes minutes later. With the id as the only key, the placeholder wins and the real message is dropped. Do not mark an `unsupported` event as processed. Let a later event with content replace it, as shown in [the 131060 article](/academy/articles/whatsapp-coexistence-131060-unsupported-message-same-wamid/).

## Why "check, then insert" still duplicates

The obvious design reads the store, finds nothing, and writes. Two retries that arrive at the same moment both read nothing and both write. A Google Sheet has no unique constraint, so it cannot refuse the second row. A Get followed by a Set in the Redis node leaves the same gap. Hand the decision to a store that refuses the duplicate inside one operation.

In Postgres, that operation is an insert with `ON CONFLICT DO NOTHING` and a `RETURNING` clause. PostgreSQL returns only rows that were inserted or updated ([INSERT](https://www.postgresql.org/docs/current/sql-insert.html)), so the duplicate returns nothing. In Redis, `INCR` on a key named after the message id returns 1 the first time and a higher value on every retry. The command "Uses 0 as initial value if the key doesn't exist" and replies with the value after the increment ([INCR](https://redis.io/docs/latest/commands/incr/)). The n8n Redis node exposes this as Increment: "Atomically increments a key by 1. Creates the key if it doesn't exist" ([Redis node](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.redis/)). The pattern below uses Postgres; the Redis variant swaps one node and tests for 1.

## The n8n pattern

Create the table once:

```sql
CREATE TABLE processed_webhooks (
  message_id  text PRIMARY KEY,
  received_at timestamptz NOT NULL DEFAULT now()
);
```

Then build the workflow in this order:

1. Webhook node. HTTP Method POST, Respond set to Immediately, Response Code 200. n8n answers with the message "Workflow got started" before any other node runs ([Webhook node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.webhook/)). The provider records success and stops retrying.
2. Postgres node, operation Execute Query. Paste the query below. Under Options, add Query Parameters with the expression `{{ $json.body.message_id }}`. n8n sanitizes query parameters ([Postgres node](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.postgres/)), so never concatenate the id into the SQL string. In the node's Settings tab, turn on Always Output Data, which returns an empty item when the node produces no data ([node settings](https://docs.n8n.io/workflows/components/nodes/)). Without it, a duplicate ends the run silently and you cannot count retries.
3. If node. One condition of type String: `{{ $json.message_id }}` is not empty ([If node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.if/)). The true output carries a message you have never processed. The false output carries a retry.
4. Connect the CRM write, the model call and the reply to the true output. Leave the false output unconnected, or attach a node that logs the retry.

```sql
INSERT INTO processed_webhooks (message_id)
VALUES ($1)
ON CONFLICT (message_id) DO NOTHING
RETURNING message_id;
```

If your provider nests the id deeper than `body.message_id`, change the expression in step 2 and nothing else.

## Respond 200 first, process later

The 200 tells the provider you hold the event, and nothing more. Once you send it, the provider will not resend, so any failure after step 1 is yours to catch. Create a second workflow that starts with the Error Trigger node, then open Options > Settings in the main workflow and select it under Error workflow ([Error Trigger](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.errortrigger/)). It fires only for automatic executions, so test it with a real webhook rather than a manual run. For the node that talks to your CRM, enable Retry On Fail in its Settings tab so a short outage never reaches the error workflow ([node settings](https://docs.n8n.io/workflows/components/nodes/)).

This ordering has one consequence. The message id is marked as seen in step 2, before the CRM write in step 4. If the CRM write fails, the retry you would have welcomed is now filtered as a duplicate. Add a `status` column to `processed_webhooks` that the last node updates to `done`, and let the error workflow re-queue rows that stayed at `received`. Or save the inbound message to your own table before any external call, which is the sequence in [Save to the CRM before you reply](/academy/articles/save-to-crm-before-replying-whatsapp-automation/).

## If it still duplicates

Check that the provider has the production URL, not the test URL. The test URL listens for 120 seconds after you select Listen for test event and then stops. Nothing sent to it goes through the published workflow ([common issues](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.webhook/common-issues/)). Then check whether two workflows share one path and method. n8n allows a single webhook per path and method combination; unpublish one of them or change its path. Finally, read the ids in `processed_webhooks`. If the duplicates carry different ids, your provider sends more than one event per message, such as a message event and a status event. Add the event type to the key.

*Z-API Omni team*
