Quickstart
This guide will walk you through creating a basic WhatsApp bot that connects to WhatsApp and responds to messages.Basic connection example
Here’s a complete working example that connects to WhatsApp and handles basic events:When you run this code for the first time, a QR code will be printed in your terminal. Scan it with WhatsApp on your phone to authenticate.
How it works
Let’s break down the key components:Authentication state
The
useMultiFileAuthState function manages your authentication credentials in a folder called auth_info_baileys. This allows you to stay logged in across restarts.Create socket connection
makeWASocket creates a connection to WhatsApp. The printQRInTerminal: true option displays the QR code in your terminal for easy scanning.Handle connection updates
The
connection.update event fires when the connection state changes. You should handle reconnection logic here.Process messages
The
messages.upsert event fires when new messages arrive. Loop through all messages in the array to handle them.Authentication methods
Baileys supports two authentication methods:QR code (default)
Scan a QR code with your phone:Pairing code
Use a pairing code instead of QR code:Sending different message types
Once connected, you can send various types of messages:Text messages
Images
Videos
Locations
Reactions
Running the example
The Baileys repository includes a comprehensive example file that demonstrates most common use cases:Important concepts
WhatsApp IDs (JIDs)
WhatsApp uses unique identifiers called JIDs for users and groups:- Individual chats:
[country code][phone number]@s.whatsapp.net(e.g.,[email protected]) - Group chats:
[group id]@g.us(e.g.,[email protected]) - Broadcast lists:
[timestamp]@broadcast - Status updates:
status@broadcast
Event handling
Baileys uses EventEmitter for all events. The main events you’ll work with:connection.update- Connection state changescreds.update- Authentication credentials updated (always save these!)messages.upsert- New messages receivedmessages.update- Message status changed (delivered, read, deleted)presence.update- User typing or online statuschats.update- Chat metadata changedgroups.update- Group settings changed
For a complete list of events, see the BaileysEventMap documentation.
Session persistence
TheuseMultiFileAuthState function saves authentication data to a local folder. This is fine for development but you should implement a database-backed solution for production:
Next steps
Event handling
Learn about all available events
Message types
Explore all message content types
Socket configuration
Configure advanced socket options
GitHub examples
View the complete example file