open-wa
DocsLink-code login

Link-code login

Authenticate an embedded Node.js session with a WhatsApp link code.

Link-code login pairs a browser session with the WhatsApp account on your phone. In the supported npm release 5.1.0, embedded applications can request the code through createClient and receive it from the runtime event emitter. The published Easy API CLI does not forward linkCode or print the generated event, so use its QR flow instead.

1. Create the project

Install the matching runtime and Puppeteer driver packages in a new Node.js project:

mkdir open-wa-link-code
cd open-wa-link-code
npm init -y
npm pkg set type=module
npm install @open-wa/wa-automate@5.1.0 @open-wa/driver-puppeteer@5.1.0

Save the following as index.mjs. Replace the example number with the full phone number, including country code, using digits only.

import { Client, createClient } from '@open-wa/wa-automate';
import { PuppeteerDriver } from '@open-wa/driver-puppeteer';

const runtime = await createClient({
  sessionId: 'link-code-demo',
  driver: new PuppeteerDriver(),
  headless: false,
  linkCode: '447123456789',
});

runtime.events.on('launch.auth.linkCode.generated', (event) => {
  const code = event.details?.linkCode;
  if (code) console.log('Enter this code on your phone:', code);
});

const client = new Client({
  client: runtime,
  transport: runtime.getTransport(),
});

const shutdown = async (signal) => {
  await client.stop(signal);
};

process.once('SIGINT', () => void shutdown('SIGINT'));
process.once('SIGTERM', () => void shutdown('SIGTERM'));

await client.start();

const readiness = runtime.getReadiness();
console.log('WhatsApp session ready:', readiness.ready);

The event listener is attached before client.start() because startup emits the generated code while authenticating. Run the application and keep its terminal open:

node index.mjs

2. Enter the code on your phone

On the phone that owns the number in linkCode:

  1. Open WhatsApp.
  2. Open Settings on iPhone, or Menu then Linked devices on Android.
  3. Tap Link a device.
  4. Choose Link with phone number instead. The wording can vary by platform version.
  5. Enter the code printed in the computer's local terminal and confirm the link.

Use digits only in linkCode. Do not include a plus sign, spaces, brackets, or dashes. For example, use 447123456789, not +44 7123 456789. A chat ID such as 447123456789@c.us belongs in messaging calls after the session connects, not in linkCode.

3. Confirm readiness

The program prints WhatsApp session ready: true after startup completes successfully. In code, runtime.getReadiness() exposes ready, pending, and blockers; proceed with messaging only when ready is true. Code generation alone does not mean that the phone completed pairing.

Link codes expire. If the phone rejects the code or startup times out, stop the current process and start it again to request a fresh code. If the loaded WhatsApp Web build does not support link-code login, the runtime can fall back to QR authentication in the browser window.

For a complete embedded lifecycle and messaging example, see Custom code. For the Easy API CLI, use the Quick start QR flow.

Was this helpful?

Your answer includes the page path and docs version.

On this page