All projects

Elysia WhatsApp Bot API: Multi-session messaging & webhooks

A lightweight, containerized multi-session WhatsApp REST API and webhook service built with Bun, ElysiaJS, TypeScript, and Baileys v7.

  • TypeScript
  • Bun
  • Elysia
  • Baileys
  • Pino
  • Docker
Runtime
Bun + Elysia
Typed TypeScript API
Session model
Multi-session
Isolated Docker volumes
Delivery
REST + Webhooks
Text, media, docs, polls

The problem

Integrating WhatsApp notifications, document delivery, and interactive polls into backend systems often means dealing with fragile single-session scripts where routing, session credentials, and webhook callbacks are tangled into a single file. When a socket drops or a container restarts, sessions break and inbound events are lost.

The goal was a clean, self-hostable REST API that manages multiple independent WhatsApp sessions, forwards inbound messages and poll votes to webhooks, and deploys cleanly with Docker.

Constraints

  • Multiple WhatsApp accounts had to run concurrently inside one service without leaking credentials across sessions.
  • Outbound delivery needed to support text, image URLs, local documents, and interactive polls.
  • Session disconnects had to recover automatically without manual process restarts.
  • Deployment needed to work cleanly with Docker volumes so auth state and webhook configs survive restarts.

Architecture

flowchart LR; Client[Backend / Client] --> Routes[Elysia Routes & Controllers]; Routes --> Service[MessageService & wa-manager]; Service --> Baileys[Baileys v7 Sessions]; Baileys --> Auth[(auth_info volume)]; Service --> Hooks[WebhookManager & PollStore]; Hooks --> Data[(data volume)]; Hooks -->|HTTP POST| Target[Consumer Webhooks]
Layered ElysiaJS service, Baileys session manager, and webhook persistence

The codebase separates HTTP concerns (routes/, controllers/) from messaging logic (services/MessageService.ts, wa-manager.ts), webhook persistence (webhook/WebhookManager.ts), and poll state tracking (poll/PollStore.ts). Structured logging runs through Pino, and compose.yaml mounts ./auth_info and ./data outside the oven/bun:1 container image.

Decisions

Layered TypeScript structure on Bun + Elysia. Splitting routes, controllers, services, and session management makes adding new message types or webhook events straightforward instead of growing a monolithic handler file.

Isolated persistent volumes for credentials and webhooks. Separating auth_info/ (Baileys session state) and data/ (webhook registrations) from the container layer lets the service rebuild and restart without forcing QR re-authentication.

Built-in auto-reconnect and poll vote tracking. Tracking poll messages in PollStore allows inbound vote updates to be resolved and forwarded cleanly to configured consumer webhooks, while automatic reconnect handles transient WhatsApp socket drops.

Result

The service provides a fast, Docker-ready WhatsApp messaging and webhook gateway in TypeScript that can be plugged into ERP, retail, or notification backends with standard REST calls.

What I would change

Moving PollStore and webhook delivery retries from in-memory/file storage to SQLite or Redis with dead-letter retries, plus adding API key authentication middleware and rate limiting by default, would make it ready for untrusted multi-tenant deployments.