Skip to main content

Overview

Baileys uses WhatsApp’s multi-device protocol for authentication. The library manages authentication state through credentials and cryptographic keys that enable secure communication with WhatsApp servers.

Authentication State

The authentication state consists of two primary components:

1. Credentials (AuthenticationCreds)

Credentials contain identity information and cryptographic material:

2. Signal Keys (SignalKeyStore)

The key store manages cryptographic keys used for message encryption:
Key types stored:
  • pre-key - Pre-keys for establishing sessions
  • session - Active session data
  • sender-key - Group encryption keys
  • app-state-sync-key - App state synchronization keys
  • identity-key - Identity verification keys

Using Multi-File Auth State

Baileys provides useMultiFileAuthState for file-based credential storage:

How It Works

The multi-file auth state:
  1. Stores credentials in creds.json
  2. Stores keys in separate files: {type}-{id}.json
  3. Uses file locks to prevent race conditions
  4. Serializes buffers using BufferJSON for safe JSON storage
From src/Utils/use-multi-file-auth-state.ts:33:
The multi-file auth state is suitable for bots and small-scale applications, but not recommended for production systems. Implement a database-backed auth state for production use.

QR Code Authentication

Connect by scanning a QR code with WhatsApp:
The QR code will be printed to the terminal. Scan it with WhatsApp to authenticate.

Pairing Code Authentication

Connect using a pairing code instead of QR:
The phone number must include the country code and contain only digits (no +, (), or -).

Initializing Credentials

When no credentials exist, Baileys generates new ones using initAuthCreds(): From src/Utils/auth-utils.ts:346:

Caching Signal Keys

For better performance, use makeCacheableSignalKeyStore to cache frequently accessed keys:
This caches keys in memory (default TTL: 5 minutes) to reduce file I/O operations.

Key Management

Critical: When messages are sent/received, Signal sessions update. You must save the updated keys (authState.keys.set() is called) or messages won’t reach recipients and decryption will fail.
The useMultiFileAuthState function automatically handles this, but custom implementations must be careful:

Account Settings

Credentials include account-level settings:

Best Practices

Production Implementation Tips:
  1. Use a database - Store credentials and keys in PostgreSQL, MongoDB, or similar
  2. Encrypt sensitive data - Encrypt credentials at rest
  3. Handle key updates - Always save keys when authState.keys.set() is called
  4. Use caching - Implement makeCacheableSignalKeyStore for performance
  5. Backup regularly - Auth state loss requires re-authentication
  6. Monitor registration - Track registered status to detect logout events

BufferJSON Serialization

When storing auth state as JSON, use the BufferJSON utility for proper Buffer serialization:
This ensures Buffers and Uint8Arrays are correctly serialized and restored.

See Also