A Node.js application that integrates AI21 Studio with WhatsApp Business API to provide AI-powered responses to WhatsApp messages.
π Includes ngrok integration for exposing your local webhook to the internet.
- Docker & Docker Compose
- Node.js 18+ (for local development without Docker)
- WhatsApp Business API account
- AI21 Studio API key
- Clone this repository
- Copy the
.env.example
file to.env
:cp .env.example .env
- Edit the
.env
file with your credentials provided in the next step
- Create an account at AI21 Studio
- Go to your Workspace API keys
- Copy your API key from the "API Key" section
To get the PHONE_NUMBER_ID
and WHATSAPP_TOKEN
:
- Go to Meta for Developers
- Create or select your app (Choose "Business" type)
- Add WhatsApp product to your app
- Go to WhatsApp > Getting Started
- Set up your WhatsApp business account if you haven't already
- Generate and copy your
WHATSAPP_TOKEN
from the "Temporary access token" section - Find your
PHONE_NUMBER_ID
in the "From" field (Number ID, not the number itself) - Configure your webhook URL using the ngrok URL (see "Using ngrok for Webhooks" section below)
This is a custom string that you create yourself. It should be:
- Random and unique
- At least 32 characters long
- Kept secret and secure
Example of generating a secure token:
# Using OpenSSL (recommended)
openssl rand -hex 32
# Or using Node.js
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
When configuring your webhook in the Meta for Developers dashboard, you'll need to provide this same VERIFY_TOKEN
value.
- Sign up or log in to your ngrok dashboard
- Navigate to the "Your Authtoken" section
- Copy your authentication token
- Add it to your
.env
file asNGROK_AUTHTOKEN
This project includes Docker configuration for easy deployment.
The project includes a Makefile with common commands:
# Build the Docker image
make build
# Run the application
make run
# View logs
make logs
# Stop the application
make stop
# For development with live reloading
make dev
# See all available commands
make help
# Build and start the application
docker-compose up -d
# Stop the application
docker-compose down
# Install dependencies
npm install
# Start development server with hot reloading
npm run dev
# Start production server
node index.js
The project includes ngrok as a Docker service to expose your local webhook to the internet.
-
Make sure you have added your ngrok authentication token to the
.env
file:NGROK_AUTHTOKEN=your_ngrok_auth_token
-
Start the application with ngrok:
make init
-
Get your public webhook URL:
make ngrok-url
-
Use the URL displayed (ending with
/webhook
) to configure your WhatsApp Business API webhook. -
View ngrok logs to debug connections:
make ngrok-logs
βββ src/
β βββ config/ # Configuration settings
β βββ routes/ # API route definitions
β βββ services/ # Business logic
βββ .env # Environment variables (create from .env.example)
βββ .env.example # Example environment file
βββ Dockerfile # Docker configuration
βββ docker-compose.yml # Docker Compose configuration (with ngrok service)
βββ Makefile # Make commands
βββ index.js # Application entry point