A robust, scalable WhatsApp messaging API built with TypeScript, Express, and whatsapp-web.js.
- 🔄 Multi-session support (multiple WhatsApp accounts)
- 📱 QR Code authentication
- 💬 Send text and media messages
- 📊 Session management and status monitoring
- 📚 Complete API documentation with Swagger
- 🔒 File upload validation and security
- 🏗️ Modular, scalable architecture
- 📝 TypeScript for type safety
-
Install dependencies:
npm install
-
Configure environment:
cp .env.example .env # Edit .env with your settings
-
Start development server:
npm run dev
-
Access the API:
- API Base: http://localhost:3001/api
- Documentation: http://localhost:3001/api-docs
- Health Check: http://localhost:3001/api/health
POST /api/sessions
- Create new sessionGET /api/sessions
- List all sessionsGET /api/sessions/:id
- Get session statusPOST /api/sessions/:id/logout
- Logout sessionDELETE /api/sessions/:id
- Delete session
POST /api/sessions/:id/send-text
- Send text messagePOST /api/sessions/:id/send-media
- Send media message
GET /api/health
- Health check
Variable | Description | Default |
---|---|---|
PORT |
Server port | 3001 |
NODE_ENV |
Environment | development |
CORS_ORIGIN |
CORS origin | http://localhost:3000 |
SESSION_PATH |
Session storage path | ./sessions |
MAX_SESSIONS |
Maximum concurrent sessions | 10 |
UPLOAD_PATH |
File upload path | ./uploads |
MAX_FILE_SIZE |
Max file size in bytes | 10485760 (10MB) |
curl -X POST http://localhost:3001/api/sessions \
-H "Content-Type: application/json" \
-d '{"sessionId": "my-session"}'
curl -X POST http://localhost:3001/api/sessions/my-session/send-text \
-H "Content-Type: application/json" \
-d '{
"to": "1234567890",
"message": "Hello from WhatsApp API!"
}'
curl -X POST http://localhost:3001/api/sessions/my-session/send-media \
-F "to=1234567890" \
-F "caption=Check this image!" \
-F "file=@/path/to/image.jpg"
src/
├── config/ # Configuration files
├── controllers/ # Route controllers
├── middlewares/ # Express middlewares
├── routes/ # Route definitions
├── services/ # Business logic
├── types/ # TypeScript type definitions
├── utils/ # Utility functions
└── index.ts # Application entry point
npm run dev
- Start development server with hot reloadnpm run build
- Build for productionnpm run start
- Start production servernpm run type-check
- Type checking without compilation
-
Build the application:
npm run build
-
Start production server:
npm start
-
Using PM2 (recommended):
pm2 start dist/index.js --name whatsapp-api
- File uploads are validated for type and size
- CORS is configured for specific origins
- Input validation on all endpoints
- Error handling prevents information leakage
- Session isolation between users
-
Puppeteer issues:
- Install missing dependencies:
apt-get install -y chromium-browser
- Use provided Puppeteer args for headless mode
- Install missing dependencies:
-
Permission errors:
- Check file permissions for session and upload directories
- Ensure proper write access
-
Memory issues:
- Monitor session count and cleanup inactive sessions
- Consider implementing session timeout
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT License - see LICENSE file for details