{"record":{"id":"931ea748a10a8e7e","repo":"screenpipe/screenpipe","slug":"whatsapp-not-connected","errorCode":null,"errorMessage":"whatsapp not connected","messagePattern":"whatsapp not connected","errorType":"http","errorClass":null,"httpStatus":503,"severity":"error","filePath":"crates/screenpipe-connect/src/whatsapp/gateway.mjs","lineNumber":109,"sourceCode":"}\n\n// HTTP server — Pi curls this directly\nconst server = createServer(async (req, res) => {\n  res.setHeader(\"Content-Type\", \"application/json\");\n  const url = parseUrl(req);\n  const pathname = url.pathname;\n\n  if (req.method === \"POST\" && pathname === \"/send\") {\n    let body = \"\";\n    for await (const chunk of req) body += chunk;\n    try {\n      const { to, text } = JSON.parse(body);\n      if (!to || !text) {\n        res.writeHead(400);\n        return res.end(JSON.stringify({ error: \"missing 'to' or 'text'\" }));\n      }\n      if (!sock) {\n        res.writeHead(503);\n        return res.end(JSON.stringify({ error: \"whatsapp not connected\" }));\n      }\n      const jid = toJid(to);\n      await sock.sendMessage(jid, { text });\n      res.writeHead(200);\n      return res.end(JSON.stringify({ success: true, to: jid }));\n    } catch (err) {\n      res.writeHead(500);\n      return res.end(JSON.stringify({ error: err.message || String(err) }));\n    }\n  }\n\n  if (req.method === \"GET\" && pathname === \"/contacts\") {\n    if (!sock) {\n      res.writeHead(503);\n      return res.end(JSON.stringify({ error: \"whatsapp not connected\" }));\n    }\n    try {","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/screenpipe/screenpipe/blob/4ebf712990fee17eeaf904dacf749b6e96ac9bf3/crates/screenpipe-connect/src/whatsapp/gateway.mjs#L91-L127","documentation":"The WhatsApp gateway keeps a module-level `sock` (Baileys socket) that is only set after a successful QR/login. `/send` refuses with 503 and this error when `sock` is null, i.e. the gateway process is running but WhatsApp has never connected or has been logged out/disconnected.","triggerScenarios":"POSTing `/send` before scanning the QR code, after the WhatsApp session was logged out from the phone, after the socket dropped and was not re-established, or when the gateway was restarted and lost its session.","commonSituations":"Automations firing at boot before the QR scan completes; phone linked device removed by the user; long-running gateway whose session expired; firewall/network interruption killing the socket.","solutions":["Open the gateway and scan the QR code to (re)link WhatsApp, then retry the send.","Check gateway logs for disconnect reasons; if the socket dropped, restart the gateway process to re-authenticate.","Verify the linked device still exists in WhatsApp Settings > Linked Devices.","In callers, treat HTTP 503 from `/send` as 'not connected' and pause the automation until health check passes."],"exampleFix":"// before: blind send\nawait post(\"/send\", { to, text });\n\n// after: gate on connection state\nconst health = await get(\"/health\"); // or check /contacts success\nif (!health.connected) throw new Error(\"gateway offline: scan QR first\");\nawait post(\"/send\", { to, text });","handlingStrategy":"retry","validationCode":"const res = await fetch(base + \"/contacts\"); // returns 503 when not connected\nif (res.status === 503) throw new Error(\"WhatsApp not connected — scan the QR code first\");","typeGuard":null,"tryCatchPattern":"async function sendWithWait(payload, attempts = 5) {\n  for (let i = 0; i < attempts; i++) {\n    const res = await post(\"/send\", payload);\n    if (res.status !== 503) return res;\n    await sleep(5000 * (i + 1)); // wait for QR scan / reconnect\n  }\n  throw new Error(\"WhatsApp gateway stayed disconnected; re-scan QR\");\n}","preventionTips":["Gate all automations on a gateway health check before the first send of a run.","Alert when the gateway needs a QR re-scan (linked device logged out).","Restart/relink the gateway promptly after session drops; don't let sends queue against a dead socket."],"tags":["whatsapp","connection","baileys","not-connected"],"backgroundTag":"not-connected","analyzedSha":"4ebf712990fee17eeaf904dacf749b6e96ac9bf3","analyzedAt":"2026-09-01T23:33:43.065Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}