Skip to main content

Overview

Pairing code authentication allows you to connect Baileys to WhatsApp without scanning a QR code. Instead, you enter a phone number and receive a pairing code to enter in the WhatsApp app.
Pairing code is not the Mobile API - it’s a method to connect WhatsApp Web without QR codes. You can still only connect one device. Learn more

Basic Pairing Code Setup

1

Disable QR Printing

Set printQRInTerminal to false when using pairing codes.
2

Check Registration Status

Verify if credentials are already registered before requesting a pairing code.
3

Request Pairing Code

Call requestPairingCode() with the phone number.
4

Enter Code in WhatsApp

User enters the pairing code in their WhatsApp app under “Linked Devices”.

Implementation

Basic Example

Interactive CLI Example

From the official example.ts:

Phone Number Format

The phone number must be in a specific format:
  • Include country code
  • No + prefix
  • No () parentheses
  • No - hyphens
  • Only numbers

Format Examples

Phone Number Validation

Always sanitize user input:

Complete Example with Error Handling

How to Use Pairing Code in WhatsApp

1

Open WhatsApp on Your Phone

Open the WhatsApp mobile app on your device.
2

Go to Linked Devices

Tap on Settings > Linked Devices.
3

Link a Device

Tap on “Link a Device” button.
4

Enter Pairing Code

Instead of scanning QR, tap “Link with phone number instead” and enter the pairing code displayed in your terminal.

Checking Registration Status

Before requesting a pairing code, check if already registered:

Pairing Code vs QR Code

Advantages:
  • Works in headless environments
  • No need to display graphics
  • Better for CLI applications
  • Can be sent via SMS/email
Disadvantages:
  • Requires phone number input
  • Slightly more steps for user
  • Only works with WhatsApp Web protocol

Environment Detection

Automatically choose method based on environment:

Best Practices

1

Validate Phone Numbers

Always sanitize and validate phone numbers before calling requestPairingCode().
2

Save Credentials

Use useMultiFileAuthState to avoid requesting new pairing codes on every restart.
3

Check Registration

Only request pairing codes when creds.registered is false.
4

Handle Timeouts

Pairing codes expire. Implement retry logic if the user takes too long to enter the code.

Troubleshooting

Pairing Code Not Working

  • Ensure phone number includes country code
  • Remove all special characters (+, -, spaces, parentheses)
  • Check that printQRInTerminal is set to false
  • Verify you’re checking creds.registered status correctly

Connection Closes Immediately

  • Make sure to save credentials with creds.update event
  • Check internet connectivity
  • Verify WhatsApp app is updated to latest version

Next Steps

QR Code Method

Use QR code authentication instead

Session Management

Learn how to save and restore sessions

Handling Events

Process messages and connection events

Socket Configuration

Advanced socket configuration options