Sign inSign up

setupautomatizado/whatsapp-flows-server

By setupautomatizado

β€’Updated 11 months ago

WhatsApp Flow Server

Image
0

2.4K

setupautomatizado/whatsapp-flows-server repository overview

⁠WhatsApp Flows Server

License: MIT Node.js Version TypeScript Docker Docker Pulls GitHub Actions Semantic Release

Production-ready Node.js server for WhatsApp Flows with automated CI/CD, multi-arch Docker support, and DDD architecture

Features⁠ β€’ Quick Start⁠ β€’ Documentation⁠ β€’ Architecture⁠ β€’ Contributing⁠


β πŸ“– Overview

WhatsApp Flow Server is a robust, production-ready TypeScript server that implements the WhatsApp Flows API⁠ using Domain-Driven Design (DDD) principles. It provides a complete solution for handling WhatsApp Flow interactions with built-in encryption, webhook processing, and comprehensive CI/CD automation.

⁠🎯 What Problem Does It Solve?

WhatsApp Flows enable rich, interactive experiences within WhatsApp, but implementing them requires:

  • Complex encryption (RSA-2048 + AES-128-GCM with mandatory IV flip)
  • Dual endpoint architecture (Flow Data API + Webhooks)
  • Strict performance requirements (<3s response time)
  • Secure key management and Meta API integration
  • Production-ready infrastructure with monitoring and scaling

This server solves all these challenges with:

  • βœ… Pre-configured encryption with IV flip pattern
  • βœ… Dual endpoint architecture out-of-the-box
  • βœ… Automatic database migrations
  • βœ… Multi-architecture Docker support (AMD64 + ARM64)
  • βœ… Complete CI/CD pipeline with semantic versioning
  • βœ… Production-ready with health checks and logging

⁠✨ Features

⁠Core Capabilities
  • πŸ—οΈ Domain-Driven Design - Clean architecture with separated layers (Domain, Application, Infrastructure)
  • πŸ” WhatsApp Flow Encryption - Complete RSA-2048 + AES-128-GCM implementation with mandatory IV flip
  • πŸ”„ Dual Endpoint System - Flow Data API + Webhook receiver with signature validation
  • πŸ“Š PostgreSQL Storage - Persistent storage for Flows, Sessions, and Responses
  • βœ… Schema Validation - Type-safe with Zod v4
  • πŸ“ Structured Logging - Winston with rotation and multiple transports
⁠DevOps & Deployment
  • 🐳 Multi-Arch Docker - Native support for AMD64 (Intel/AMD) and ARM64 (Apple Silicon, AWS Graviton)
  • πŸš€ Automated CI/CD - GitHub Actions with semantic release and Docker Hub publishing
  • πŸ“¦ Conventional Commits - Automated versioning and changelog generation
  • πŸ”„ Auto Migrations - Database migrations run automatically on container startup
  • πŸ’ͺ Production Ready - Health checks, monitoring, and graceful shutdown
⁠Developer Experience
  • πŸ› οΈ TypeScript 5.9 - Full type safety with strict mode
  • 🎨 Modern Stack - Express 5.x, PostgreSQL 18, Node.js 20+
  • πŸ”§ Developer Tools - ESLint, Prettier, Husky, Commitlint
  • πŸ“– Comprehensive Docs - Complete guides for setup, deployment, and troubleshooting
  • πŸ§ͺ Testing Ready - Structure prepared for unit and integration tests

β πŸ›οΈ Architecture

⁠System Architecture
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        WhatsApp Platform                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
             β”‚                                      β”‚
             β”‚ Encrypted Flow Data                  β”‚ Webhook Events
             β”‚ (RSA-2048 + AES-128-GCM)            β”‚ (nfm_reply)
             β–Ό                                      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Flow Endpoint (Data API)  β”‚      β”‚   Webhook Endpoint       β”‚
β”‚   POST /flows/endpoint/:id  β”‚      β”‚   POST /webhooks/whatsappβ”‚
β”‚                             β”‚      β”‚                          β”‚
β”‚   - Decrypt request         β”‚      β”‚   - Validate signature   β”‚
β”‚   - Process action          β”‚      β”‚   - Parse response_json  β”‚
β”‚   - Return encrypted data   β”‚      β”‚   - Forward to callback  β”‚
β”‚   - < 3s response time      β”‚      β”‚   - Store in database    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚                                β”‚
               β”‚                                β”‚
               β–Ό                                β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         Application Layer                        β”‚
