WhatsApp AI agent in n8n, with the reply written by your workflow
An automation step POSTs the customer's message, signed and with up to 10 recent messages of the chat, to an n8n Webhook node. An AI Agent node writes the answer, a Respond to Webhook node returns {"reply": "..."} within 20 seconds, and that text is sent from the WhatsApp Business number, which keeps working in the app. Slower agents receive Meta's webhook directly and reply through the Graph API.
easyCoexistence connects the WhatsApp Business number you already use in the app to Meta's Cloud API through Coexistence and runs automations on it.
How long the Call my webhook step waits for your workflow's answer before the step fails.
Most recent messages of the chat sent in history, oldest first, ending with the message that started the run.
Characters of a reply sent as one WhatsApp text; anything longer is cut.
Shortest gap between two failure emails for the same automation when a webhook step fails.
What n8n Cloud allows in a Code node, crypto and moment; self-hosted n8n enables crypto with NODE_FUNCTION_ALLOW_BUILTIN.
Customer service window after the customer's last message; after it, Meta accepts only template messages.
How does a WhatsApp message reach an n8n AI agent?
Through an automation on the number. The number has to receive its messages through easyCoexistence (the "Through easyCoexistence" mode), so Meta delivers them here; an automation whose trigger is A message arrives and whose step is Call my webhook then POSTs the message to your n8n Production URL. The workflow answers in the same HTTP response, and the text it returns goes to the customer as a WhatsApp message from your number.
Before n8n is called, the run has already passed the guards. A delivery Meta repeats never starts a second run, a reply you typed in the app pauses the automation on that chat (4 hours by default), and the reply only goes out within 24 hours of the customer's last message. Your workflow only decides what to say.
What does easyCoexistence send to the n8n Webhook node?
A JSON POST with User-Agent easyCoexistence-automations/1, X-EC-Event set to automation.step and an X-EC-Signature header. message holds the customer's text, or a placeholder such as [image] with the caption for media; a voice note arrives as [voice message] followed by its words when an OpenAI or OpenRouter key is saved. history holds up to 10 recent messages of the chat, oldest first, each with a direction: in for the customer, out for an automation's reply and echo for a person writing from the phone. It covers the messages kept for the account's retention period (30, 90 or 365 days, 90 by default), and only those that arrived while the number's messages came through easyCoexistence.
A test from the editor's Try it panel sends the same POST with dry_run set to true, an empty history, the sample contact 15550000000 named Test customer and a message id starting with test_. Nothing is sent to anyone, but your workflow really runs, so skip side effects such as creating orders when dry_run is true.
{
"event": "automation.step",
"dry_run": false,
"automation": { "id": "b7e0c3a2-5d1f-4c8e-9a6b-2f4d8e1c7a90", "name": "Send to my system" },
"run_id": "0d9f6a41-8c2e-4b7a-a315-6e9c2b8d4f17",
"channel": {
"type": "whatsapp",
"connection_id": "3f2a9c1e-7b4d-4e8a-9c2f-1a2b3c4d5e6f",
"phone": "+1 631-555-0142",
"phone_number_id": "106540352242922"
},
"connection": { "id": "3f2a9c1e-7b4d-4e8a-9c2f-1a2b3c4d5e6f", "phone": "+1 631-555-0142", "phone_number_id": "106540352242922" },
"contact": { "id": "5511987654321", "wa_id": "5511987654321", "name": "Ana Souza" },
"message": {
"id": "wamid.HBgNNTUxMTk4NzY1NDMyMRUCABIYFjNFQjBDNzE4RjQ2QjI5RDkxQTNFAA==",
"type": "text",
"text": "Do you have gluten-free bread today?",
"at": "2026-11-11T14:02:11.000Z"
},
"history": [
{ "direction": "in", "text": "Hi! Are you open today?", "at": "2026-11-11T13:58:40.000Z" },
{ "direction": "out", "text": "Hi Ana! Yes, until 8 pm.", "at": "2026-11-11T13:58:45.000Z" },
{ "direction": "in", "text": "Do you have gluten-free bread today?", "at": "2026-11-11T14:02:11.000Z" }
]
}How do you check the signature in n8n?
Add a Code node right after the Webhook node. X-EC-Signature reads t=<unix seconds>,v1=<hex>, where v1 is an HMAC-SHA256 of t, a dot and the body, keyed with the automation's Signing secret. To copy it, open the saved automation and select its Call my webhook step. Each automation has its own secret, and MCP tools never return it, so an automation your AI created still needs this one copy from the dashboard. A New secret button replaces it and the old one stops working at once, and only the account owner can see it, not an invited team member.
Reject a t more than 5 minutes from your clock, compare in constant time, and throw on failure: the workflow then errors before Respond to Webhook, n8n answers 500 and nothing is sent. n8n Cloud allows the crypto module in Code nodes; self-hosted n8n needs NODE_FUNCTION_ALLOW_BUILTIN=crypto. The Crypto node's Hmac action with an IF node also works, though an IF comparison is not constant-time.
// n8n Code node, mode "Run Once for Each Item", placed right after the Webhook node.
// n8n Cloud allows require('crypto'); self-hosted needs NODE_FUNCTION_ALLOW_BUILTIN=crypto.
const crypto = require('crypto');
const SECRET = 'whsec_...'; // the automation's Signing secret, from its editor page
const m = /^t=(\d+),v1=([0-9a-f]{64})$/.exec($json.headers['x-ec-signature'] || '');
if (!m) throw new Error('Missing X-EC-Signature');
if (Math.abs(Date.now() / 1000 - Number(m[1])) > 300) throw new Error('Stale signature');
// The body was signed as compact JSON, which JSON.stringify rebuilds from the parsed body.
const mac = crypto.createHmac('sha256', SECRET).update(`${m[1]}.${JSON.stringify($json.body)}`).digest();
if (!crypto.timingSafeEqual(mac, Buffer.from(m[2], 'hex'))) throw new Error('Bad signature');
return { json: $json.body }; // event, dry_run, contact, message, history...How do you set up the AI Agent node and the reply?
Give the AI Agent node the customer's text as its user message and everything else in its system message: who the business is, the facts it may quote, the tone, and the recent chat from history so a follow-up question makes sense. Attach the chat model you want, plus tools such as an HTTP Request tool that looks up an order.
End with a Respond to Webhook node that responds with JSON and puts the agent's text in a field named reply. easyCoexistence trims it, ignores an empty or non-string reply, and sends the rest as one WhatsApp text of up to 4,096 characters, so ask the model for plain sentences rather than markdown.
AI Agent node
Prompt: Define below
Prompt (User Message): {{ $json.message.text }}
Options > System Message:
You answer WhatsApp messages for Bloom Café, 12 Baker St, open Tue to Sun 8:00 to 20:00.
Reply in 1 to 3 short sentences, in the customer's language, as plain text.
Never invent prices or stock. If unsure, say a person will reply here soon.
Recent chat, oldest first:
{{ $json.history.map(h => (h.direction === 'in' ? 'Customer: ' : 'Business: ') + h.text).join('\n') }}
Chat Model: your OpenAI, Anthropic or OpenRouter model
Tools (optional): an HTTP Request tool that looks up orders
Respond to Webhook node
Respond With: JSON
Response Body: { "reply": {{ $json.output.toJsonString() }} }What happens when the workflow is slow, fails or returns nothing?
Every outcome is logged in the automation's Runs tab with the step's output or error, so you can see which of these happened:
- No answer within 20 seconds: the step fails with Webhook did not answer, nothing is sent and the run shows Failed.
- A status outside 2xx, redirects included: the step fails with Webhook answered HTTP and the code.
- A 2xx without a reply string: the run is Done, the step shows HTTP 200, no reply, and the customer gets nothing automatic.
- A failed run emails you so you can answer that customer yourself, at most once per automation every 6 hours.
- A chat you answered from the phone within the pause (4 hours by default, set under Advanced) is skipped as
human_activebefore n8n is called. - Several quick messages start several runs. A 1-minute Wait before Call my webhook answers once, because a newer message that picks the same automation supersedes the waiting run.
When should n8n receive Meta's webhook directly instead?
When the agent needs more than 20 seconds, when the workflow must see every event (statuses, your phone replies, media ids), when it sends something no step covers, such as a Flow or a catalog message, or when the business wants the 6-month history import, which only goes to a direct destination saved within 24 hours of connecting. Direct mode turns automations off for the number: Meta posts its own payload straight to n8n and easyCoexistence never sees the messages.
In n8n, use the plain Webhook node, not the WhatsApp Trigger node, which expects your own Meta app and its secret. Turn on Allow Multiple HTTP Methods so one long random path takes Meta's GET handshake and the POSTs, answer the GET with the raw hub.challenge as text, and save that Production URL with a verify token through set_webhook_destination. Replies then go out from an HTTP Request node to the send endpoint, and dedupe, the phone pause and the 24-hour check become yours.
| Call my webhook (Automations on) | Direct webhook (automations off) | |
|---|---|---|
| Who calls n8n | easyCoexistence, when the automation matches | Meta, for every event on the number |
| Time to answer | 20 seconds, reply in the response | No limit: send later through the Graph API |
| Authentication | X-EC-Signature with the automation's secret | A long random secret in the URL path |
| Guards | Dedupe, phone pause and 24-hour check built in | Yours to write |
| What n8n can send | Text in the reply; the other steps of the automation can send buttons, files and templates | Any Cloud API message type, templates included |
| History import | Not available | If saved within 24 hours of connecting |
| Kept by easyCoexistence | Message text for your retention period (90 days by default), run logs 30 days | Nothing |
How to set it up
- Turn on automations for the numberOn the number's page, the Automations and incoming messages card shows Automations on or Direct to your server. Click Turn on automations if needed; numbers connected without a webhook URL start with automations on.
- Build and publish the workflowWebhook node (POST, Respond: Using 'Respond to Webhook' Node), Code node, AI Agent node, Respond to Webhook node. Publish or activate it and copy the Production URL.
- Create the automation, switched OffIn Automations, click New automation and pick Send to my system under Start from an example. Paste the Production URL into Webhook URL, set the switch to Off so it does not answer customers yet, and click Save.
- Copy the Signing secret into n8nSelect the Call my webhook step of the saved automation, copy Signing secret and paste it into the Code node.
- Test, then switch it OnClick Test, type a sample message in Try it and press Run test: n8n runs with dry_run true and the result shows the reply. When it reads right, switch the automation On and save.
Common mistakes
- Leaving the Webhook node's Respond option on Immediately. n8n answers Workflow got started at once, the run logs HTTP 200, no reply, and the customer gets nothing. Choose Using 'Respond to Webhook' Node.
- Pasting the Test URL, the one with /webhook-test/. It only listens while the editor waits for a test event; automations need the Production URL of a published workflow.
- Verifying with the number's forward secret. Call my webhook is signed with the automation's own Signing secret; the forward secret signs forwarded events only, so genuine steps fail the check.
- Returning the text in another shape. Only a JSON body with a top-level string named reply is sent; {"output": "..."} or a text/plain body is logged as no reply.
Its Call my webhook step hands the customer's message to n8n and sends back the reply, with dedupe, the pause when you answer from the phone and the 24-hour check built in, at no extra charge per run. US$ 9 down to US$ 0.75 per number per month, falling as you add numbers, with the first 3 days free.
easyCoexistence is a multichannel API and MCP server that connects AI agents such as Claude and ChatGPT to WhatsApp, Instagram, TikTok, Facebook and more. On WhatsApp it uses Coexistence: the business keeps using its number in the WhatsApp Business app while the AI works through Meta's official API.
Frequently asked questions
Can ChatGPT or Claude create this automation for me?
Yes. With the easyCoexistence connector added, ask for an automation on your number that sends incoming messages to your n8n URL; the AI calls create_automation with a call_webhook step, then test_automation. Ask it to create the automation disabled until the test passes. The Signing secret is not returned through MCP, so copy it from the automation's page.
Can the workflow send images, buttons or templates?
Not through the reply field, which carries text only. Let the automation do it instead: steps such as Send a picture or file, Ask a question with buttons and Send a template can sit before or after Call my webhook. For other message types, send them from an HTTP Request node to the Cloud API, with the send endpoint and token that get_api_credentials returns, and return no reply so the customer is not answered twice.
Can one workflow answer several numbers?
Yes. Point one automation per number at the same Production URL and branch on channel.phone_number_id or automation.id. Each automation has its own Signing secret, so pick the secret by automation.id before checking the signature.
Does a self-hosted n8n work?
Yes, if its Production URL is public HTTPS. easyCoexistence refuses http, localhost, private and internal addresses when you save the URL and again before every call, and it does not follow redirects.
Keep reading
Ready to get started?
Set up WhatsApp Coexistence in minutes, not months. The app keeps working on the phone.
Start Free TrialVerified on