Automating WhatsApp Leads with n8n and the Cloud API
WhatsApp is often where a lead will actually reply. The safe way to automate that conversation is Meta’s official WhatsApp Business Platform Cloud API, with n8n (or a similar workflow tool) sitting in the middle to parse the webhook, write the lead somewhere durable, and send a reply that stays inside Meta’s rules.
This post stays on that official path. Unofficial “WhatsApp Web” scrapers and unofficial device APIs can get a number banned and are not a production plan. The Cloud API is a normal HTTPS integration: Meta calls your webhook, you call the Graph API to send messages.
The workflow at the end of this guide does five things: receive the webhook, parse the message, upsert the lead into a CRM or a Google Sheet, send an auto-reply (a free-form message or an approved template, depending on the 24-hour window), and notify someone on the team.
What you need before a single node is added
- A Meta developer app with the WhatsApp product enabled, a WhatsApp Business Account (WABA) and a phone number (the test number is enough to learn the flow).
- The phone number ID and a permanent access token (or a system user token) with the right WhatsApp permissions. Put both in n8n credentials, not in a function node.
- n8n, either self-hosted or n8n Cloud. If you self-host, the VPS pattern in Self-Hosting Next.js on a VPS with nginx — TLS at nginx, a process manager, Cloudflare in front — is the same shape, even though the Node app is n8n rather than Next.js.
- At least one approved message template for anything you will send outside the customer service window, or for the first message you send to a lead who has not written yet.
- A place to store opt-in proof and opt-outs. A Google Sheet is enough to start; a CRM is better once more than one person is working the queue.
Pin the Graph API version in an environment variable. Meta’s current version around late 2026 is v26.0. Older versions stay usable until their expiry date, but new apps should start on a current version and keep it in one place:
WHATSAPP_GRAPH_VERSION=v26.0
WHATSAPP_PHONE_NUMBER_ID=106540352242922
# Token lives in n8n credentials, not in this file
How the webhook works
Meta sends two kinds of HTTP request to the callback URL you configure in the app dashboard (WhatsApp → Configuration).
GET is the verification handshake. Whenever you set or change the callback URL or the verify token, Meta requests:
GET /webhook?hub.mode=subscribe&hub.challenge=1158201444&hub.verify_token=your-secret
If hub.verify_token matches the string you configured, respond with HTTP 200 and the raw hub.challenge value as the body. Anything else, and Meta treats the endpoint as unverified and will not send events.
POST is the event itself. The header X-Hub-Signature-256 is sha256= followed by an HMAC-SHA256 of the raw body, keyed with your app secret. Verify that signature before you trust the payload. Then respond with 200 quickly. Meta retries on failure; a slow CRM write should happen after you have acknowledged the request, not before.
A text message from a user looks like this (structure taken from Meta’s messages webhook reference; identifiers are examples):
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "102290129340398",
"changes": [
{
"field": "messages",
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "15550783881",
"phone_number_id": "106540352242922"
},
"contacts": [
{
"profile": { "name": "Sheena Nelson" },
"wa_id": "16505551234"
}
],
"messages": [
{
"from": "16505551234",
"id": "wamid.HBgLMTY1MDM4Nzk0MzkVAgASGBQzQTRBNjU5OUFFRTAzODEwMTQ0RgA=",
"timestamp": "1749416383",
"type": "text",
"text": { "body": "Hi, I'd like a quote for an app" }
}
]
}
}
]
}
]
}
Delivery, read and send receipts arrive on the same messages field, but with a statuses array instead of messages. Your workflow must branch on that, or it will try to treat a “delivered” event as a new lead.
Subscribe the app to the messages field. One more step that catches people out: the app must also be subscribed to the WABA. n8n’s WhatsApp Trigger registers a webhook URL when you publish the workflow; it does not, by itself, subscribe the app to the business account. A test ping from the Meta dashboard can succeed while real inbound messages never arrive. In Meta’s tools, confirm the app is subscribed to the WABA you are using.
The n8n workflow, node by node
You can start from n8n’s WhatsApp Trigger (faster; it uses Meta OAuth and event checkboxes) or from a pair of generic Webhook nodes (clearer if you want to see the handshake and verify signatures yourself). The logic after the trigger is the same.
- Trigger. WhatsApp Trigger on the “Messages” event, or a POST webhook. If you use a generic webhook, add a GET route that checks
hub.verify_tokenand returnshub.challenge. - Split messages from statuses. An IF (or Switch) node: if
value.messagesexists, continue as an inbound message; ifvalue.statusesexists, log the status and stop, or update a “last delivered” column. - Parse. Extract
from(the WhatsApp number),wa_id, profile name,type, text body, messageidandtimestamp. Ignoretypesyou do not handle yet (image, audio, location, button replies), or add a branch for them. - Deduplicate. Meta can retry. Use the WhatsApp message
id(wamid…) as a unique key. If you have already processed it, exit. - Opt-out and opt-in checks. Look up the number in your sheet or CRM. If the person has opted out, do not send. If this is an outbound-first sequence (you are messaging a lead from a form, not replying to an inbound chat), also require a stored opt-in. More on that below.
- Upsert the lead. Google Sheets, HubSpot, or whatever you already use. Write the name, number, last message, last inbound timestamp and the raw message id.
- Decide the reply type. If the last inbound timestamp is within 24 hours, a free-form session message is allowed. If it is not, or if you are starting the conversation, send an approved template.
- Send. Use n8n’s WhatsApp Business Cloud node (
Message → SendorSend Template), or an HTTP Request node to the Graph API. - Notify. Email, Slack, or a Telegram message to whoever should follow up. Include a link to the sheet row or CRM record.
- Log the send (template name or text, time, Graph API message id, or the error). That log is how you debug quality-rating problems later.
A compact Code node for the parse-and-branch step, if you are using a generic webhook, looks like this:
const body = $input.first().json;
const change = body.entry?.[0]?.changes?.[0]?.value;
if (!change) return [];
if (Array.isArray(change.statuses)) {
return [{ json: { kind: 'status', statuses: change.statuses } }];
}
const messages = change.messages ?? [];
const contact = change.contacts?.[0];
return messages.map((m) => ({
json: {
kind: 'message',
from: m.from,
name: contact?.profile?.name ?? '',
waId: contact?.wa_id ?? m.from,
messageId: m.id,
timestamp: Number(m.timestamp) * 1000,
type: m.type,
text: m.type === 'text' ? m.text?.body ?? '' : '',
},
}));
To send a session message yourself (the WhatsApp node wraps this):
POST https://graph.facebook.com/v26.0/{{PHONE_NUMBER_ID}}/messages
Authorization: Bearer {{ACCESS_TOKEN}}
Content-Type: application/json
{
"messaging_product": "whatsapp",
"to": "16505551234",
"type": "text",
"text": { "body": "Thanks for getting in touch. What are you looking to build?" }
}
A first-contact or out-of-window send uses a template instead:
{
"messaging_product": "whatsapp",
"to": "16505551234",
"type": "template",
"template": {
"name": "lead_welcome",
"language": { "code": "en" },
"components": [
{
"type": "body",
"parameters": [{ "type": "text", "text": "Sheena" }]
}
]
}
}
Template names and language codes must match what WhatsApp Manager approved. The variables must also stay inside the category and format you submitted; “approved” is not “you can now put anything in the body”.
The 24-hour window, templates and opt-in
These rules are the core of the workflow, not a footnote. They come from Meta’s WhatsApp Business policies and the Cloud API messaging model. Read the current policy before you go live; this is the shape of the rules, not a substitute for them.
Customer service window. When a user messages your business number, a 24-hour window opens. Each new inbound message (or call, where that applies) resets it. Inside the window you may send free-form service messages — ordinary text, images, interactive buttons. After it closes, you may only send a pre-approved template.
Store lastInboundAt on the contact and compute Date.now() - lastInboundAt < 24 * 60 * 60 * 1000 before any free-form send. If the check fails, switch to the template branch or do not send.
You cannot start a conversation with a free-form message. The first message to a lead who has not written to you must be a template. That is why a “Facebook lead ad → WhatsApp” automation always begins with Send Template, not Send.
Opt-in is required. You may only contact people who gave you their number and permission to message them on WhatsApp. A form should say that WhatsApp is the channel, name the business, and collect an explicit checkbox — not a pre-ticked one. Store the timestamp, the wording they agreed to, and the source. If you cannot show that later, you should not have sent the message.
Honour opt-out. “STOP”, “unsubscribe”, or the same request made off WhatsApp, should set a flag that every send path checks. Remove the number from outreach lists, not just from the current workflow run.
Automated replies need a path to a human. If you auto-reply inside the 24-hour window, Meta’s policy expects a prompt way to reach a person — a handoff keyword, a phone number, or an email. Put that in the auto-reply text and actually monitor the notify step.
Quality and pricing. Meta tracks blocks and reports against the number’s quality rating and can throttle or suspend it. Conversation pricing is Meta’s and it changes, so check the official WhatsApp Business Platform pricing before you scale.
A useful IF-tree, in order, before every send:
- Is there a recorded opt-in, and no opt-out?
- Is this a retry of a message id you already sent?
- Are you inside the 24-hour window?
- If yes, send the session reply (and mention how to reach a person).
- If no, do you have an approved template that fits this situation? Send that, or stop.
A Google Sheet that is good enough to start
A workable set of columns: wa_id, phone, name, opted_in, opted_in_at, opted_out, last_inbound_at, last_outbound_at, last_message, last_wamid, last_send_status. The Sheets node can look up wa_id, update the row, or append one.
When a message body matches /^(stop|unsubscribe|cancel)$/i, set opted_out to true, send one short confirmation if the window (and policy) still allows it, and skip every later marketing or follow-up workflow that reads this sheet.
That is also the right place to record lead-form consent when the WhatsApp message was started from a form rather than from an inbound chat. The webhook for the form (or the CRM) should write opted_in before n8n sends the first template.
What usually goes wrong
- The handshake works, but inbound messages do not: the app is not subscribed to the WABA.
- Duplicate follow-ups: the workflow does not key on
wamid, and Meta’s retry looks like a new message. - Instant bans or a falling quality rating: you messaged a sheet of numbers that never opted in, or you used a marketing-like body in the wrong template category.
- Silent failures after a successful send node: you ignored
statusesand never noticedfailedwith an error code (invalid number, template rejected, window closed). - A verify-token mismatch after moving from n8n’s test URL to the production URL: update the callback URL in the Meta dashboard and complete the GET handshake again.
WhatsApp also works well for simpler, no-API flows. My free Dairy Farm Manager Android app, for example, sends each milk customer’s monthly bill to their WhatsApp with a plain wa.me link, no Business API needed. More are in the free apps section.
Automation like this is often one piece of a wider lead process — a site, a form, a CRM and a follow-up path. MakeWorking does that kind of automation work alongside web and mobile development. If you want it built for a specific pipeline, say hello.