Connection & Pairing

Two ways in: scan a QR code, or use an 8-character pairing code โ€” both fully supported.

Option 1 โ€” QR in the terminal

const sock = makeWASocket({
  auth: state,
  printQRInTerminal: true
})

Scan it from WhatsApp on the phone: Settings โ†’ Linked devices โ†’ Link a device.

Option 2 โ€” Pairing code

const sock = makeWASocket({ auth: state })

if (!sock.authState.creds.registered) {
  // international format without '+', e.g. US: 12125550123
  const code = await sock.requestPairingCode('12125550123')
  console.log('Pairing code:', code)
}

Enter the code on the phone via Link a device โ†’ Link with phone number instead. The pairing key derivation matches WhatsApp's protocol (PBKDF2) โ€” verified in this codebase.

Handling connection state

sock.ev.on('connection.update', (update) => {
  const { connection, lastDisconnect, qr } = update
  if (qr) { /* render your own QR if you prefer */ }
  if (connection === 'open') console.log('Connected as', sock.user?.id)
  if (connection === 'close') { /* decide: reconnect / stop */ }
})

Safe reconnects

Since v1.0.5, use AunuAntiBan's classifyDisconnect() to decide what to do โ€” don't guess from raw codes:

import { classifyDisconnect } from 'aunubaileys'

sock.ev.on('connection.update', ({ connection, lastDisconnect }) => {
  if (connection !== 'close') return
  const info = classifyDisconnect(lastDisconnect?.error)
  // info: { category, action: 'stop' | 'restart' | 'reconnect' | 'backoff', detail }
  if (info.action === 'reconnect') start()
  else if (info.action === 'backoff') setTimeout(start, 15000)
  else console.log('Stopping:', info.detail) // e.g. logged out โ†’ pair again
})

Socket end handlers (v1.0.3+)

sock.registerSocketEndHandler(() => {
  console.log('Socket ended โ€” clean up resources here')
})

And for full teardown when closing a socket, the event emitter now has a real sock.ev.destroy() (v1.0.4).

Never reconnect in a tight loop. Aggressive reconnects are the easiest way to get an account restricted. Combine backoff with AunuAntiBan.