Documentation Index
Fetch the complete documentation index at: https://mintlify.com/jzszdznzzl/WABotJS/llms.txt
Use this file to discover all available pages before exploring further.
Bot is the central class of WABotJS. It manages the full lifecycle of a WhatsApp session: authenticating with Baileys, maintaining a persistent SQLite-backed credential store, routing incoming messages to your handlers, and automatically reconnecting when the connection drops. Every WABotJS project starts by instantiating a Bot.
Constructor
Bot instance and initialises its Auth instance and all three cache stores. The socket is not created until you call login().
A unique string identifier for this bot instance. Used to differentiate between
multiple bots sharing the same process. Validated as a non-empty string.
The directory path where all SQLite files (
auth.sqlite, jid.sqlite,
message.sqlite) will be stored. Relative paths are resolved to an absolute
path via path.resolve() at construction time.Properties
id
prefix
'/'. Update it with setPrefix(). Exposed as a getter; use setPrefix() to change it.
sock
Socket instance wrapping the underlying Baileys WebSocket. Accessing this property before calling login() throws:
auth
Auth instance responsible for persisting WhatsApp session credentials in SQLite. Bot calls auth.load() automatically at the start of login() and auth.saveCreds() on every creds.update event.
cache
| Sub-cache | Type | Purpose |
|---|---|---|
jid | Stores.JID | Maps phone-number JIDs to LID JIDs, backed by jid.sqlite |
message | Stores.Message | Caches recent messages for retry look-ups, backed by message.sqlite |
metadata | TTLCache<GroupMetadata> | Caches group metadata in-memory with a 10-minute TTL |
Event Handlers
All handler-registration methods returnthis, enabling fluent chaining. Every callback must be an async function (returning a Promise<void>). Errors thrown inside a callback are caught and forwarded to onError.
Register all handlers before calling
login(). Handlers registered after
login() may miss early events such as onQR or onOTP.onError
console.error by default.
Called whenever an unhandled error occurs anywhere inside WABotJS — including
errors thrown in your other handlers, connection errors, and credential-save
failures.
onQR
str is the raw QR payload string — pass it to a QR-rendering library to display it.
Receives the raw QR string. Render it with a library such as
qrcode and
display it to the user who needs to scan with their phone.onQR fires only when login() is called without a phone number, or
when a phone number is provided but the credentials are already registered.
For OTP/pairing-code login, handle onOTP instead.onOTP
Receives the 8-character pairing code that the user must enter in WhatsApp
on their phone under Linked Devices → Link a Device → Link with phone
number instead.
onOpen
user is the Baileys Contact object for the logged-in account. It exposes
user.id (phone-number JID), user.lid (LID JID), and user.name.onClose
loggedOut, forbidden, 405, or 400. WABotJS does not reconnect after calling onClose; logout() is invoked automatically.
err is the @hapi/boom output object describing the disconnect reason,
including err.statusCode and err.payload.message.onMessage
notify (i.e. real-time messages, not history sync).
m is a fully constructed Message instance, including
resolved LID/sender, quoted message, and media metadata if present.onCommand
onMessage — both fire for command messages.
Receives four arguments:
m — the same Message passed to onMessage; prefix — the matched prefix string (e.g. '/'); name — the command name, lower-cased (e.g. 'ping'); args — the remaining whitespace-split tokens (e.g. ['hello', 'world']).Methods
setPrefix
onCommand. Takes effect immediately for all subsequent messages.
The new prefix string. Any non-empty string is valid (e.g.
'!', '.',
'//').login
Optional E.164-style phone number for OTP/pairing-code login (e.g.
'15551234567' or '+15551234567'). If provided and the session credentials
are not yet registered, WABotJS requests a pairing code from WhatsApp and
delivers it via onOTP. If credentials are already registered the phone
number is ignored and QR login proceeds normally.login() performs the following steps internally:
- Loads credentials via
auth.load() - Fetches the latest WhatsApp Web version
- Constructs a new
Socketwith the configured browser fingerprint - Binds the JID and message cache stores to the socket
- Sets up
creds.update,connection.update, andmessages.upsertevent listeners
logout
auth.drop(), and resets the login state. Subsequent calls to login() will start a fresh session.
close
login() again, and it will reuse the existing credentials.
Reconnection Behavior
WABotJS implements automatic reconnection with exponential back-off. When the socket disconnects with a non-permanent reason (anything other thanloggedOut, forbidden, 405, or 400), the bot waits and calls login() again:
- Initial retry delay: 5 seconds
- Each subsequent failure doubles the delay
- Maximum retry delay: 5 minutes
- A
restartRequireddisconnect skips the delay and reconnects immediately - On a successful reconnect the delay resets back to 5 seconds
loggedOut, forbidden, 405, and 400 — trigger onClose and a full logout() instead of reconnecting.