β”‚                                                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ HandleFlowRequest    β”‚        β”‚ ProcessWebhook          β”‚   β”‚
β”‚  β”‚ UseCase              β”‚        β”‚ UseCase                 β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚             β”‚                                 β”‚                 β”‚
β”‚             β–Ό                                 β–Ό                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚              Domain Layer (Business Logic)              β”‚   β”‚
β”‚  β”‚                                                          β”‚   β”‚
β”‚  β”‚  β€’ FlowEngine - Navigation and data exchange           β”‚   β”‚
β”‚  β”‚  β€’ Flow - Immutable Flow templates                     β”‚   β”‚
β”‚  β”‚  β€’ FlowSession - User session tracking                 β”‚   β”‚
β”‚  β”‚  β€’ FlowResponse - Completed Flow data                  β”‚   β”‚
β”‚  β”‚  β€’ EncryptionService - RSA/AES with IV flip            β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                             β”‚                                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Infrastructure Layer                          β”‚
β”‚                                                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   PostgreSQL     β”‚  β”‚   Express HTTP  β”‚  β”‚   Axios HTTP  β”‚  β”‚
β”‚  β”‚   Repositories   β”‚  β”‚   Server        β”‚  β”‚   Client      β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                                  β”‚
β”‚  Database Tables:                                               β”‚
β”‚  β€’ flows - Flow JSON templates                                 β”‚
β”‚  β€’ flow_sessions - Active/completed sessions                   β”‚
β”‚  β€’ flow_responses - Completed Flow data                        β”‚
β”‚  β€’ webhook_events - Webhook audit trail                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
⁠Flow Interaction Sequence
User                WhatsApp          Flow Server         Database        Your System
  β”‚                    β”‚                    β”‚                 β”‚                β”‚
  β”‚ Click Flow Button  β”‚                    β”‚                 β”‚                β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                    β”‚                 β”‚                β”‚
  β”‚                    β”‚                    β”‚                 β”‚                β”‚
  β”‚                    β”‚ POST /flows/endpoint (INIT)          β”‚                β”‚
  β”‚                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                 β”‚                β”‚
  β”‚                    β”‚                    β”‚ Create Session  β”‚                β”‚
  β”‚                    β”‚                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                β”‚
  β”‚                    β”‚                    β”‚ Return INIT     β”‚                β”‚
  β”‚                    β”‚<────────────────────                 β”‚                β”‚
  β”‚                    β”‚                    β”‚                 β”‚                β”‚
  β”‚  Display Screen 1  β”‚                    β”‚                 β”‚                β”‚
  β”‚<────────────────────                    β”‚                 β”‚                β”‚
  β”‚                    β”‚                    β”‚                 β”‚                β”‚
  β”‚ Fill & Submit      β”‚                    β”‚                 β”‚                β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                    β”‚                 β”‚                β”‚
  β”‚                    β”‚ POST /flows/endpoint (data_exchange) β”‚                β”‚
  β”‚                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                 β”‚                β”‚
  β”‚                    β”‚                    β”‚ Update Session  β”‚                β”‚
  β”‚                    β”‚                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                β”‚
  β”‚                    β”‚                    β”‚ Return Screen 2 β”‚                β”‚
  β”‚                    β”‚<────────────────────                 β”‚                β”‚
  β”‚                    β”‚                    β”‚                 β”‚                β”‚
  β”‚  Display Screen 2  β”‚                    β”‚                 β”‚                β”‚
  β”‚<────────────────────                    β”‚                 β”‚                β”‚
  β”‚                    β”‚                    β”‚                 β”‚                β”‚
  β”‚ Complete Flow      β”‚                    β”‚                 β”‚                β”‚
  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                    β”‚                 β”‚                β”‚
  β”‚                    β”‚ POST /flows/endpoint (complete)      β”‚                β”‚
  β”‚                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                 β”‚                β”‚
  β”‚                    β”‚                    β”‚ Mark Complete   β”‚                β”‚
  β”‚                    β”‚                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                β”‚
  β”‚                    β”‚                    β”‚                 β”‚                β”‚
  β”‚                    β”‚ POST /webhooks/whatsapp (nfm_reply)  β”‚                β”‚
  β”‚                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                 β”‚                β”‚
  β”‚                    β”‚                    β”‚ Store Response  β”‚                β”‚
  β”‚                    β”‚                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                β”‚
  β”‚                    β”‚                    β”‚ Forward Data    β”‚                β”‚
  β”‚                    β”‚                    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚
  β”‚                    β”‚                    β”‚                 β”‚                β”‚
  β”‚  "Thank you!"      β”‚                    β”‚                 β”‚                β”‚
  β”‚<────────────────────                    β”‚                 β”‚                β”‚
⁠Encryption Flow (IV Flip Pattern)
WhatsApp Request                    Server Response
     β”‚                                    β”‚
     β”‚  Encrypted with:                  β”‚  Encrypted with:
     β”‚  β€’ AES key encrypted by RSA       β”‚  β€’ FLIPPED IV ⚠️
     β”‚  β€’ Normal IV                      β”‚  β€’ Same AES key
     β”‚                                   β”‚
     β–Ό                                   β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Decrypt AES Keyβ”‚              β”‚ Flip IV Buffer β”‚
