meta-cloud-api-express-example
v1.0.0
Published
Example Express app using the meta-cloud-api package
Readme
WhatsApp API Example Server
This is a Express.js example server that demonstrates how to use the meta-cloud-api package to handle WhatsApp Cloud API webhooks and send messages.
Setup
Clone this repository
Install dependencies:
npm installCreate a
.envfile based on theenv.examplefile and fill in your WhatsApp API credentials:cp env.example .envStart the development server:
npm run dev
Project Structure
The project follows a clean architecture:
src/
├── server.ts # Main entry point
├── routes/
│ └── index.ts # Route definitions
└── handlers/
└── webhookHandler.ts # All message handlers in one placeFeatures
This example demonstrates various WhatsApp API features:
- Command-based responses (
hello,help,info, etc.) - Interactive button messages
- Template message support (requires approved template for your WhatsApp business account)
- Handling of different message types (text, image, document, interactive)
- Pre-processing of messages
- Clean architecture separating concerns
Endpoints
GET /: Simple homepage showing the server is runningGET /webhook: WhatsApp webhook verification endpointPOST /webhook: WhatsApp webhook for receiving messages and events
Webhook Setup
To receive messages from WhatsApp, you need to make your webhook publicly accessible. You can use a tool like ngrok for local development:
ngrok http 3000Then set up your webhook in the Meta Developer Dashboard:
- Go to Webhook Setup
- Set the webhook URL to your ngrok URL + "/webhook" (e.g.,
https://your-ngrok-url.ngrok.io/webhook) - Set the verify token to match your
WEBHOOK_VERIFICATION_TOKENin the.envfile
Environment Variables
PORT: Port for the Express server (default: 3000)CLOUD_API_ACCESS_TOKEN: Your WhatsApp Cloud API access tokenWA_PHONE_NUMBER_ID: Your WhatsApp phone number IDWA_BUSINESS_ACCOUNT_ID: Your WhatsApp business account IDWEBHOOK_VERIFICATION_TOKEN: Verification token for webhook verificationDEBUG: Enable debug mode (true/false)
