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.

Pairing code authentication lets you link Baileys to WhatsApp using an 8-digit code instead of a QR code. This is useful in headless environments where displaying a QR code is inconvenient, or when you want to link a device by entering a code on your phone manually.
Pairing code is a method to connect WhatsApp Web without scanning a QR code. It is not the Mobile API. You can only link one device per phone number using this method. See the WhatsApp FAQ for details.

Setup

1

Create the socket

Create the socket without rendering a QR code. You can simply ignore the qr field on connection.update while using pairing codes.
import makeWASocket from '@whiskeysockets/baileys'

const sock = makeWASocket({})
The legacy printQRInTerminal option is deprecated and should not be set. Don’t render the QR string while pairing by code — just call requestPairingCode instead.
2

Request a pairing code

Check sock.authState.creds.registered before requesting a code. If the socket is already registered (i.e., credentials are already present from a previous session), you do not need to request a new code.
if (!sock.authState.creds.registered) {
  // Phone number must include country code, digits only.
  // Do not include +, (), or - characters.
  const number = '15551234567'
  const code = await sock.requestPairingCode(number)
  console.log(`Pairing code: ${code}`)
}
The returned code is an 8-digit string you enter on your phone.
3

Enter the code on your phone

On your phone, open WhatsApp and go to Settings → Linked Devices → Link a Device. Select Link with phone number instead and enter the 8-digit code displayed in your terminal.Once you confirm on the phone, the connection.update event fires with connection: 'open'.

Phone number format

The phone number you pass to requestPairingCode must follow these rules:
  • Include the country code (e.g., 1 for the US, 44 for the UK)
  • Use digits only — no +, (, ), or - characters
  • Do not include spaces
const code = await sock.requestPairingCode('15551234567')  // US number
const code = await sock.requestPairingCode('447911123456') // UK number

Complete example

The following example uses readline to prompt for the phone number at runtime, matching the pattern used in the official example script.
import makeWASocket, { useMultiFileAuthState } from '@whiskeysockets/baileys'
import readline from 'readline'

const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
const question = (text: string) => new Promise<string>((resolve) => rl.question(text, resolve))

async function connect() {
  const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')

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

  sock.ev.on('connection.update', async ({ connection, qr }) => {
    // The QR event also fires in pairing code mode — use it as the trigger
    // to request a code if the device isn't registered yet.
    if (qr && !sock.authState.creds.registered) {
      const number = await question('Enter your phone number (digits only, with country code):\n')
      const code = await sock.requestPairingCode(number)
      console.log(`Pairing code: ${code}`)
    }

    if (connection === 'open') {
      console.log('Connected to WhatsApp')
      rl.close()
    }
  })

  sock.ev.on('creds.update', saveCreds)
}

connect()
The qr field in connection.update fires even in pairing code mode. Use it as your trigger to call requestPairingCode rather than calling it immediately after creating the socket, because the socket may not be ready yet.
Combine this with useMultiFileAuthState so the pairing code is only needed once. On subsequent runs, the saved credentials reconnect automatically without a new code. See Save and restore WhatsApp sessions.