β”‚ with RSA       β”‚              β”‚ (reverse bytes)β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                                β”‚
        β–Ό                                β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Decrypt Payloadβ”‚              β”‚ Encrypt Payloadβ”‚
β”‚ with AES       β”‚              β”‚ with AES       β”‚
β”‚ (normal IV)    β”‚              β”‚ (flipped IV)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                                β”‚
        β–Ό                                β–Ό
   Flow Data                      Encrypted Response

β πŸš€ Quick Start

⁠Prerequisites
  • Node.js >= 20.0.0
  • Docker & Docker Compose (recommended) or PostgreSQL 18+
  • WhatsApp Business Account with Flows enabled
  • Meta Developer Account for API credentials
# 1. Clone the repository
git clone https://github.com/guilhermejansen/whatsapp-flows-server.git
cd whatsapp-flows-server

# 2. Configure environment
cp .env.docker.example .env
nano .env  # Edit with your credentials

# 3. Start services
docker-compose up -d

# 4. Generate encryption keys
docker-compose exec app npm run generate-keys
# Copy output to .env

# 5. Restart application
docker-compose restart app

# 6. Register public key with Meta
docker-compose exec app npm run register-key

# 7. Verify health
curl http://localhost:3000/health

Your server is now running! πŸŽ‰

Configure WhatsApp Manager:

  • Flow Endpoint: https://your-domain.com/flows/endpoint/csat-feedback
  • Webhook URL: https://your-domain.com/webhooks/whatsapp
⁠Option 2: Local Development
# 1. Clone and install
git clone https://github.com/guilhermejansen/whatsapp-flows-server.git
cd whatsapp-flows-server
npm install

# 2. Setup PostgreSQL
createdb whatsapp_flows
npm run migrate

# 3. Generate keys
npm run generate-keys
# Copy output to .env

# 4. Configure environment
cp .env.example .env
nano .env  # Edit with your credentials

# 5. Register public key
npm run register-key

# 6. Start development server
npm run dev

β πŸ“¦ Docker Deployment

⁠Multi-Architecture Support

Pre-built images available for AMD64 (Intel/AMD) and ARM64 (Apple Silicon, AWS Graviton):

# Pull latest version
docker pull setupautomatizado/whatsapp-flows-server:latest

# Or specific version
docker pull setupautomatizado/whatsapp-flows-server:1.0.0
⁠Production Deployment
⁠1. Server Setup
# Create directory
mkdir -p ~/whatsapp-flows-server-production
cd ~/whatsapp-flows-server-production

# Download compose file
curl -O https://raw.githubusercontent.com/guilhermejansen/whatsapp-flows-server/main/docker-compose.yml

# Configure environment
curl -O https://raw.githubusercontent.com/guilhermejansen/whatsapp-flows-server/main/.env.docker.example
cp .env.docker.example .env
nano .env
⁠2. SSL Setup (Nginx + Let's Encrypt)
# Install Nginx + Certbot
sudo apt update
sudo apt install nginx certbot python3-certbot-nginx -y

# Configure Nginx
sudo nano /etc/nginx/sites-available/whatsapp-flows-server

Nginx Configuration:

server {
    listen 443 ssl http2;
    server_name your-domain.com;

    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # WhatsApp requires < 3s response
        proxy_connect_timeout 2s;
        proxy_send_timeout 2s;
        proxy_read_timeout 2s;
    }
}
# Enable site
sudo ln -s /etc/nginx/sites-available/whatsapp-flows-server /etc/nginx/sites-enabled/
sudo nginx -t

# Get SSL certificate
sudo certbot --nginx -d your-domain.com

# Restart Nginx
sudo systemctl restart nginx
⁠3. Start Application
# Start services
docker-compose up -d

# Generate keys
docker-compose exec app npm run generate-keys
# Add keys to .env

# Restart
docker-compose restart app

# Register public key
docker-compose exec app npm run register-key

# Verify
curl https://your-domain.com/health
⁠Available Commands
# View logs
docker-compose logs -f app

# Shell access
docker-compose exec app sh

# Database access
docker-compose exec postgres psql -U whatsapp_flow -d whatsapp_flows

# Run migrations manually
docker-compose exec app npm run migrate

# Backup database
docker-compose exec postgres pg_dump -U whatsapp_flow whatsapp_flows > backup.sql

# Stop services
docker-compose down

β πŸ”§ Configuration

