A Node.js WhatsApp Web API using @whiskeysockets/baileys.
- ✅ WhatsApp Web connection with QR code authentication
- ✅ Send text messages to phone numbers
- ✅ Receive and store incoming messages
- ✅ Real-time message streaming via WebSocket (Socket.IO)
- ✅ Message history management
- ✅ Environment variable configuration
- ✅ Docker support
- Node.js 18+
- npm or yarn
- WhatsApp mobile app for QR code scanning
# Clone the repository
git clone <repository-url>
cd whatsapp-api
# Install dependencies
npm install
# Build the project
npm run buildCreate a .env file in the root directory:
# Server Configuration
HOST=127.0.0.1
PORT=3000
# Session Configuration
SESSION_SECRET=your-session-secret-here
# Environment
NODE_ENV=development# Development mode
npm run dev
# Production mode
npm startThe API will be available at http://localhost:3000
GET /status- Get WhatsApp connection statusGET /qr- Get QR code for authenticationPOST /logout- Logout from WhatsApp
GET /message- Message form (web interface)POST /message- Send a message- Body:
{ "phoneNumber": "+1234567890", "message": "Hello!" }
- Body:
GET /messages- Get all received messagesDELETE /messages- Clear all received messagesGET /messages/stream- Stream messages in real-time (SSE)GET /messages/paginated- Get paginated messages- Query params:
page,limit
- Query params:
GET /realtime- Real-time chat monitoring page (WebSocket)GET /api/realtime/status- Get real-time status
The API supports true real-time message streaming using Socket.IO WebSockets.
Client → Server:
clear_messages- Clear all messagesrefresh_messages- Refresh message listget_status- Get current status
Server → Client:
connect- Client connecteddisconnect- Client disconnectedstatus- Initial status on connectionstatus_update- Status updatesexisting_messages- Previously received messagesnew_message- Real-time incoming messagemessages_cleared- Messages cleared confirmationmessages_refreshed- Messages refreshed with data
const socket = io('http://localhost:3000');
socket.on('connect', () => {
console.log('Connected to WebSocket server');
});
socket.on('new_message', (message) => {
console.log('New message:', message);
});
socket.on('status', (status) => {
console.log('Status:', status);
});Use the included test script:
node test-websocket.jsVisit http://localhost:3000/realtime for a live chat monitoring interface featuring:
- Real-time connection status with visual indicators
- Live message streaming with instant updates
- Message type filtering (text, image, video, audio, document)
- Message management (clear, refresh)
- Connection notifications with status updates
- Responsive design for mobile and desktop
- ✅ True real-time - No page refreshes needed
- ✅ Visual status indicators - Connection, phone number, QR code
- ✅ Message animations - New messages slide in smoothly
- ✅ Message categorization - Different colors for message types
- ✅ Auto-scroll - New messages automatically scroll into view
- ✅ Connection notifications - Real-time status updates
- ✅ Mobile responsive - Works on all devices
This API integrates with the MCP (Model Context Protocol) WhatsApp server for AI assistant integration.
# MCP Server Configuration
MCP_HOST=127.0.0.1
MCP_PORT=3002
# WhatsApp API Configuration
WHATSAPP_API_HOST=localhost
WHATSAPP_API_PORT=3000# Build the image
docker build -t whatsapp-api .
# Run the container
docker run -p 3000:3000 \
-e HOST=0.0.0.0 \
-e PORT=3000 \
whatsapp-apidocker-compose up -dsrc/
├── controllers/ # Route handlers
├── services/ # Business logic
├── util/ # Utilities
├── views/ # Pug templates
└── public/ # Static assets
npm run dev- Start development servernpm run build- Build TypeScriptnpm start- Start production servernpm test- Run tests
-
QR Code not appearing
- Restart the service
- Check if WhatsApp Web is already connected on another device
-
Connection lost
- The service will automatically reconnect
- Check your internet connection
-
Messages not received
- Ensure the phone number is in international format
- Check if the contact exists in your WhatsApp
-
WebSocket connection issues
- Check if the server is running on the correct port
- Ensure no firewall is blocking WebSocket connections
- Check browser console for connection errors
Check the console output for detailed logs and error messages.
MIT License