WhatsApp & Messaging 11 min read

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.

Umer Hasan

Umer Hasan

Co-Founder & CTO at Zapprex

WhatsApp Cloud API Tutorial: Send Your First Message and Receive Replies with Webhooks
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.
At a glance: What you need — A Meta developer account and a Meta…, A server that can receive HTTPS requests…, Node.js (or any language that can make…, A test phone number — Meta provides one…
At a glance: What you need

Set up the app

Set up the app: 4 steps
  1. 1

    Create an app: in Meta for Developers, create a Business app and add the WhatsApp product.

  2. 2

    Note the IDs: copy the Phone number ID and WhatsApp Business Account ID from the API setup page.

  3. 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. 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.

Send a template message (Node.js 18+, built-in fetch)javascript
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 id

Receive 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.

Express webhook: verification, signature check and replyjavascript
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.”

— Umer Hasan, CTO
  1. 1

    Add a real number: register your business phone number and set its display name.

  2. 2

    Verify your business: complete Meta business verification to raise limits.

  3. 3

    Create your own templates: utility templates for order updates, authentication templates for OTPs, marketing templates for offers.

  4. 4

    Handle statuses and errors: store message IDs and status updates; retry on temporary errors with back-off.

  5. 5

    Respect opt-in: message only customers who agreed to receive WhatsApp messages from you, and honour opt-outs.

Topics

WhatsApp Cloud APIWhatsApp API TutorialWebhooksNode.js
Umer Hasan
Written by

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.

FAQ

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.

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