A comprehensive Express.js API server for WhatsApp Web integration with student management features, built with whatsapp-web.js
.
- WhatsApp Integration: Full WhatsApp Web API with QR code authentication
- REST API: Send single and bulk messages with media support
- Web Interface: User-friendly web dashboard for QR authentication and messaging
- Student Management: Bulk messaging for educational institutions
- Real-time Updates: WebSocket integration for live QR codes and status updates
- Media Support: Send images, documents, and other media files
- API Authentication: Secure API key-based authentication
- Rate Limiting: Protection against API abuse
- Docker Support: Complete containerization with Docker Compose
- Production Ready: SSL support, Nginx reverse proxy, and security hardening
- Node.js 18+ or Docker
- Chrome/Chromium browser (for WhatsApp Web)
- (Optional) Redis for session storage
Deploy to Appwrite Cloud for serverless, scalable hosting:
-
Install Appwrite CLI:
npm install -g appwrite-cli
-
Configure for Appwrite:
cp .env.appwrite .env # Edit .env with your Appwrite project details
-
Build and Deploy:
npm run build:appwrite appwrite deploy function
π Complete Appwrite Deployment Guide
-
Clone the repository:
git clone <your-repo-url> cd wwebjs-bot
-
Make deployment script executable:
chmod +x deploy.sh
-
Configure environment:
cp .env.example .env # Edit .env with your configuration
-
Deploy the application:
./deploy.sh
-
Install dependencies:
npm install
-
Configure environment:
cp .env.example .env # Edit .env with your settings
-
Start the application:
# Development npm run dev # Production npm start
All API endpoints require an API key in the header:
Authorization: Bearer YOUR_API_KEY
GET /health
GET /api/whatsapp/status
POST /api/whatsapp/send-message
Content-Type: application/json
{
"number": "1234567890",
"message": "Hello from WhatsApp API!"
}
POST /api/whatsapp/send-bulk
Content-Type: application/json
{
"recipients": [
{
"number": "1234567890",
"name": "John Doe",
"message": "Hello John!"
},
{
"number": "0987654321",
"name": "Jane Smith",
"message": "Hello Jane!"
}
]
}
POST /api/whatsapp/send-media
Content-Type: multipart/form-data
number=1234567890
caption=Check this out!
media=[file upload]
GET /api/whatsapp/contacts
Access the web interface at:
- HTTP:
http://localhost
- HTTPS:
https://localhost
Features:
- QR code authentication
- Real-time connection status
- Send individual messages
- Bulk message management
- File upload for media messages
Import the Postman collection from the postman/
directory:
- Open Postman
- Import
postman/WhatsApp_API_Collection.json
- Import environment from
postman/environments/
- Set your API key in the environment variables
# Deploy application
./deploy.sh
# Start development environment
./deploy.sh dev
# View logs
./deploy.sh logs
# Stop services
./deploy.sh stop
# Restart services
./deploy.sh restart
# Clean up (remove all containers and images)
./deploy.sh clean
# Create backup
./deploy.sh backup
Key configuration options in .env
:
# Server
NODE_ENV=production
PORT=3000
API_KEY=your-api-key-here
# WhatsApp
WHATSAPP_SESSION_NAME=default-session
WHATSAPP_HEADLESS=true
# Security
RATE_LIMIT_MAX_REQUESTS=100
SSL_ENABLED=false
# Features
FEATURE_BULK_MESSAGING=true
FEATURE_MEDIA_MESSAGES=true
FEATURE_STUDENT_MANAGEMENT=true
- whatsapp-api: Main application server
- redis: Session storage and caching
- nginx: Reverse proxy with SSL termination
- Functions: Serverless WhatsApp API execution
- Database: Session, message, and contact storage
- Storage: Media file uploads and management
- CDN: Global content delivery
- API key authentication
- Rate limiting protection
- CORS configuration
- Security headers (Helmet.js)
- Input validation and sanitization
- File upload restrictions
- SSL/TLS support
wwebjs-bot/
βββ src/
β βββ config/ # Configuration files
β βββ middleware/ # Express middleware
β βββ routes/ # API route handlers
β βββ services/ # Business logic services
βββ public/ # Static web files
βββ views/ # HTML templates
βββ postman/ # API testing collection
βββ ssl/ # SSL certificates
βββ uploads/ # File uploads
βββ logs/ # Application logs
βββ docker-compose.yml # Production Docker setup
βββ docker-compose.dev.yml # Development Docker setup
βββ Dockerfile # Container configuration
βββ nginx.conf # Nginx configuration
βββ deploy.sh # Deployment script
-
First Time Setup:
./deploy.sh
-
Access the web interface: Navigate to
http://localhost
-
Scan QR Code: Use your phone's WhatsApp to scan the QR code
-
Test API: Use the Postman collection or curl:
curl -X POST http://localhost/api/whatsapp/send-message \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{"number":"1234567890","message":"Hello World!"}'
- Application Logs:
./logs/app.log
- Docker Logs:
docker-compose logs -f
- Health Check:
GET /health
- Status Check:
GET /api/whatsapp/status
- Bulk messaging to student groups
- Contact import/export
- Message templates
- Student data management
- Personalized messaging
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
This project is not officially affiliated with WhatsApp. Use responsibly and in accordance with WhatsApp's Terms of Service.
- Check the logs:
./deploy.sh logs
- Review the health check:
GET /health
- Restart services:
./deploy.sh restart
- Clean deployment:
./deploy.sh clean && ./deploy.sh
Made with β€οΈ for educational institutions and developers
- Rate Limiting - Prevent API abuse
- Input Validation - Comprehensive request validation
- API Key Authentication - Optional API key protection
- CORS Support - Configurable cross-origin requests
- File Upload Validation - Secure media file handling
- Node.js (v14 or higher)
- Google Chrome browser installed
- WhatsApp mobile app
-
Clone or download the project:
git clone <repository-url> cd wwebjs-bot
-
Install dependencies:
npm install
-
Configure environment variables:
cp .env.example .env
Edit
.env
file as needed:PORT=3000 HOST=localhost API_KEY=your-optional-api-key MESSAGE_DELAY=2000 MAX_BULK_MESSAGES=50
-
Start the server:
# Development mode with auto-reload npm run dev # Production mode npm start
- Open your browser and navigate to
http://localhost:3000
- Scan the QR code with your WhatsApp mobile app:
- Open WhatsApp on your phone
- Go to Settings β Linked Devices β Link a Device
- Scan the QR code displayed on the web interface
- Once connected, you can send messages through the web interface
A comprehensive Postman collection is included for API testing:
-
Import Collection:
- Import
postman/WhatsApp-API-Collection.json
- Import
postman/WhatsApp-API-Environment-Dev.json
- Import
-
Configure Environment:
- Set
baseUrl
tohttp://localhost:3000
- Set
apiKey
if using API key authentication - Set
testPhoneNumber
to your test number
- Set
-
Start Testing:
- Begin with "Health Check" and "WhatsApp Status"
- Test message sending with various scenarios
- See
postman/README.md
for detailed instructions
If API_KEY
is set in your environment, include it in requests:
# Header method
curl -H "X-API-Key: your-api-key" ...
# Query parameter method
curl "http://localhost:3000/api/whatsapp/status?apiKey=your-api-key"
curl -X POST http://localhost:3000/api/whatsapp/send \
-H "Content-Type: application/json" \
-d '{
"number": "+201234567890",
"message": "Hello from WhatsApp API!"
}'
curl -X POST http://localhost:3000/api/whatsapp/send-bulk \
-H "Content-Type: application/json" \
-d '{
"contacts": [
{"number": "+201234567890", "name": "Ahmed Ali", "studentId": "2023001", "class": "Grade 10A"},
{"number": "+201234567891", "name": "Sara Mohamed", "studentId": "2023002", "class": "Grade 10A"}
],
"message": "Dear {name}, your student ID {studentId} has been assigned to {class}.",
"options": {"delay": 2000}
}'
curl -X POST http://localhost:3000/api/whatsapp/send-media \
-F "number=+201234567890" \
-F "message=Check out this document!" \
-F "media=@/path/to/your/file.pdf"
curl http://localhost:3000/api/whatsapp/status
curl http://localhost:3000/api/whatsapp/qr
wwebjs-bot/
βββ app.js # Main application entry point
βββ package.json # Dependencies and scripts
βββ .env.example # Environment configuration template
βββ README.md # This file
βββ postman/ # Postman collection and environments
β βββ WhatsApp-API-Collection.json # Postman collection
β βββ WhatsApp-API-Environment-Dev.json # Development environment
β βββ WhatsApp-API-Environment-Prod.json # Production environment
β βββ test-scripts.js # Automated test scripts
β βββ README.md # Postman documentation
βββ src/
β βββ config/
β β βββ config.js # Application configuration
β βββ services/
β β βββ whatsappService.js # WhatsApp client service
β βββ routes/
β β βββ whatsapp.js # API routes
β βββ middleware/
β β βββ auth.js # Authentication middleware
β β βββ validation.js # Request validation
β βββ utils/
β β βββ logger.js # Logging utility
β βββ server.js # Express server setup
βββ public/
β βββ css/
β β βββ styles.css # Web interface styles
β βββ js/
β βββ main.js # Client-side JavaScript
βββ views/
β βββ index.html # Web interface template
βββ uploads/ # Media file uploads (auto-created)
const classAnnouncement = {
"contacts": [
{"number": "+201234567890", "name": "Ahmed Ali", "class": "Grade 10A"},
{"number": "+201234567891", "name": "Sara Mohamed", "class": "Grade 10A"}
],
"message": "Dear {name},\n\nImportant announcement for {class}:\n\nTomorrow's math exam has been postponed to next Friday. Please prepare accordingly.\n\nBest regards,\nSchool Administration"
}
const gradeNotification = {
"contacts": [
{"number": "+201234567890", "name": "Ahmed Ali", "studentId": "2023001", "grade": "A+"},
{"number": "+201234567891", "name": "Sara Mohamed", "studentId": "2023002", "grade": "A"}
],
"message": "Congratulations {name}!\n\nYour final grade for Mathematics is: {grade}\n\nStudent ID: {studentId}\n\nKeep up the excellent work!"
}
const parentNotification = {
"contacts": [
{"number": "+201234567892", "name": "Mohamed Ali", "studentName": "Ahmed Ali", "class": "Grade 10A"},
{"number": "+201234567893", "name": "Fatima Mohamed", "studentName": "Sara Mohamed", "class": "Grade 10A"}
],
"message": "Dear {name},\n\nThis is to inform you that your child {studentName} from {class} will have a parent-teacher meeting scheduled for next Tuesday at 3 PM.\n\nPlease confirm your attendance.\n\nThank you."
}
Variable | Default | Description |
---|---|---|
PORT |
3000 | Server port |
HOST |
localhost | Server host |
API_KEY |
null | Optional API authentication key |
CORS_ORIGIN |
* | CORS allowed origins |
RATE_LIMIT_MAX |
100 | Max requests per window |
MESSAGE_DELAY |
2000 | Delay between bulk messages (ms) |
MAX_BULK_MESSAGES |
50 | Maximum contacts in bulk operation |
CHROME_PATH |
auto-detected | Custom Chrome executable path |
Placeholder | Description |
---|---|
{name} |
Full name |
{firstName} |
First name |
{lastName} |
Last name |
{studentId} |
Student ID number |
{class} |
Class/grade level |
{grade} |
Academic grade |
All API responses follow this format:
{
"success": true,
"data": { ... },
"message": "Operation completed successfully"
}
{
"success": false,
"error": "Error type",
"message": "Detailed error message"
}
200
- Success400
- Bad Request (validation errors)401
- Unauthorized (invalid API key)404
- Not Found429
- Too Many Requests (rate limit exceeded)500
- Internal Server Error
-
QR Code not displaying:
- Check if Chrome is installed and accessible
- Verify Chrome executable path in configuration
- Restart the application
-
Messages not sending:
- Ensure WhatsApp client is connected (green status)
- Check phone number format (include country code)
- Verify rate limits aren't exceeded
-
Authentication failures:
- Clear browser cache and cookies
- Restart the application
- Re-scan QR code
-
File upload issues:
- Check file size (max 10MB by default)
- Verify file type is supported
- Ensure uploads directory has write permissions
npm run dev
- Create feature branch
- Add routes in
src/routes/
- Update service in
src/services/whatsappService.js
- Add validation in
src/middleware/validation.js
- Update web interface if needed
- Test thoroughly
Logs are automatically written to logs/
directory with daily rotation.
- Always use HTTPS in production
- Set strong API keys
- Configure CORS properly
- Implement rate limiting
- Validate all inputs
- Keep dependencies updated
- Monitor logs for suspicious activity
This project is licensed under the ISC License.
For issues and questions:
- Check the troubleshooting section
- Review API documentation
- Check browser console for errors
- Verify network connectivity
- Built with whatsapp-web.js
- UI powered by Bootstrap 5
- Real-time updates via Socket.IO