# How to put an AI agent on WhatsApp with n8n

> Seven n8n nodes that take a WhatsApp message in, drop duplicates, keep context, call the model, apply guardrails, send the reply and log it.

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

A WhatsApp agent in n8n is seven nodes. A Webhook node receives, a lookup drops duplicates and a memory sub-node keeps context. The AI Agent node answers, an If node applies guardrails, an HTTP Request node sends the reply and a Postgres node logs it. The chain works with any messaging API that delivers inbound messages to a URL and accepts outbound messages over HTTP. Every node and setting below is named as the [n8n docs](https://docs.n8n.io/) spell it, so you can search for it in the editor.

## Step 1: receive the message

Add a Webhook node with HTTP Method set to POST and a Path you choose. Set Authentication to Header auth so your provider must send a secret header. Set Respond to Immediately, which returns "Workflow got started" before anything else runs ([Webhook node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.webhook/)). Providers retry when they do not get a fast 2xx, and a model call takes seconds, so answering first is what keeps retries away.

Your provider's payload has its own field names. Add an Edit Fields (Set) node right after the webhook, in Manual Mapping mode. Set four fields you will use everywhere else: `message_id`, `phone`, `text` and `from_me` ([Edit Fields (Set) node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.set/)). Everything downstream reads these names, so changing providers later means changing one node.

Add an If node with the condition `from_me` is true, and end the true branch. Messages your own number sent (from the phone, from a human agent, from this workflow) must never feed the agent, or it will answer itself.

## Step 2: drop duplicates

Use `message_id` as the idempotency key: look it up in Postgres or Redis, continue only when it is new, and write it before you call the model. The pattern, with the n8n nodes, is in [Duplicate WhatsApp webhooks in n8n: dedupe by message id](/academy/articles/n8n-duplicate-webhooks-idempotency/). Skip this and a slow model call plus one provider retry means two replies to the same question.

## Step 3: keep the conversation

The AI Agent node takes a memory sub-node. Simple Memory is the quick one, with a Session Key and a Context Window Length (the number of previous interactions it keeps). Its docs carry a warning for queue mode: "this node doesn't work in an active production workflow". The reason given is that n8n "can't guarantee that every call to Simple Memory will go to the same worker" ([Simple Memory](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memorybufferwindow/)).

For anything past a test, use Postgres Chat Memory or Redis Chat Memory. Postgres Chat Memory stores history in a Table Name it creates if missing ([Postgres Chat Memory](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memorypostgreschat/)). Redis Chat Memory adds a Session Time To Live in seconds ([Redis Chat Memory](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memoryredischat/)). Set Session Key to the expression `{{ $json.phone }}` so each WhatsApp contact gets its own thread. Both docs note that all memory nodes in a workflow share one instance unless their session keys differ. That is why the key has to be the contact and not a constant.

## Step 4: call the model

Add the AI Agent node. Since n8n 1.82.0 the agent type setting is deprecated and every AI Agent node runs as a Tools Agent ([AI Agent node](https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.agent/)). Set Prompt (User Message) to Define below and enter `{{ $json.text }}`. The other option, Take from previous node automatically, expects a field named `chatInput`, which a webhook payload does not have ([Tools Agent](https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.agent/tools-agent/)).

Under Options, write the System Message and leave Max Iterations at its default of 10 unless the agent calls tools in loops. Connect a chat model sub-node such as OpenAI Chat Model or Anthropic Chat Model. Set Maximum Number of Tokens and Sampling Temperature in its options. The OpenAI node also exposes Timeout and Max Retries ([OpenAI Chat Model](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.lmchatopenai/), [Anthropic Chat Model](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.lmchatanthropic/)).

A system message that holds up on WhatsApp states the channel and the exits:

```text
You answer customers of ACME_NAME on WhatsApp.
Reply in the customer's language, in at most 600 characters, plain text, no Markdown.
Answer only what the customer asked. Do not offer to send documents, links or follow-ups unless asked.
If the customer asks for a person, asks about a refund, or you are not sure, reply with exactly: HANDOFF
```

To call a model's API directly instead, replace the AI Agent node with an HTTP Request node and build the messages array from the memory table yourself. You lose the memory sub-node and tool handling, and gain a payload you control completely.

## Step 5: guardrails before anything is sent

Add an If node after the agent with three conditions joined by OR ([If node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.if/)). The true branch goes to a human handoff instead of the send step:

1. `{{ $json.output }}` String is empty.
2. `{{ $json.output }}` String contains `HANDOFF`.
3. `{{ $json.output.length }}` Number is greater than 1000.

The handoff branch writes the contact and the last message to the table your inbox or CRM reads. It then sends the customer a fixed sentence saying a person will continue. The model never writes that sentence, so it cannot drift.

Two rules sit outside any node. The agent gets no tool that sends messages; the only send is the HTTP Request node in step 6, which runs once per inbound message. And the workflow has no trigger other than the webhook, so the agent cannot message anyone who did not write first. If you need proactive notifications, build them in a separate workflow with their own approval path.

## Step 6: send the reply

Add an HTTP Request node. Set Method to POST and URL to your provider's send endpoint. Set Authentication to the generic Header auth credential, or a predefined one if n8n lists your provider. Turn Send Body on with Body Content Type JSON ([HTTP Request node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.httprequest/)). Under Options, set Timeout so a stalled provider does not hold the execution. In the node's Settings tab, turn on Retry On Fail and set On Error to Continue (using error output). A failed send then still reaches the log in step 7 ([node settings](https://docs.n8n.io/workflows/components/nodes/)).

The body is whatever your API expects: the `phone` and `output` fields from the earlier nodes, in its names. Z-API Omni is one API that fits this node; its send calls are documented at [developer.omni.z-api.io](https://developer.omni.z-api.io/en).

<!-- TODO:test Omni snippet: HTTP Request node body and headers for sending a text message, copied from the tested Omni docs -->

If your number runs on Meta's official API, one more check belongs before this node. Meta lets you send free-form messages only while the 24-hour customer service window is open. Outside it, "businesses can only send template messages to reach out to users" ([Meta pricing page](https://developers.facebook.com/docs/whatsapp/pricing), accessed 2026-10-28). An agent replying to a message the customer sent minutes ago is inside the window, but a queued reply delivered the next day is not. Error 131047 and how to detect the window are in [WhatsApp error 131047: the 24-hour window and templates](/academy/articles/whatsapp-error-131047-24-hour-window-templates/).

## Step 7: log everything

Add a Postgres node with Operation set to Insert ([Postgres node](https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.postgres/)) and write one row per inbound message:

```sql
CREATE TABLE agent_log (
  message_id   text PRIMARY KEY,
  phone        text NOT NULL,
  received_at  timestamptz NOT NULL DEFAULT now(),
  inbound      text NOT NULL,
  outbound     text,
  handoff      boolean NOT NULL DEFAULT false,
  send_status  integer,
  send_error   text,
  model        text,
  latency_ms   integer
);
```

Fill `send_status` and `send_error` from the HTTP Request node's output, including its error output. Then set an Error workflow in the workflow Settings, starting with the Error Trigger node. A crash anywhere in the chain then notifies you instead of silently dropping the customer ([error handling](https://docs.n8n.io/flow-logic/error-handling/)).

A 2xx from the send endpoint means your provider accepted the message. Delivery is a separate event that arrives later on the webhook, if your provider sends status updates. Log both, and read the gap between them before you decide the agent is working.

*Z-API Omni team*
