Cloud API or a WhatsApp Web library
There are two ways to build a WhatsApp bot, and they host very differently:
- Meta's Cloud API. You register a business phone number with Meta. WhatsApp POSTs each incoming message to your webhook, and you reply with a call to the Graph API. It's the official route, it needs no login session, and it runs as a normal web service.
- whatsapp-web.js or Baileys. These log in to a personal or business WhatsApp account as a linked device, the way WhatsApp Web does, by scanning a QR code. There's no webhook: the bot holds a connection open all the time. Neither library is made or approved by WhatsApp, and accounts that use them can be banned. See below.
Use the Cloud API for anything customers rely on. The rest of this guide starts with it.
Cloud API webhook as a web service
The app needs three routes: GET / for our health check, GET /webhook for Meta's verification request, and POST /webhook for messages. Check the X-Hub-Signature-256 header on every POST: it's an HMAC-SHA256 of the raw request body, keyed with your app's App Secret, so it proves the request came from Meta. Compute it over the raw bytes, not over re-serialized JSON.
With Express (npm install express):
// index.js
const crypto = require("node:crypto");
const express = require("express");
const app = express();
// Keep the raw body: the signature covers the exact bytes Meta sent.
app.use(express.json({ verify: (req, res, buf) => { req.rawBody = buf; } }));
app.get("/", (req, res) => res.send("ok"));
// Meta's verification request when you save the callback URL.
app.get("/webhook", (req, res) => {
if (
req.query["hub.mode"] === "subscribe" &&
req.query["hub.verify_token"] === process.env.WHATSAPP_VERIFY_TOKEN
) {
return res.send(req.query["hub.challenge"]);
}
res.sendStatus(403);
});
function validSignature(req) {
const header = req.get("X-Hub-Signature-256") || "";
const expected =
"sha256=" +
crypto.createHmac("sha256", process.env.WHATSAPP_APP_SECRET).update(req.rawBody).digest("hex");
return (
header.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(header), Buffer.from(expected))
);
}
app.post("/webhook", (req, res) => {
if (!validSignature(req)) return res.sendStatus(401);
res.sendStatus(200); // answer first, then do the work
const message = req.body.entry?.[0]?.changes?.[0]?.value?.messages?.[0];
if (message?.type === "text") {
reply(message.from, `You said: ${message.text.body}`).catch(console.error);
}
});
async function reply(to, body) {
const url = `https://graph.facebook.com/v27.0/${process.env.WHATSAPP_PHONE_NUMBER_ID}/messages`;
const res = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.WHATSAPP_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ messaging_product: "whatsapp", to, type: "text", text: { body } }),
});
if (!res.ok) console.error("send failed", res.status, await res.text());
}
app.listen(process.env.PORT || 3000);Add "start": "node index.js" to your package.json scripts. Express listens on every interface by default, and we set PORT, so nothing else is needed.
The route answers 200 before it calls the Graph API. Meta treats anything other than 200 as a failed delivery and sends it again, so a slow handler can make the bot answer the same message twice. Retries happen anyway at times, so if a duplicate reply would hurt, remember the message ids you've handled. The version in the Graph URL (v27.0) is the one Meta's send-messages docs use; Meta releases new versions regularly, so check theirs when you start.
Connect the webhook in Meta's dashboard
- Deploy the app once, so it has its URL:
https://<project-slug>.ethiodeploy.app. Meta only accepts HTTPS with a valid certificate, which that address has. - Make up a verify token (any random string) and add the env vars in the project's settings:
WHATSAPP_VERIFY_TOKEN=<the string you made up> WHATSAPP_APP_SECRET=<App settings, Basic, App Secret> WHATSAPP_TOKEN=<access token> WHATSAPP_PHONE_NUMBER_ID=<from WhatsApp, API Setup>
- In your Meta app, open WhatsApp, then Configuration. Set the callback URL to
https://<project-slug>.ethiodeploy.app/webhookand the verify token to the same string. Meta sends the verification GET when you save. - Subscribe to the
messageswebhook field. It carries incoming messages and the delivery status of the ones you send.
The temporary token on the API Setup page expires within hours. For a bot that keeps running, create a System User in Meta Business settings and give its token the whatsapp_business_messaging and whatsapp_business_management permissions. Meta's rules on what you can send still apply: you can reply freely for 24 hours after a user's last message. After that, only pre-approved template messages go through.
Sleep on the Free plan
A Free web service stops after 30 minutes without traffic. When a message arrives for a sleeping bot, we start the app and hold Meta's request for up to 30 seconds while it boots, then pass it through, so the user waits a few seconds longer for the reply. If the app isn't up in 30 seconds, Meta gets a 503 and sends the message again later. Meta keeps retrying failed deliveries, less and less often, for up to 7 days.
Don't count on webhook deliveries alone to keep a Free app awake. If the first reply after a quiet spell has to be fast, use Pro, which never sleeps.
whatsapp-web.js and Baileys
Both libraries say plainly that they aren't affiliated with WhatsApp. The whatsapp-web.js README says it is “not guaranteed you will not be blocked”, and the Baileys README asks you not to use it for spam, bulk or automated messaging. WhatsApp's Terms of Service forbid using the service “through automated means” in ways it hasn't authorized, and forbid bulk messaging and auto-messaging. The risk is that WhatsApp bans the number. Run these on a number you can afford to lose, never for bulk sends: our terms also forbid spam and bulk-messaging infrastructure.
If you still go this way, three things decide whether it works here:
- Run it as a Background Worker. The bot serves no HTTP, so set Service Type to Background Worker in Settings → Build. A worker never sleeps, and a deploy stops the old instance before it starts the new one, so two copies never share one session.
- Store the session outside the container. Each deploy starts a fresh container, and app containers have no persistent disk. A session saved to files (whatsapp-web.js's
LocalAuth, which writes.wwebjs_auth, or Baileys'useMultiFileAuthState) is gone after the next push, and you scan the QR code again. Baileys' docs sayuseMultiFileAuthStateis not for production and recommend an auth state backed by a database; a Postgres or Redis addon works for that. whatsapp-web.js hasRemoteAuthwith a MongoDB or S3 store. - Get the QR code out. Baileys hands you the QR on the
connection.updateevent (printQRInTerminalis deprecated). Print it to the logs or, simpler, callsock.requestPairingCode()with your number and type the 8-digit code into WhatsApp under Linked devices.
Baileys talks to WhatsApp over a WebSocket, with no browser, so it fits in a small container. It needs Node 20 or newer, and the latest tag on npm is currently a 7.0 release candidate, so pin the version you test with.
whatsapp-web.js drives a real Chrome through Puppeteer. Our default build image doesn't include the system libraries Chrome needs (such as libnss3 and libgbm), so deploy it from a Dockerfile that installs them, launch Chrome with --no-sandbox, and expect a browser's memory use. It won't fit in the Free plan's 256 MB; plan on Pro.
Health check
This applies to the Cloud API webhook. Every deploy requests your health check path (/ by default) and only goes live once it returns a 2xx or 3xx status. The platform keeps checking it after that. If / returns 404 or needs a login, add a route like /healthz that returns 200 and set it as the Health Check Path in Settings. Otherwise the deploy fails and the previous version keeps serving. A Background Worker has no health check.
Host a WhatsApp bot for free
The Free plan costs nothing and needs no card. It includes:
- One project, live over HTTPS at
<project-slug>.ethiodeploy.app. - 256 MB of memory and half a CPU core.
- A managed Postgres, MySQL or Redis addon with 512 MB of storage. It doesn't expire.
- A redeploy on every push, the same as the paid plans.
The catch is sleep. After 30 minutes without traffic the app stops, and the next request starts it again. We hold that request for up to 30 seconds while the app boots, so the first visitor after a quiet spell waits a little.
A Cloud API webhook bot fits the Free plan: it only uses memory while it's awake, and a sleeping bot wakes on the next message. A Baileys bot as a Background Worker never sleeps, so it counts against the Free plan's monthly quota of 50 GB-hours of memory and 20 CPU-hours. A project that stays over it for 14 days is stopped until the next month or until you upgrade.
When the app has to stay awake or needs your own domain, Pro is $10 or 1,399 ETB a month: always on, 1 GB of memory and a 5 GB database.
Deploy
- Sign in with GitHub and install the EthioDeploy app on the repo.
- Click “New Project” and pick the repo.
- Watch the build in the dashboard. When the health check passes, your app is live at
<project-slug>.ethiodeploy.app. Every push to the default branch redeploys, and the old version keeps serving until the new one is healthy.
Troubleshooting
Meta says the callback URL or verify token couldn't be validated
Cause: The GET route didn't return hub.challenge as the body, the verify token doesn't match WHATSAPP_VERIFY_TOKEN, or the path is wrong.
Fix: Return req.query["hub.challenge"] as plain text when the token matches, and check that the path matches the callback URL.
Every POST gets 401 from your own app
Cause: The signature is computed over parsed and re-serialized JSON, or WHATSAPP_APP_SECRET holds the access token instead of the App Secret.
Fix: HMAC the raw body (the verify hook above) with the App Secret from App settings, Basic.
The bot answers the same message twice
Cause: Meta retried a delivery that didn't get a 200 in time, or retried for another reason.
Fix: Send the 200 before doing slow work, and skip message ids you've already handled.
Replies fail with an OAuth error after a day
Cause: The temporary access token from API Setup expired.
Fix: Use a System User token with whatsapp_business_messaging permission.
Deploy fails: GET / returned 404
Cause: The app only has the /webhook routes, so the health check never passes.
Fix: Add a GET / route that returns 200.
A whatsapp-web.js or Baileys bot asks for a new QR scan after every deploy
Cause: The session was saved to files in the container, which is replaced on every deploy.
Fix: Store the session in a database (Baileys) or use RemoteAuth (whatsapp-web.js).
Still stuck? Check the deployment's build log in the dashboard, or see Troubleshooting.
Questions
Yes, a Cloud API webhook bot runs on the Free plan. It sleeps after 30 minutes without traffic and wakes on the next message, so the first reply after a quiet spell takes a few seconds longer. Pro ($10/month) keeps it always on.
Not for anything you can't afford to lose. Neither library is made or approved by WhatsApp, WhatsApp's terms forbid unauthorized automated access and bulk messaging, and accounts that use them can be banned. Meta's Cloud API is the supported way to run a bot.
useMultiFileAuthState saves the session to files, and every deploy starts a new container without them. Store the auth state in a database such as a Postgres or Redis addon instead.
https://<project-slug>.ethiodeploy.app plus your webhook path, for example /webhook. Meta requires HTTPS with a valid certificate, which every project gets. Your app itself listens on the PORT env var.
Ready to deploy?
Sign in with GitHub and pick your repo. No Dockerfile, no YAML.
More guides
All guides on the deploy guides page. Custom domains, env vars, addons and billing are in the docs.