The Problem

Bondly uses WhatsApp (@whiskeysockets/baileys) alongside Telegram for passwordless OTP signup, 2FA, and notifications. In production, we ran into three real-world issues:

  1. Stuck at Status: disconnected... with no QR code.
  2. [Neon/PG] syncBaileysAuth error: ENOENT: no such file or directory, open '.../pre-key-16.json'.
  3. Valid 10-digit Indian numbers rejected with "This number is not registered on WhatsApp".

1. Diagnosing the Silent 405 with /api/auth/whatsapp/health

Instead of guessing from generic logs, we instrumented baileysClient.js to capture lastDisconnectCode, lastDisconnectReason, hasAuthCreds, and reconnectAttempts, and exposed GET /api/auth/whatsapp/health (with ?strict=1 returning 503 for uptime monitors).

The very first check pinpointed the culprit:

{ "lastDisconnectCode": 405, "lastDisconnectReason": "Connection Failure", "reconnectAttempts": 12 } 

WhatsApp Web had updated its protocol, and @whiskeysockets/baileys@6.7.16 with a fallback static version was being rejected with 405 before a QR could be generated. Upgrading to ^7.1.0 and calling fetchLatestWaWebVersion() fixed the handshake immediately.

2. Fixing the ENOENT Race During Auth Backup

Baileys continuously rotates and deletes pre-key-*.json files in data/baileys_auth/. When syncBaileysAuthToPg() called fs.readdirSync(authDir) and looped over filenames, a file deleted mid-loop threw ENOENT, aborting the entire sync. Checking fs.existsSync(fullPath) and wrapping fs.readFileSync in a per-file try/catch eliminated the crash.

3. Fixing onWhatsApp() False Negatives

Users naturally type 10-digit numbers like 9876543210 without +91. We added consistent normalization (0-prefix stripping + auto-prefixing 91 for 10-digit numbers) and made sendWhatsAppViaBaileys() fall back to direct sock.sendMessage() if sock.onWhatsApp() returns an empty/timed-out response rather than an explicit exists: false.

Free Engineering & AI Search Diagnostics

Originally published at Kinetic Code Labs.