Overview
Baileys is a free, open-source WhatsApp Web API implementation that lets you connect to WhatsApp without official business API credentials. It works by emulating the WhatsApp Web client and uses QR code scanning for authentication.Baileys connects through WhatsApp Web, making it perfect for personal use, testing, or applications that don’t require official Meta Business API credentials.
How It Works
Baileys establishes a WebSocket connection to WhatsApp servers by:- Generating a QR code or pairing code for authentication
- Maintaining a persistent session with encrypted credentials
- Synchronizing messages, contacts, and chat history
- Handling reconnections automatically
Authentication Methods
- QR Code
- Pairing Code
The default authentication method displays a QR code that you scan with your WhatsApp mobile app.
QR Code Configuration
- QRCODE_LIMIT: Maximum number of QR code regenerations before timeout (default: 30)
- QRCODE_COLOR: HEX color for the QR code dark pixels (default: ‘#175197’)
Session Configuration
Configure how your instance appears in WhatsApp’s “Linked Devices”:.env
Connection Lifecycle
1
Initialize Connection
Create an instance to start the connection process:
2
Authenticate
Scan the QR code or enter the pairing code in WhatsApp. The API monitors authentication status and emits connection events:
3
Session Persistence
Evolution API automatically saves your session to prevent re-authentication:
- Database: Prisma stores encrypted credentials
- Redis: Optional caching layer for faster reconnections
- File Provider: Alternative storage backend
4
Reconnection Handling
The API automatically reconnects on disconnection, except for:
- Logout (code 401)
- Forbidden/banned account (code 403)
- Payment required (code 402)
- Invalid session (code 406)
Connection Events
Monitor connection status through webhook events:Connection States
Features
Message Synchronization
Baileys supports full history sync when enabled:.env
Group Support
Control whether to ignore group messages:Media Handling
Baileys automatically downloads and processes media messages:Limitations vs Business API
Best Practices
1
Avoid Spam
Don’t send bulk messages or automated spam. This can get your number banned from WhatsApp.
2
Rate Limiting
Implement delays between messages to avoid detection:
3
Session Backup
Regularly backup your session data to avoid re-authentication if the instance is deleted.
4
Monitor Connection
Subscribe to
connection.update webhooks to handle disconnections gracefully.Troubleshooting
QR Code Not Appearing
Check your webhook configuration and ensure events are enabled:.env
Connection Keeps Closing
Verify your session is being saved:.env
Messages Not Syncing
Enable full history sync and check group settings:Advanced Configuration
Proxy Support
Configure proxy settings for Baileys connections:.env
Custom Baileys Version
The API automatically fetches the latest WhatsApp Web version:Logging
Control Baileys log output:.env
Next Steps
Send Messages
Learn how to send text, media, and interactive messages
Receive Messages
Set up webhooks to receive messages and events
Connection Management
Master connection lifecycle and reconnection strategies
Business API
Compare with official WhatsApp Business API