[{"data":1,"prerenderedAt":482},["ShallowReactive",2],{"project-wapi-gateway":3},{"id":4,"title":5,"body":6,"description":111,"extension":464,"meta":465,"navigation":477,"path":478,"seo":479,"stem":480,"__hash__":481},"content\u002Fprojects\u002Fwapi-gateway.md","WAPI Gateway",{"type":7,"value":8,"toc":455},"minimark",[9,14,63,67,84,98,102,112,116,135,274,278,434,438],[10,11,13],"h2",{"id":12},"project-at-a-glance","Project at a Glance",[15,16,17,25,48,57],"ul",{},[18,19,20,24],"li",{},[21,22,23],"strong",{},"Problem:"," Integrating automated WhatsApp alerts, transactional messages, or media dispatches into backend services often leads to tightly coupled scripts that lack proper HTTP authentication, request validation, reconnection resilience, or operational monitoring.",[18,26,27,30,31,34,35,38,39,43,44,47],{},[21,28,29],{},"Deliverables:"," A headless ",[21,32,33],{},"TypeScript + Fastify 5"," REST API gateway wrapping ",[21,36,37],{},"Baileys"," (",[40,41,42],"code",{},"@whiskeysockets\u002Fbaileys","), supporting text and media delivery (both multipart file upload and remote URL), number verification, session logout, Bearer token authentication, JSON Schema validation, typing simulation, optional ",[21,45,46],{},"Telegram Bot"," event & heartbeat monitoring, Vitest test setup, and Docker \u002F PM2 deployment configs.",[18,49,50,53,54,56],{},[21,51,52],{},"Core Stack:"," Node.js 18+, TypeScript 5, Fastify 5, Baileys (",[40,55,42],{},"), Pino, Helmet, Vitest, Docker \u002F Docker Compose, PM2.",[18,58,59,62],{},[21,60,61],{},"Practical Value:"," Built solo in 2026 as a clean, self-hostable open-source backend utility for programmatic WhatsApp notification and alert dispatching.",[10,64,66],{"id":65},"project-overview","Project Overview",[68,69,70,72,73,76,77,80,81,83],"p",{},[21,71,5],{}," is a lightweight, headless WhatsApp API gateway built with ",[21,74,75],{},"TypeScript",", ",[21,78,79],{},"Fastify 5",", and ",[21,82,37],{},". Rather than embedding WhatsApp Web socket handling directly inside application servers, external services communicate with WAPI Gateway over a standard JSON and multipart REST API secured by Bearer token authentication.",[68,85,86,87,90,91,94,95,97],{},"On first startup, the service prints a QR code to the terminal for WhatsApp Linked Devices pairing and persists session credentials locally under ",[40,88,89],{},"auth\u002F",". From there, it maintains an auto-reconnecting socket connection, logs structured operational events via ",[21,92,93],{},"Pino",", and optionally reports connection health and periodic heartbeats to a ",[21,96,46],{},".",[10,99,101],{"id":100},"system-architecture-request-flow","System Architecture & Request Flow",[103,104,109],"pre",{"className":105,"code":107,"language":108},[106],"language-text","+-------------------------------------------------------------+\n|            EXTERNAL SERVICES \u002F CRON \u002F WEBHOOKS              |\n+------------------------------+------------------------------+\n                               | HTTP (JSON \u002F Multipart)\n                               | Authorization: Bearer \u003CAPI_KEY>\n                               v\n+-------------------------------------------------------------+\n|                   FASTIFY 5 API SERVER                      |\n|  - Helmet Security Headers & Pino Structured Logging        |\n|  - JSON Schema Request Validation                           |\n|  - preHandler Bearer Auth Hook (\u002Fapi\u002F*)                     |\n+--------------+-------------------------------+--------------+\n               |                               |\n               v                               v\n+-----------------------------+ +-----------------------------+\n|      WHATSAPP SERVICE       | |      TELEGRAM SERVICE       |\n|  - Baileys Socket Client    | |  - Event Alerts (Connect,   |\n|  - Local Session Storage    | |    Disconnect, Logout, QR)  |\n|  - Auto-Reconnect Handler   | |  - Periodic Uptime Heartbeat|\n|  - Typing Delay Simulation  | |    (configurable interval)  |\n+--------------+--------------+ +--------------+--------------+\n               |                               |\n               v                               v\n     WhatsApp Web Socket               Telegram Bot API\n","text",[40,110,107],{"__ignoreMap":111},"",[10,113,115],{"id":114},"rest-api-surface","REST API Surface",[68,117,118,119,122,123,126,127,130,131,134],{},"All ",[40,120,121],{},"\u002Fapi\u002F*"," endpoints are protected by a Fastify ",[40,124,125],{},"preHandler"," Bearer token hook (",[40,128,129],{},"Authorization: Bearer \u003CAPI_KEY>","), while ",[40,132,133],{},"GET \u002Fhealth"," remains unauthenticated for container and uptime probes:",[136,137,138,155],"table",{},[139,140,141],"thead",{},[142,143,144,149,152],"tr",{},[145,146,148],"th",{"align":147},"left","Method",[145,150,151],{"align":147},"Endpoint",[145,153,154],{"align":147},"Purpose",[156,157,158,174,192,214,246,260],"tbody",{},[142,159,160,166,171],{},[161,162,163],"td",{"align":147},[40,164,165],{},"GET",[161,167,168],{"align":147},[40,169,170],{},"\u002Fhealth",[161,172,173],{"align":147},"Returns socket connection status, linked user JID, and server uptime",[142,175,176,180,185],{},[161,177,178],{"align":147},[40,179,165],{},[161,181,182],{"align":147},[40,183,184],{},"\u002Fapi\u002Fstatus\u002F:number",[161,186,187,188,191],{"align":147},"Checks whether a phone number (international format without ",[40,189,190],{},"+",") is registered on WhatsApp",[142,193,194,199,204],{},[161,195,196],{"align":147},[40,197,198],{},"POST",[161,200,201],{"align":147},[40,202,203],{},"\u002Fapi\u002Fmessage",[161,205,206,207,76,210,213],{"align":147},"Sends a text message with structured error codes (",[40,208,209],{},"422 WA_NUMBER_NOT_FOUND",[40,211,212],{},"503 WA_NOT_CONNECTED",")",[142,215,216,220,225],{},[161,217,218],{"align":147},[40,219,198],{},[161,221,222],{"align":147},[40,223,224],{},"\u002Fapi\u002Fmedia",[161,226,227,228,76,231,76,234,237,238,241,242,245],{"align":147},"Sends ",[40,229,230],{},"image",[40,232,233],{},"video",[40,235,236],{},"audio",", or ",[40,239,240],{},"document"," via ",[40,243,244],{},"multipart\u002Fform-data"," (10MB default limit)",[142,247,248,252,257],{},[161,249,250],{"align":147},[40,251,198],{},[161,253,254],{"align":147},[40,255,256],{},"\u002Fapi\u002Fmedia-url",[161,258,259],{"align":147},"Sends supported media directly from a remote URL with optional caption and required document filename",[142,261,262,266,271],{},[161,263,264],{"align":147},[40,265,198],{},[161,267,268],{"align":147},[40,269,270],{},"\u002Fapi\u002Flogout",[161,272,273],{"align":147},"Logs out the active WhatsApp session and clears stored local auth credentials",[10,275,277],{"id":276},"core-engineering-highlights","Core Engineering Highlights",[279,280,281,344,370,397],"ol",{},[18,282,283,286],{},[21,284,285],{},"Layered Fastify Architecture:",[15,287,288,337],{},[18,289,290,291,294,295,38,298,301,302,38,305,76,308,301,311,38,314,76,317,76,320,76,323,326,327,38,330,76,333,336],{},"Clean separation across ",[40,292,293],{},"config\u002F"," (environment validation), ",[40,296,297],{},"hooks\u002F",[40,299,300],{},"auth.hook.ts","), ",[40,303,304],{},"routes\u002F",[40,306,307],{},"api.ts",[40,309,310],{},"health.route.ts",[40,312,313],{},"controllers\u002F",[40,315,316],{},"health",[40,318,319],{},"status",[40,321,322],{},"message",[40,324,325],{},"media","), and ",[40,328,329],{},"services\u002F",[40,331,332],{},"whatsapp.service.ts",[40,334,335],{},"telegram.service.ts",").",[18,338,339,340,343],{},"Built-in Fastify JSON Schema validation rejects malformed payloads at the boundary with standardized ",[40,341,342],{},"VALIDATION_ERROR"," responses before reaching the WhatsApp socket layer.",[18,345,346,349],{},[21,347,348],{},"Connection Resilience & Human-Like Pacing:",[15,350,351,354],{},[18,352,353],{},"Automatic reconnection handles transient socket drops without restarting the container.",[18,355,356,357,38,360,76,363,76,366,369],{},"Configurable ",[21,358,359],{},"typing simulation",[40,361,362],{},"WA_TYPING_DELAY",[40,364,365],{},"WA_TYPING_MIN_MS",[40,367,368],{},"WA_TYPING_CHAR_MS",") introduces realistic pacing proportional to message length to reduce automated-behavior flags.",[18,371,372,375],{},[21,373,374],{},"Out-of-Band Telegram Monitoring:",[15,376,377],{},[18,378,379,380,76,383,76,386,76,389,392,393,396],{},"Because WhatsApp session disconnects or forced logouts require operator awareness (such as re-scanning a QR code), the optional Telegram service pushes instant notifications for ",[40,381,382],{},"Connected",[40,384,385],{},"Disconnected",[40,387,388],{},"Logged out",[40,390,391],{},"QR needed",", and periodic ",[40,394,395],{},"Heartbeat"," uptime summaries (default every 30 minutes).",[18,398,399,402],{},[21,400,401],{},"Deployment & Operational Readiness:",[15,403,404],{},[18,405,406,407,410,411,413,414,417,418,421,422,425,426,429,430,433],{},"Ships with ",[21,408,409],{},"Helmet"," security headers, ",[21,412,93],{}," structured JSON logging, ",[21,415,416],{},"Vitest"," test configuration, ",[40,419,420],{},"Dockerfile"," + ",[40,423,424],{},"docker-compose.yml"," for containerized hosting, and ",[40,427,428],{},"ecosystem.config.cjs"," for ",[21,431,432],{},"PM2"," process management.",[10,435,437],{"id":436},"scope-known-limitations","Scope & Known Limitations",[15,439,440,446],{},[18,441,442,445],{},[21,443,444],{},"Single-Session Design:"," Scoped intentionally as a lightweight 1-instance = 1-WhatsApp-number gateway rather than a multi-tenant SaaS hub.",[18,447,448,451,452,454],{},[21,449,450],{},"Unofficial Protocol Caveat:"," Because it relies on ",[21,453,37],{}," (an unofficial WhatsApp Web client library), protocol changes by WhatsApp\u002FMeta can require library updates, and bulk\u002Fspam messaging risks account restrictions. Official commercial high-volume messaging should use Meta's WhatsApp Business API.",{"title":111,"searchDepth":456,"depth":456,"links":457},2,[458,459,460,461,462,463],{"id":12,"depth":456,"text":13},{"id":65,"depth":456,"text":66},{"id":100,"depth":456,"text":101},{"id":114,"depth":456,"text":115},{"id":276,"depth":456,"text":277},{"id":436,"depth":456,"text":437},"md",{"subtitle":466,"slug":467,"client":468,"year":469,"role":470,"category":471,"technologies":472},"Headless WhatsApp REST API Gateway with Fastify & Baileys","wapi-gateway","Personal Open Source",2026,"Solo Backend Developer","Developer Tools & Infrastructure",[75,473,474,37,475,476,416],"Fastify","Node.js","REST API","Docker",true,"\u002Fprojects\u002Fwapi-gateway",{"title":5,"description":111},"projects\u002Fwapi-gateway","6nvTEPdn3zYutIQOMKUsthlaYVtbIe5wdZPHhSeW33I",1796006983851]