WhatsApp Cloud API Tutorial: Send Your First Message and Receive Replies with Webhooks
A developer tutorial for Meta’s WhatsApp Cloud API: create the app, get a token and phone number ID, send a template message, set up and verify webhooks, reply to customers and go to production safely.

Table of contents
What you need
- A Meta developer account and a Meta Business portfolio.
- A server that can receive HTTPS requests (for webhooks).
- Node.js (or any language that can make HTTP requests).
- A test phone number — Meta provides one for development.

Set up the app

- 1
Create an app: in Meta for Developers, create a Business app and add the WhatsApp product.
- 2
Note the IDs: copy the Phone number ID and WhatsApp Business Account ID from the API setup page.
- 3
Get a token: use the temporary token for testing; for production create a System User in Business settings and generate a permanent token with WhatsApp permissions.
- 4
Add test recipients: add your own phone number as a recipient while in development.
Tip: Store the access token and app secret in environment variables or a secrets manager — never in front-end code or a public repository.
Send a template message
Business-initiated messages must use an approved template. The default “hello_world” template is available for testing.
const res = await fetch(
"https://graph.facebook.com/v21.0/" + process.env.WA_PHONE_NUMBER_ID + "/messages",
{
method: "POST",
headers: {
Authorization: "Bearer " + process.env.WA_TOKEN,
"Content-Type": "application/json",
},
body: JSON.stringify({
messaging_product: "whatsapp",
to: "919800000000", // country code + number, no +
type: "template",
template: { name: "hello_world", language: { code: "en_US" } },
}),
}
)
console.log(await res.json()) // contains the message idReceive messages with webhooks
When a customer replies, Meta sends a POST request to your webhook URL. First, Meta verifies your URL with a GET request containing a challenge you must echo back. Then every incoming message and status update (sent, delivered, read) arrives as JSON.
import crypto from "crypto"
app.get("/webhook", (req, res) => {
const ok = req.query["hub.mode"] === "subscribe" &&
req.query["hub.verify_token"] === process.env.WA_VERIFY_TOKEN
ok ? res.send(req.query["hub.challenge"]) : res.sendStatus(403)
})
app.post("/webhook", express.raw({ type: "application/json" }), async (req, res) => {
const sig = "sha256=" + crypto.createHmac("sha256", process.env.META_APP_SECRET)
.update(req.body).digest("hex")
if (sig !== req.get("x-hub-signature-256")) return res.sendStatus(401)
res.sendStatus(200) // acknowledge fast
const value = JSON.parse(req.body).entry?.[0]?.changes?.[0]?.value
const msg = value?.messages?.[0]
if (msg?.type === "text") await sendText(msg.from, "Thanks! We got: " + msg.text.body)
})Going to production
“Acknowledge webhooks immediately and do the work in a queue. Slow webhook responses cause retries and duplicate replies.”
- 1
Add a real number: register your business phone number and set its display name.
- 2
Verify your business: complete Meta business verification to raise limits.
- 3
Create your own templates: utility templates for order updates, authentication templates for OTPs, marketing templates for offers.
- 4
Handle statuses and errors: store message IDs and status updates; retry on temporary errors with back-off.
- 5
Respect opt-in: message only customers who agreed to receive WhatsApp messages from you, and honour opt-outs.
Topics
Umer Hasan
Co-Founder & CTO at Zapprex
Leads engineering, cloud infrastructure and AI automation at Zapprex Technologies, with 7+ years of full-stack and mobile experience.
Keep reading
All articlesWhatsApp Business API Explained: How to Get Access, Pricing Basics and What You Can Build
A beginner-friendly explanation of the WhatsApp Business Platform: Cloud API vs Business Solution Providers, verification, phone numbers, message templates, pricing basics, limits and the most useful use cases.
WhatsApp Message Templates: Categories, Approval Tips and 15 Examples That Get Approved
Learn the three WhatsApp template categories, how variables, buttons and media headers work, why templates get rejected, and copy-ready examples for orders, reminders, OTPs and offers.
WhatsApp Automation for Small Businesses: 12 Workflows That Save Hours Every Week
Simple WhatsApp automations any small business can use — auto-replies, lead capture, appointment reminders, payment links, review requests and more — with what each needs and how to set it up.
Questions readers ask
Outside the 24-hour customer service window, WhatsApp only allows approved template messages. Once the customer messages you, you can send free-form replies within 24 hours.
Create a System User in Meta Business settings, assign it to your app and WhatsApp account, and generate a token with the WhatsApp messaging and management permissions.
Use a current supported version from Meta’s changelog and upgrade before older versions are retired. The version is part of the URL.
Have a project in mind?
Let's build something that moves your business forward.
Share your idea, process or problem — we will come back with a clear plan, timeline and cost range. No technical spec needed.
- Founder reply within 1 business day
- NDA on request · 100% code ownership
- Talk directly to the engineers
