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:
- Stuck at
Status: disconnected...with no QR code. [Neon/PG] syncBaileysAuth error: ENOENT: no such file or directory, open '.../pre-key-16.json'.- 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.
- Test how ChatGPT, Claude & Perplexity read your site: Run Free AI Vision Simulator
- Audit mobile speed, Core Web Vitals & SEO blockers: Run Free Lighthouse SEO Audit
- Need custom MERN, ERP, or AI automation without middlemen? Book a Free Discovery Call