Skip to main content

Documentation Index

Fetch the complete documentation index at: https://whiskeysockets-docs-jids-socket-config-ptbr.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

QR code authentication is the default way to link Baileys to your WhatsApp account. Baileys emits a QR string on the connection.update event, which you render however you like — to the terminal, to an image, or to your frontend. Open WhatsApp on your phone, navigate to Linked Devices, and scan the code to complete the link.
The printQRInTerminal socket option is deprecated and will be removed in a future version. Listen for the qr field on the connection.update event and render the code yourself.

Basic setup

1

Install Baileys

If you haven’t already, add Baileys to your project.
npm install @whiskeysockets/baileys qrcode-terminal
2

Create the socket and listen for the QR string

The QR string is delivered through the connection.update event. Use a library like qrcode-terminal (or qrcode for image/canvas output) to render it.
import makeWASocket from '@whiskeysockets/baileys'
import qrcode from 'qrcode-terminal'

const sock = makeWASocket({})

sock.ev.on('connection.update', ({ connection, qr }) => {
  if (qr) {
    // Render the QR code yourself — printQRInTerminal is deprecated.
    qrcode.generate(qr, { small: true })
  }

  if (connection === 'open') {
    console.log('Connected to WhatsApp')
  }
})
In production, send the qr string to your frontend and render it there instead of in the terminal.
3

Scan the QR code

On your phone, open WhatsApp and go to Settings → Linked Devices → Link a Device. Point your camera at the rendered QR code.Once scanned, WhatsApp forcibly disconnects the socket so Baileys can reconnect with full credentials. This is expected — handle it by reconnecting on DisconnectReason.restartRequired (see Keeping the connection alive).

Customizing the browser identity and receiving full history

The browser identity Baileys presents to WhatsApp affects how your client appears in Linked Devices and how much message history is delivered on first sync. Both options are configured via the socket — see Configure the Baileys socket connection for the Browsers presets and the syncFullHistory flag.

Keeping the connection alive

Baileys maintains a persistent WebSocket connection. If your process exits, the session is lost. Handle the connection.update event to detect disconnections and reconnect when appropriate.
import makeWASocket, { DisconnectReason } from '@whiskeysockets/baileys'
import { Boom } from '@hapi/boom'
import qrcode from 'qrcode-terminal'

function connect() {
  const sock = makeWASocket({})

  sock.ev.on('connection.update', ({ connection, lastDisconnect, qr }) => {
    if (qr) {
      qrcode.generate(qr, { small: true })
    }

    if (connection === 'close') {
      const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode
      const shouldReconnect = statusCode !== DisconnectReason.loggedOut

      if (shouldReconnect) {
        connect()
      } else {
        console.log('Logged out. Re-scan the QR code to reconnect.')
      }
    }
  })
}

connect()
Pair QR code authentication with useMultiFileAuthState so you only need to scan once. See Save and restore WhatsApp sessions.