IntegrationsIntermediate

How to Auto-Reply on WhatsApp With AI Using Twilio

Connect a Twilio WhatsApp sandbox to an Express webhook that answers messages with AI.

10 minIntermediate

Twilio's WhatsApp sandbox is the fastest way to prototype an AI auto-reply on WhatsApp without waiting for business verification. You join the sandbox from your phone, point Twilio at a webhook, and reply with TwiML. This guide gets you from zero to a working bot in about ten minutes.

What you need

  • A free Twilio account
  • A phone with WhatsApp installed
  • Node 18+ and ngrok (or any public tunnel)
  • An AI API key

Step 1: Join the WhatsApp sandbox

In the Twilio Console go to Messaging > Try it out > Send a WhatsApp message. You will see a sandbox number and a join code like join velvet-tiger. Send that exact phrase from your WhatsApp to the sandbox number to opt in.

Twilio Console - WhatsApp Sandbox
Sandbox number: +1 415 523 8886
To join: send 'join velvet-tiger' on WhatsApp
When a message comes in -> WEBHOOK URL (set this next)
Send the join phrase once to link your phone.

Step 2: Build the webhook

Twilio sends an HTTP POST to your webhook for each inbound message, with the text in the Body field. You answer by returning TwiML. Install the dependencies and write a small Express server.

zsh - whatsapp-ai
$npm install express twilio openai dotenv
added 88 packages in 5s
$
server.js
import "dotenv/config";
import express from "express";
import twilio from "twilio";
import OpenAI from "openai";

const ai = new OpenAI();
const app = express();
app.use(express.urlencoded({ extended: false }));

app.post("/whatsapp", async (req, res) => {
  const incoming = req.body.Body;

  const ai_res = await ai.chat.completions.create({
    model: "gpt-5-mini",
    messages: [{ role: "user", content: incoming }],
  });

  const twiml = new twilio.twiml.MessagingResponse();
  twiml.message(ai_res.choices[0].message.content);

  res.type("text/xml").send(twiml.toString());
});

app.listen(3000, () => console.log("Listening on :3000"));

Step 3: Expose your server and connect it

Twilio cannot reach localhost, so open a tunnel with ngrok and paste the public URL into the sandbox webhook field, with /whatsapp on the end. Set the method to HTTP POST.

zsh - tunnel
$node server.js
Listening on :3000
$ngrok http 3000
Forwarding https://a1b2c3.ngrok.io -> http://localhost:3000
Webhook URL: https://a1b2c3.ngrok.io/whatsapp
$
The sandbox is for testing only
Sandbox sessions expire and only reach numbers that joined. For a real product you must register a WhatsApp sender and get a Business Profile approved by Meta.

Step 4: Text your bot

WhatsApp - sandbox chat
You
What is a good 3 minute stretch before running?
Agent
Try leg swings, walking lunges, and ankle circles. Keep it dynamic, skip static holds before a run.
Round trip in under two seconds.

Result

Messages sent to the sandbox number now come back answered by AI. When you are ready to launch, swap the sandbox for an approved WhatsApp sender and keep the same webhook code.

Watch related tutorials

Tags
#whatsapp#twilio#webhook#ai#express