How to Connect AI to WhatsApp With the Meta Cloud API
Set up the official WhatsApp Cloud API, verify a webhook, and reply with AI through the Graph API.
The WhatsApp Cloud API is Meta's official, free-to-start path with no middleman. It is more setup than Twilio but it is what you ship to production. This guide covers the test number, webhook verification, and sending an AI reply back through the Graph API.
What you need
- A Meta developer account and a Business app at developers.facebook.com
- The WhatsApp product added to that app
- A public HTTPS endpoint (ngrok works for testing)
- An AI API key and Node 18+
Step 1: Grab the test credentials
In the app dashboard open WhatsApp > API Setup. Meta gives you a temporary access token, a test phone number id, and a field to add recipient numbers. Add your own number and send the sample template to confirm delivery works.
Step 2: Verify the webhook
Meta verifies your webhook with a GET request carrying hub.challenge and a verify token you choose. Echo the challenge back when the token matches. Inbound messages then arrive as POST requests.
import "dotenv/config";
import express from "express";
import OpenAI from "openai";
const ai = new OpenAI();
const app = express();
app.use(express.json());
const VERIFY = process.env.VERIFY_TOKEN;
app.get("/webhook", (req, res) => {
if (
req.query["hub.mode"] === "subscribe" &&
req.query["hub.verify_token"] === VERIFY
) {
return res.send(req.query["hub.challenge"]);
}
res.sendStatus(403);
});
app.listen(3000, () => console.log("up on :3000"));Step 3: Handle inbound and reply
Inbound messages are buried in entry[0].changes[0].value.messages. Pull the text and the sender, run the model, then POST to the Graph API messages endpoint to reply.
app.post("/webhook", async (req, res) => {
res.sendStatus(200); // ack fast
const value = req.body.entry?.[0]?.changes?.[0]?.value;
const msg = value?.messages?.[0];
if (!msg) return;
const out = await ai.chat.completions.create({
model: "gpt-5-mini",
messages: [{ role: "user", content: msg.text.body }],
});
await fetch(
`https://graph.facebook.com/v20.0/${process.env.PHONE_ID}/messages`,
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.WA_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
messaging_product: "whatsapp",
to: msg.from,
text: { body: out.choices[0].message.content },
}),
},
);
});Step 4: Point Meta at your tunnel
Result
Messages to your test number now return AI answers through Meta's official pipeline. Replace the temporary token with a permanent System User token and register your own number to move beyond testing.
Watch related tutorials
5:42
24:16
33:42
41:18
28:05
3:12