Blog · · 5 min read
How to Build a WhatsApp AI Agent with n8n (Free, Step by Step)
A practical guide to building a WhatsApp AI chatbot in n8n with the WhatsApp Cloud API and OpenAI: per-customer memory, loop protection and human handoff. Free template included.

For many small businesses, WhatsApp is where customers actually get in touch: opening hours, prices, "can I book for tomorrow?", "where do I park?". The same questions arrive again and again, often after hours. A WhatsApp AI agent can answer them instantly from your own business information, and hand the conversation to a human when it should.
This guide shows you how to build one with n8n, a visual automation tool you can use in the cloud or self-host. No coding is required, but take the steps slowly. If you'd rather not start from scratch, there's a free ready-to-import template at the end.
What you'll build
The finished workflow does four things:
- Receives messages sent to your WhatsApp Business number.
- Filters out what it shouldn't answer: delivered/read receipts, messages sent by your own number and, in a first version, voice notes or images.
- Hands off to a person when the customer asks (for example by typing HUMAN).
- Replies with AI using only your business information, remembering each customer's recent conversation.
What you need first
- n8n reachable from the internet over HTTPS. n8n Cloud or a self-hosted instance on a server. If it only runs on your laptop, WhatsApp can't deliver messages to it.
- A Meta for Developers account with a Business-type app and the WhatsApp product added. Meta gives you a test number so you can experiment before connecting your real one.
- An OpenAI API key with credit. Short replies are cheap, but set a monthly spending limit in your OpenAI account on day one.
- Your business information in writing: hours, services, prices, cancellation policy, address and your most frequent questions.
Allow one to two hours the first time, mostly for the Meta setup.
Step 1: set up the WhatsApp app in Meta
In Meta for Developers, create an app, add WhatsApp and open API Setup. Note three values:
- The Phone number ID that will send replies.
- The WhatsApp Business Account ID.
- An access token. The temporary one expires after 24 hours: fine for testing, but for real use create a permanent System User token in your Business Manager settings.
Add your personal phone as a test recipient so you can message the bot while you build.
Step 2: the WhatsApp trigger in n8n
Create a new workflow in n8n and add the WhatsApp Trigger node subscribed to message events. Its credential needs your app's App ID and App Secret (Meta → App settings → Basic). When you activate the workflow, n8n registers the webhook with Meta.
One important detail: WhatsApp doesn't only notify you about new messages. It also sends status updates (sent, delivered, read). If you don't filter them, your agent will try to reply to them, and in the worst case you get a loop that burns credits.
Step 3: the loop-protection filter
Right after the trigger, add a Filter or IF node that only lets an event through if:
- It contains a message, not just a status.
- The message type is text.
- The sender is not your own business number.
It's the least exciting node in the workflow and the one that prevents the most trouble. If you later want to handle voice notes, add them as a separate branch with transcription rather than loosening this filter.
Step 4: one customer at a time
When WhatsApp batches several messages into a single delivery, n8n processes them together. If two customers write at the same moment, their conversations can get mixed up. Avoid this with a Loop Over Items node (formerly Split in Batches) set to a batch size of 1, so each message is handled separately with its own memory.
Step 5: human handoff
Before calling the AI, add an IF node that checks whether the text contains your chosen keyword (for example HUMAN). If it does:
- Reply with a fixed message: "Sure, someone from the team will get back to you shortly."
- Alert your team through whatever you use: an email, a message to your phone or a task.
That way a customer is never stuck talking to a bot when they want a person.
Step 6: the AI agent
Add the AI Agent node and connect:
- An OpenAI chat model. For customer service a small, inexpensive model is usually enough, so start there.
- A window memory (Simple Memory) with the session key set to the customer's phone number and about 10 messages of context. It remembers what that customer said without mixing them up with anyone else.
The biggest quality lever is the system prompt. A good WhatsApp prompt includes:
- Who the assistant is, which business it represents, and that it must disclose it's an automated assistant.
- Your business information, pasted verbatim.
- Clear rules: "If you don't know, don't guess; offer a human." "Keep replies to two or three sentences." "Never quote prices that aren't on this list."
- Tone: formal or friendly.
- What to do with sensitive topics: complaints, personal data, emergencies.
Step 7: reply on WhatsApp
Finish with the WhatsApp → Send Message node, using your Phone number ID as sender and the customer's number as recipient. The message text is the agent's output.
Remember WhatsApp's 24-hour rule: you can send free-form replies within 24 hours of the customer's last message. Outside that window Meta only allows approved templates. For an agent that replies instantly, this is rarely an issue.
Step 8: test before going live
Write down 15–20 real questions your customers ask and send them one by one from your phone. Check that the agent:
- Never invents prices or hours.
- Offers a human when it doesn't know.
- Responds correctly to the handoff keyword.
- Never replies to its own messages or to read receipts.
Use n8n's Executions view to see exactly what happened with each message. When you're happy, connect your real WhatsApp Business number.
Common mistakes
- Using the temporary token in production. It works for a day, then silently stops.
- n8n without HTTPS or behind your home router. Meta needs a public, secure URL.
- Open-ended prompts. Without limits, the AI tries to be helpful by making things up.
- No spending cap on OpenAI. One setting, and it saves you surprises.
Is it allowed?
Using the official WhatsApp Business Platform to serve your customers is exactly what Meta intends. Best practice is to say it's an automated assistant, always offer a way to reach a person, and mention in your privacy policy that messages are processed by an AI provider. If you handle sensitive data, get professional advice for your jurisdiction.
Shortcut: the free template
We've packaged this exact workflow as an n8n template: 11 nodes with the loop filter, one-customer-at-a-time processing, per-phone-number memory and human handoff, plus a fill-in-the-blanks system prompt and a PDF guide. The logic has been tested in n8n with simulated messages; run your own test with WhatsApp before using it with customers.
👉 Download the free WhatsApp agent
Want the production version, with persistent database memory and a real bot pause during handoff, plus seven more agents? See the AgentKiosk pack.
Curious what else you can automate? Read 7 AI automations for small businesses.


