Authentication & Sessions
WhatsApp Web works as a linked device. The first time a client starts, WhatsApp asks you to link it, either by scanning a QR code or by typing a pairing code on the phone. After that, the browser profile holds the session, so an auth strategy decides where that profile lives and whether it survives restarts.
| Strategy | Session survives restarts | Use it when |
|---|---|---|
NoAuth |
No | One-off scripts and experiments. |
LocalAuth |
Yes, on local disk | One server or one container with a persistent volume. |
RemoteAuth |
Yes, in your own store | Ephemeral containers, or moving a session between hosts. |
Without any strategy and without sessionDirectory, the client uses NoAuth behavior: a temporary browser profile that is discarded on close.
QR code login
Section titled “QR code login”Listen for QrCodeReceived and render the code. TerminalQrCode::render() prints a compact, scannable QR code using half-block characters:
use Pam\WhatsApp\Event\QrCodeReceived;use Pam\WhatsApp\TerminalQrCode;
$client->onQrCode(static function (QrCodeReceived $event): void { echo TerminalQrCode::render($event->code), "\n";});$event->code is the raw QR payload. To show it somewhere else, for example on an admin page, render it with any QR library; chillerlan/php-qrcode is already installed as a dependency.
WhatsApp rotates the QR code periodically, and the event fires again for each new code. Limit the number of rotations with qrMaxRetries. When the limit is exceeded, the client disconnects with DisconnectionReason::QrRetryLimit:
$client = new Client(new ClientOptions(qrMaxRetries: 5));Pairing code login
Section titled “Pairing code login”On a server without a screen for the QR code, link with a pairing code instead. Pass the account’s phone number, digits only with the country code:
use Pam\WhatsApp\ClientOptions;use Pam\WhatsApp\Event\PairingCodeReceived;use Pam\WhatsApp\EventType;use Pam\WhatsApp\PairWithPhoneNumber;
$client = new Client(new ClientOptions( pairWithPhoneNumber: new PairWithPhoneNumber('15555550123'),));
$client->on(EventType::PairingCodeReceived, static function (PairingCodeReceived $event): void { echo "Enter this code on your phone: {$event->code}\n";});On the phone, open Linked devices > Link a device > Link with phone number instead and type the code. PairWithPhoneNumber also accepts showNotification (default true) and intervalMs (default 180000), and you can request a code manually with $client->requestPairingCode($digits) or cancel it with $client->cancelPairingCode().
LocalAuth
Section titled “LocalAuth”LocalAuth keeps the browser profile on disk:
use Pam\WhatsApp\Auth\LocalAuth;use Pam\WhatsApp\Auth\LocalAuthOptions;
$client = new Client(new ClientOptions( authStrategy: new LocalAuth(new LocalAuthOptions( clientId: 'sales', // letters, digits, _ and - only dataPath: __DIR__.'/.sessions', // default: ./.wwebjs_auth )),));The profile is stored in {dataPath}/session-{clientId}, or {dataPath}/session without a client id, and is created with 0700 permissions. Use a distinct clientId for each account you run.
As a shortcut, pass sessionDirectory to use one directory as the browser profile directly. You cannot combine sessionDirectory with authStrategy.
RemoteAuth
Section titled “RemoteAuth”RemoteAuth keeps the session in a store you implement, such as object storage or a database, and restores it into a local working directory on startup. Implement Pam\WhatsApp\Auth\RemoteStore:
<?php
declare(strict_types=1);
namespace App\WhatsApp;
use Pam\WhatsApp\Auth\RemoteStore;use Pam\WhatsApp\Auth\RemoteStoreOptions;
final class DirectorySessionStore implements RemoteStore{ public function __construct(private readonly string $root) { }
public function sessionExists(RemoteStoreOptions $options): bool { return is_dir($this->root.'/'.$options->session); }
// Copy the saved session into the local profile directory ($options->path). public function extract(RemoteStoreOptions $options): void { self::copy($this->root.'/'.$options->session, (string) $options->path); }
// Copy the local profile directory ($options->path) into the store. public function save(RemoteStoreOptions $options): void { self::copy((string) $options->path, $this->root.'/'.$options->session); }
public function delete(RemoteStoreOptions $options): void { // Remove $this->root/$options->session recursively. }
private static function copy(string $from, string $to): void { // Recursively copy $from into $to. }}Then configure the strategy. backupSyncIntervalMs must be at least 60000:
use App\WhatsApp\DirectorySessionStore;use Pam\WhatsApp\Auth\RemoteAuth;use Pam\WhatsApp\Auth\RemoteAuthOptions;use Pam\WhatsApp\Event\RemoteSessionSaved;use Pam\WhatsApp\EventType;
$client = new Client(new ClientOptions( authStrategy: new RemoteAuth(new RemoteAuthOptions( store: new DirectorySessionStore('/mnt/sessions'), backupSyncIntervalMs: 300_000, clientId: 'sales', dataPath: '/tmp/whatsapp', )),));
$client->on(EventType::RemoteSessionSaved, static function (RemoteSessionSaved $event): void { echo "Session backed up\n";});How it behaves:
- On startup, the local
{dataPath}/RemoteAuth-{clientId}directory is wiped and, if the store has a session namedRemoteAuth-{clientId}, it is extracted there. - After the client is ready, the first backup happens about 60 seconds later for a new session, then every
backupSyncIntervalMs. Backups run while the client pumps events. RemoteSessionSavedfires after the first backup of a session that did not exist in the store yet.logout()deletes the session from the store as well as from disk.
Authentication events and timeouts
Section titled “Authentication events and timeouts”| Event | Payload | Meaning |
|---|---|---|
EventType::Authenticated |
Event\Authenticated (timestamp, authPayload) |
The session is authenticated. |
EventType::AuthenticationFailure |
Event\ClientError (message) |
Restoring the session failed. The client state becomes Failed. |
EventType::Ready |
Event\Ready (timestamp) |
The client is ready to send and receive. |
authenticationTimeoutSeconds (default 60.0) controls how long the client waits for authentication; authTimeoutMs overrides it in milliseconds for whatsapp-web.js compatibility.
Session conflicts
Section titled “Session conflicts”WhatsApp allows a single WhatsApp Web session per account at a time. If you open WhatsApp Web elsewhere, the connection state becomes ConnectionState::Conflict. Set takeoverOnConflict: true to take the session back automatically, optionally after takeoverTimeoutMs:
$client = new Client(new ClientOptions( takeoverOnConflict: true, takeoverTimeoutMs: 5_000,));Logging out and closing
Section titled “Logging out and closing”| Method | Effect |
|---|---|
$client->destroy() (alias close()) |
Closes the browser. The saved session stays, so the next start reconnects without a QR code. |
$client->logout() |
Unlinks the device from the account and deletes the saved session. The next start needs a new QR code. |
Both set the client state to ClientState::Closed, which ends run().