⁠Environment Variables
VariableDescriptionRequiredExample
NODE_ENVEnvironment modeβœ…production
PORTServer portβœ…3000
DATABASE_URLPostgreSQL connection stringβœ…postgresql://user:pass@localhost:5432/db
PRIVATE_KEYRSA private key (PEM format)βœ…-----BEGIN RSA PRIVATE KEY-----\n...
PUBLIC_KEYRSA public key (PEM format)βœ…-----BEGIN PUBLIC KEY-----\n...
META_APP_SECRETMeta app secretβœ…your_app_secret
META_VERIFY_TOKENWebhook verify tokenβœ…your_verify_token
META_ACCESS_TOKENWhatsApp API access tokenβœ…EAAB...
CALLBACK_WEBHOOK_URLYour callback URL for completed flowsβœ…https://yourapi.com/webhook
DEFAULT_FLOW_NAMEDefault flow when not specified⚠️csat-feedback
FLOW_ENDPOINT_TIMEOUTMax response time (< 3000ms)⚠️2500
CORS_ORIGINSAllowed CORS origins⚠️https://app.com
API_TOKENAPI authentication token⚠️Generated with openssl rand -hex 32

Legend: βœ… Required | ⚠️ Recommended

⁠Key Generation
# Generate RSA-2048 key pair
npm run generate-keys

# Validate keys
npm run validate-keys

# Register public key with Meta
npm run register-key

β πŸ” Security

⁠Encryption Implementation

This server implements WhatsApp's mandatory IV flip pattern for encryption:

// Decryption (incoming from WhatsApp) - Normal IV
const iv = Buffer.from(initialVector, 'base64');
const decipher = crypto.createDecipheriv('aes-128-gcm', aesKey, iv);

// Encryption (outgoing to WhatsApp) - FLIPPED IV ⚠️
const iv = Buffer.from(initialVector, 'base64');
const flippedIV = Buffer.from(iv).reverse(); // Must reverse!
const cipher = crypto.createCipheriv('aes-128-gcm', aesKey, flippedIV);

Critical: If IV is not flipped, WhatsApp returns error 421.

⁠Security Best Practices
  • βœ… Never commit private keys to Git
  • βœ… Use environment variables for all secrets
  • βœ… Rotate tokens every 3-6 months
  • βœ… Enable HTTPS in production (required by WhatsApp)
  • βœ… Validate webhooks with X-Hub-Signature-256
  • βœ… Run as non-root user in Docker
  • βœ… Keep dependencies updated via Dependabot

See SECURITY.md⁠ for vulnerability reporting.


β πŸ›£οΈ API Reference

⁠Flow Endpoint (Data API)

Endpoint: POST /flows/endpoint/:flowName

Handles encrypted interactions during Flow execution.

Actions:

  • ping - Health check
  • INIT - Initialize Flow session
  • data_exchange - Process user input and navigate
  • navigate - Explicit screen navigation
  • complete - Mark Flow as completed

Headers:

Content-Type: application/json

Example Request (INIT):

{
  "version": "3.0",
  "action": "INIT",
  "flow_token": "unique_token_123",
  "encrypted_flow_data": "...",
  "encrypted_aes_key": "...",
  "initial_vector": "..."
}

Response: Encrypted Flow data with next screen

⁠Webhook Endpoint

Endpoint: POST /webhooks/whatsapp

Receives nfm_reply events when user completes Flow.

Headers:

Content-Type: application/json
X-Hub-Signature-256: sha256=...

Example Payload:

{
  "entry": [{
    "changes": [{
      "value": {
        "messages": [{
          "type": "interactive",
          "interactive": {
            "type": "nfm_reply",
            "nfm_reply": {
              "response_json": "{\"flow_token\":\"...\",\"data\":...}",
              "name": "flow_name"
            }
          }
        }]
      }
    }]
  }]
}

Important: response_json is a string, must be parsed with JSON.parse().

⁠Health Check

Endpoint: GET /health

Response:

{
  "status": "ok",
  "timestamp": "2025-01-27T12:00:00.000Z",
  "version": "1.0.0",
  "uptime": 3600,
  "database": "connected"
}
⁠Swagger Documentation

Interactive API docs available at /docs when server is running.


β πŸ“š Documentation

⁠Official WhatsApp Flows Resources

⁠πŸ§ͺ Development

⁠Project Structure
whatsapp-flow/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ domain/              # Business entities and rules
β”‚   β”‚   β”œβ”€β”€ flows/           # Flow, FlowSession entities
β”‚   β”‚   β”œβ”€β”€ encryption/      # Encryption services
β”‚   β”‚   └── webhooks/        # Webhook events
β”‚   β”œβ”€β”€ application/         # Use cases (business logic)
β”‚   β”‚   β”œβ”€β”€ dtos/            # Da

Tag summary

Content type

Image

Digest

sha256:b2006710a…

Size

85 MB

Last updated

11 months ago

docker pull setupautomatizado/whatsapp-flows-server