This guide walks you through creating a minimal Baileys application that connects to WhatsApp, persists your session, listens for incoming messages, and replies to them. By the end you will have a working bot you can extend with your own logic.Documentation Index
Fetch the complete documentation index at: https://whiskeysockets-docs-jids-socket-config-ptbr.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Baileys is an unofficial library and is not affiliated with WhatsApp. Use it responsibly and in accordance with WhatsApp’s Terms of Service. The maintainers do not condone bulk messaging, spam, or stalkerware.
Complete example
The steps below build up to this complete working script. You can use it as a starting point for your own project.Step-by-step walkthrough
Install Baileys
Add Baileys and its Make sure you are running Node.js 20.0.0 or later. Installation will fail otherwise.
@hapi/boom peer to your project. @hapi/boom is a direct dependency of Baileys and is used to inspect disconnect error codes.Set up auth state
Baileys requires an auth state object to manage your session credentials and Signal Protocol keys. The built-in
useMultiFileAuthState utility saves everything to a local folder.state holds the current credentials and keys. saveCreds is a callback you will pass to the creds.update event so the session is written to disk whenever it changes.Create the socket
Create a socket by calling
makeWASocket with your auth state. Listen for the qr field on connection.update and render it yourself — printQRInTerminal is deprecated.When you run this for the first time, a QR code appears in your terminal. Open WhatsApp on your phone, go to Settings → Linked devices → Link a device, and scan the code. On subsequent runs, the saved credentials are reused and no QR code is shown.
Handle connection updates
Listen to
connection.update to react when the connection opens, closes, or encounters an error. The reconnect logic below restarts the socket automatically — unless you were explicitly logged out.DisconnectReason.loggedOut means your session was revoked (for example, you removed the linked device in the WhatsApp app). In that case, delete the auth folder and re-scan a QR code.Listen for incoming messages
The
messages.upsert event fires whenever new messages arrive. Iterate over event.messages to handle each one.m.key.remoteJid is the WhatsApp ID (JID) of the chat the message came from — a phone number for individual chats, or a group ID for group chats. Pass it as the first argument to sock.sendMessage to reply.Save credentials on update
Register the If you skip this step, your session will not be saved and you will need to scan the QR code again on every restart.
saveCreds callback on the creds.update event. Baileys calls this whenever your session credentials change — for example, after the initial QR scan or after Signal session keys rotate.What’s next
Now that you have a working connection, explore the rest of the documentation to learn what Baileys can do:Authentication
Use a pairing code instead of a QR code, or learn how to manage sessions in a database.
Sending messages
Send text, images, video, audio, polls, reactions, and more.
Events
See the full list of events Baileys emits and what data each one carries.
Groups
Create and manage groups, handle join requests, and configure ephemeral messages.