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.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 The returned
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.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 torequestPairingCode must follow these rules:
- Include the country code (e.g.,
1for the US,44for the UK) - Use digits only — no
+,(,), or-characters - Do not include spaces
Complete example
The following example usesreadline to prompt for the phone number at runtime, matching the pattern used in the official example script.
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.