NEWLoisaX CLI 1.4 and 1-command cloud deploys now liveRead post

How to host a WhatsApp bot in Node.js, 24 hours a day

Your WhatsApp bot works on your PC, but stops whenever your computer sleeps. By the end of this guide it runs 24 hours a day on a server, paired once and reconnecting on its own. An upfront note: Baileys is an unofficial WebSocket client — Step 01 explains when the official Meta Cloud API is the better choice.

Updated Nov 18, 2026·10 min read·5 steps

What you will learn:

  • Entender as diferenças entre a API Oficial da Meta e conexão WebSocket via Baileys.
  • Desenvolver um bot de WhatsApp leve em Node.js sem depender de Chromium.
  • Vincular o número utilizando código de pareamento de 8 dígitos nos logs.
  • Preservar as chaves de sessão em disco para evitar novos pareamentos.
  • Publicar sua aplicação na LoisaX com variáveis de ambiente isoladas.
  • Manter reconexão automática 24/7 em oscilações transitórias de rede.

Download the ready WhatsApp example bot

A Baileys + Node.js 22 bot with pairing-code login, !ping command, session persistence, and automatic reconnect.

Step 01

Pick your path before writing code

The official **WhatsApp Business Cloud API** from Meta uses HTTPS webhooks, requires business verification, and bills per conversation template with zero ban risk. **Baileys** connects directly as a linked WebSocket device without Chromium overhead — lightweight for internal tools, provided you avoid bulk spam.

Step 02

Use Baileys, which needs no headless browser

Browser-based libraries launch full Chromium instances exceeding 700 MB of RAM. @whiskeysockets/baileys speaks WebSocket natively inside 128 MB–256 MB of RAM.

1{
2 "name": "meu-bot-whatsapp",
3 "version": "1.0.0",
4 "private": true,
5 "type": "module",
6 "main": "src/bot.js",
7 "scripts": {
8 "start": "node src/bot.js"
9 },
10 "engines": {
11 "node": ">=22"
12 },
13 "dependencies": {
14 "@whiskeysockets/baileys": "^6.7.16",
15 "pino": "^9.6.0"
16 }
17}

Step 03

Pair cleanly via WA_PAIRING_NUMBER

Set WA_PAIRING_NUMBER in your application variables so an 8-character pairing code prints cleanly in your live logs.

Step 04

Build the ZIP without your local session folder

Exclude data/wa-session when packaging your project so production generates a clean, conflict-free device session.

Step 05

Reconnect automatically unless explicitly logged out

Checking DisconnectReason.loggedOut ensures transient socket resets reconnect within seconds without asking for a new QR.

Step 06

Got an error? The most common ones, with the fix

Repeated 401 loggedOut disconnect loop

Cause: Session unlinked on the phone or shared simultaneously with your local laptop.

Fix: Clear data/wa-session once and pair exclusively from the server.

Step 07

Frequently asked questions

256 MB to 512 MB handles text commands and media forwarding smoothly.

Community & Education

Our commitment to developers

At LoisaX, we believe our role goes beyond providing hosting and cloud computing services. We also want to contribute to the learning and growth of our developer community.

We are continuously expanding our catalog of guides with new content on programming languages, API integrations, automation, and application development.

More deployment guides

All guides