A RESTful API for sending WhatsApp messages with bearer token authentication using the whatsapp-web.js library.
- Node.js (v14 or higher)
- NPM
- Clone the repository
- Install dependencies:
npm install - Create a
.envfile with the following variables:PORT=3000 API_TOKEN=your_example_token
npm start
For development with automatic restart:
npm run dev
On first launch, a QR code will be generated in the terminal that you'll need to scan with WhatsApp on your phone for authentication.
-
Build the Docker image:
docker build -t whatsapp-api . -
Run the container:
docker run -p 3000:3000 -e API_TOKEN=your-secure-token --name whatsapp-api -v whatsapp-auth:/app/.wwebjs_auth whatsapp-api
- Customize the environment variables in
docker-compose.ymlif needed - Start the services:
docker-compose up -d - View logs to scan the QR code when first starting:
docker-compose logs -f
The session data is persisted in a Docker volume, so you won't need to scan the QR code again after restarts.
The API automatically maintains the WhatsApp session even after server restart:
- The session is saved in the
.wwebjs_authfolder - You don't need to scan the QR code every time you restart the server
- To manually disconnect, a specific endpoint is available (
/api/logout)
To avoid WhatsApp bans, the API implements a queue system that limits message sending:
- Maximum one message every 30-60 seconds (random wait time)
- Messages are queued and sent automatically respecting these limits
- API responses include information about the queue and estimated wait times
GET /api/status
Required header:
Authorization: Bearer <token-from-env-file>
Response: Service status including queue and WhatsApp connection information.
POST /api/send
Required header:
Authorization: Bearer <token-from-env-file>
Content-Type: application/json
Body:
{
"number": "391234567890",
"message": "Hi, this is a test message",
"options": {} // Additional options (optional)
}Note:
- The number must be in international format without '+' or other special characters
- The message will be queued and sent respecting the time limits
- The response will include information about the estimated wait time
GET /api/queue
Required header:
Authorization: Bearer <token-from-env-file>
Response: Information about the message queue, including the number of messages waiting.
POST /api/logout
Required header:
Authorization: Bearer <token-from-env-file>
Response: Confirmation of disconnection from WhatsApp. You'll need to scan the QR code again to reconnect.
curl -X GET http://localhost:3000/api/status \
-H "Authorization: Bearer <token-from-env-file>"
curl -X POST http://localhost:3000/api/send \
-H "Authorization: Bearer <token-from-env-file>" \
-H "Content-Type: application/json" \
-d '{"number":"391234567890","message":"Test message"}'
curl -X GET http://localhost:3000/api/queue \
-H "Authorization: Bearer <token-from-env-file>"
curl -X POST http://localhost:3000/api/logout \
-H "Authorization: Bearer <token-from-env-file>"