Overview
Pairing code authentication allows you to connect Baileys to WhatsApp without scanning a QR code. Instead, you enter a phone number and receive a pairing code to enter in the WhatsApp app.Basic Pairing Code Setup
Check Registration Status
Verify if credentials are already registered before requesting a pairing code.
Implementation
Basic Example
Interactive CLI Example
From the official example.ts:Phone Number Format
Format Examples
Phone Number Validation
Always sanitize user input:Complete Example with Error Handling
How to Use Pairing Code in WhatsApp
Checking Registration Status
Before requesting a pairing code, check if already registered:Pairing Code vs QR Code
- Pairing Code
- QR Code
Advantages:
- Works in headless environments
- No need to display graphics
- Better for CLI applications
- Can be sent via SMS/email
- Requires phone number input
- Slightly more steps for user
- Only works with WhatsApp Web protocol
Environment Detection
Automatically choose method based on environment:Best Practices
Validate Phone Numbers
Always sanitize and validate phone numbers before calling
requestPairingCode().Troubleshooting
Pairing Code Not Working
- Ensure phone number includes country code
- Remove all special characters (+, -, spaces, parentheses)
- Check that
printQRInTerminalis set tofalse - Verify you’re checking
creds.registeredstatus correctly
Connection Closes Immediately
- Make sure to save credentials with
creds.updateevent - Check internet connectivity
- Verify WhatsApp app is updated to latest version
Next Steps
QR Code Method
Use QR code authentication instead
Session Management
Learn how to save and restore sessions
Handling Events
Process messages and connection events
Socket Configuration
Advanced socket configuration options