DEV Community

APIVAI
APIVAI

Posted on • Originally published at apivai.com

Build a WhatsApp AI Customer Service Bot with a Cheap OpenAI-Compatible API

Build a WhatsApp AI customer-service bot

You can run an AI customer-service agent on WhatsApp by wiring three pieces together: the WhatsApp
Cloud API (to receive and send messages), a small webhook server (your glue), and an
OpenAI-compatible LLM for the replies. APIVAI provides the LLM step — GPT-5.5 is a great fit for
natural, multilingual support — at a fraction of official price, which matters when you're paying
per customer message.

This guide is the architecture plus the core code.

Architecture

Customer on WhatsApp
   │
   ▼
WhatsApp Cloud API (Meta)  ──webhook──▶  Your server
                                            │  (build prompt + history)
                                            ▼
                                   APIVAI  /v1/chat/completions  (GPT-5.5)
                                            │  reply text
                                            ▼
WhatsApp Cloud API  ◀──send message──  Your server
Enter fullscreen mode Exit fullscreen mode

1. Get WhatsApp Cloud API access

Create a Meta app, add the WhatsApp product, and get: a phone number ID, a permanent access
token, and a webhook verify token. Point the webhook at your server's /webhook URL.

2. Webhook server (Node.js)

import express from "express";
import OpenAI from "openai";

const app = express();
app.use(express.json());

const ai = new OpenAI({ apiKey: process.env.APIVAI_API_KEY, baseURL: "https://api.apivai.com/v1" });
const SYSTEM = "You are a friendly customer-service agent for our store. Answer concisely in the customer's language. If unsure, offer to connect a human.";

// Meta webhook verification
app.get("/webhook", (req, res) => {
  if (req.query["hub.verify_token"] === process.env.VERIFY_TOKEN) return res.send(req.query["hub.challenge"]);
  res.sendStatus(403);
});

// Incoming messages
app.post("/webhook", async (req, res) => {
  res.sendStatus(200); // ack fast
  const msg = req.body?.entry?.[0]?.changes?.[0]?.value?.messages?.[0];
  if (!msg?.text) return;

  const reply = await ai.chat.completions.create({
    model: "gpt-5.5",
    messages: [{ role: "system", content: SYSTEM }, { role: "user", content: msg.text.body }],
    max_tokens: 300,
  });

  await sendWhatsApp(msg.from, reply.choices[0].message.content);
});

async function sendWhatsApp(to, text) {
  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, text: { body: text } }),
  });
}

app.listen(8080);
Enter fullscreen mode Exit fullscreen mode

3. Add memory and product knowledge

  • Conversation memory: store the last few messages per customer (by phone number) and pass them in the messages array so replies stay in context.
  • Product knowledge: prepend key facts (hours, shipping, return policy, catalog highlights) to the system prompt, or retrieve relevant snippets from your docs and inject them (RAG).
  • Human handoff: detect low-confidence or escalation phrases and route to a human inbox.

4. Pick the model

GPT-5.5 is the recommended default for support — natural tone, strong multilingual handling, low
latency, and cheap per message through APIVAI. For very high volume, route simple FAQs to a
smaller model and reserve GPT-5.5 for nuanced questions.

Cost note

Support bots send many short messages. APIVAI's OpenAI-compatible pricing at a fraction of list,
pay-as-you-go, keeps the per-conversation cost low — and you can pay with crypto/USDT/Alipay.

FAQ

Does this work for Facebook/Instagram DMs too? Yes — same pattern with the Messenger Platform
webhook; only the send/receive API differs. The APIVAI call is identical.

Which model for customer service? GPT-5.5 for natural multilingual replies; a smaller model for
bulk/simple FAQs.

Do I need to change code to switch models? No — change the model string; the endpoint is
OpenAI-compatible.

Get started

Get an APIVAI key, drop it into the webhook above, and connect the WhatsApp Cloud API. Examples:
APIVAI examples repo.

Top comments (0)