Overview
QR code authentication is the primary method for connecting Baileys to WhatsApp. Your phone scans the QR code displayed by Baileys, establishing a multi-device connection.
Basic QR Code Connection
The simplest way to connect with a QR code:
import makeWASocket from '@whiskeysockets/baileys'
const sock = makeWASocket ({
printQRInTerminal: true
})
When printQRInTerminal is set to true, the QR code will be displayed in your terminal for easy scanning.
Browser Configuration
You can customize the browser identity that appears in your WhatsApp’s “Linked Devices” section using the browser parameter.
Available Browser Configs
Baileys provides predefined browser configurations:
macOS
Ubuntu
Windows
Baileys
Auto-detect
import makeWASocket , { Browsers } from '@whiskeysockets/baileys'
const sock = makeWASocket ({
browser: Browsers . macOS ( 'Chrome' ),
printQRInTerminal: true
})
// Appears as: Mac OS (Chrome)
Browser Configuration Options
Browser Platform Version Browsers.ubuntu()Ubuntu 22.04.4 Browsers.macOS()Mac OS 14.4.1 Browsers.windows()Windows 10.0.22631 Browsers.baileys()Baileys 6.5.0 Browsers.appropriate()Auto-detected System release
Custom Browser Configuration
You can also provide a custom browser configuration:
const sock = makeWASocket ({
browser: [ 'My Custom OS' , 'My App Name' , '1.0.0' ],
printQRInTerminal: true
})
The browser parameter is a tuple: [Platform, AppName, Version]
Receiving Full Message History
To receive complete message history when connecting, configure these options:
import makeWASocket , { Browsers } from '@whiskeysockets/baileys'
const sock = makeWASocket ({
browser: Browsers . macOS ( 'Desktop' ),
syncFullHistory: true ,
printQRInTerminal: true
})
Set syncFullHistory
Enable syncFullHistory: true to request full chat history from WhatsApp.
Use Desktop Browser
Desktop browsers (macOS, Windows, Ubuntu) receive more message history than mobile browsers.
Handle History Events
History will be received via the messaging-history.set event after connection.
Complete Connection Example
Here’s a complete example with QR code authentication and session persistence:
import makeWASocket , {
Browsers ,
DisconnectReason ,
useMultiFileAuthState
} from '@whiskeysockets/baileys'
import { Boom } from '@hapi/boom'
async function connectToWhatsApp () {
// Load saved session
const { state , saveCreds } = await useMultiFileAuthState ( 'auth_info_baileys' )
const sock = makeWASocket ({
auth: state ,
browser: Browsers . macOS ( 'Chrome' ),
printQRInTerminal: true ,
syncFullHistory: true
})
sock . ev . on ( 'connection.update' , ( update ) => {
const { connection , lastDisconnect , qr } = update
if ( qr ) {
console . log ( 'QR Code updated, scan with your phone' )
}
if ( connection === 'close' ) {
const shouldReconnect =
( lastDisconnect ?. error as Boom )?. output ?. statusCode !==
DisconnectReason . loggedOut
console . log ( 'Connection closed:' , lastDisconnect ?. error )
if ( shouldReconnect ) {
connectToWhatsApp ()
}
} else if ( connection === 'open' ) {
console . log ( 'Connected successfully!' )
}
})
// Save credentials when updated
sock . ev . on ( 'creds.update' , saveCreds )
}
connectToWhatsApp ()
QR Code Timeout
You can configure how long to wait for QR code generation:
const sock = makeWASocket ({
qrTimeout: 60000 , // 60 seconds (in milliseconds)
printQRInTerminal: true
})
QR codes expire after a short period. If the user doesn’t scan in time, a new QR code will be generated automatically and emitted via the connection.update event.
Connection Events
Monitor the QR code and connection status:
sock . ev . on ( 'connection.update' , ( update ) => {
const { connection , qr , lastDisconnect } = update
if ( qr ) {
// New QR code available
console . log ( 'Scan this QR code:' , qr )
}
if ( connection === 'connecting' ) {
console . log ( 'Establishing connection...' )
}
if ( connection === 'open' ) {
console . log ( 'Connection opened' )
}
if ( connection === 'close' ) {
console . log ( 'Connection closed' )
}
})
Best Practices
Choose the Right Browser Config
Use desktop browser configs (macOS, windows, ubuntu) to receive more message history.
Save Authentication State
Always use useMultiFileAuthState to save sessions and avoid repeated QR scanning.
Handle Reconnections
Implement automatic reconnection logic for network failures (but not for logout).
Display QR Properly
If building a UI, extract the QR from the connection.update event rather than printing to terminal.
Next Steps
Session Management Learn how to save and restore sessions
Pairing Code Method Alternative authentication without QR codes
Handling Events Process messages and connection events
Socket Configuration Advanced socket configuration options