Simple WhatsApp bot built with Express.js, featuring clean architecture and comprehensive API endpoints.
- π Installation
- π Authentication
- β Adding a New Command
- π API Documentation
- π§ API Parameters
- π Documentation
- π€ Contributing
- π License
-
Clone the repository:
git clone https://github.com/nestorzamili/whatsapp-web-js.git cd whatsapp-web-js
-
Install dependencies:
npm install
-
Create
.env
file:PORT=3000 API_KEY=your_api_key_here
-
Start the application:
npm start
-
Scan the QR code with your WhatsApp to authenticate
All API endpoints require authentication via the x-api-key
header.
node -e "console.log('whatsapp_' + require('crypto').randomBytes(32).toString('hex'))"
Or use OpenSSL:
echo "whatsapp_$(openssl rand -hex 32)"
To add a new command, edit the utils/commands.js
file and add an entry to the COMMANDS
object.
command_name: {
type: 'simple' | 'script' | 'script_with_param',
enabled: true, // enable or disable the command
groupOnly: false, // true if only for groups
allowedGroups: [], // array of allowed group IDs (or empty for all)
pattern: '!commandname', // message prefix recognized as a command
exact: true/false, // true: must match exactly, false: can have parameters after pattern
reply: 'Reply text', // only for type: 'simple'
script: 'script command', // only for type: 'script' or 'script_with_param'
cwd: './scripts', // working directory for script (optional)
successMessage: 'Message if success',
errorMessage: 'Message if error',
noDataMessage: 'Message if no data',
},
hello: {
type: 'simple',
enabled: true,
groupOnly: false,
allowedGroups: [],
pattern: '!hello',
exact: true,
reply: 'Hello there!',
},
search: {
type: 'script_with_param',
script: 'node search_data.js',
cwd: './scripts',
enabled: true,
groupOnly: true,
allowedGroups: ['[email protected]'],
pattern: '!search:', // message must start with !search:
exact: false,
successMessage: 'Search results sent to',
errorMessage: 'Error executing search.',
noDataMessage: 'No search results found.',
},
Tips:
- For commands with parameters, use
exact: false
and set the pattern as needed (!command
or!command:
)
All endpoints require the x-api-key
header for authentication.
All API responses follow a consistent JSON format:
Success Response:
{
"status": "success",
"message": "Operation completed successfully",
"data": {
// Response data here
}
}
Error Response:
{
"status": "error",
"message": "Error description",
"error": {
"code": "ERROR_CODE",
"details": "Additional error details"
}
}
The API supports two content types for flexible usage:
- application/json - For text messages and file paths only
- multipart/form-data - For file uploads with optional text and file paths
Endpoint: GET /
Response:
{
"status": "success",
"message": "Server is running healthy",
"data": {
"status": "healthy",
"uptime": "1h 0m 0s"
}
}
Endpoint: POST /send-message
curl -X POST http://localhost:3000/send-message \
-H "x-api-key: your_api_key" \
-H "Content-Type: application/json" \
-d '{
"id": ["[email protected]"],
"message": "Hello World!"
}'
curl -X POST http://localhost:3000/send-message \
-H "x-api-key: your_api_key" \
-H "Content-Type: application/json" \
-d '{
"id": ["[email protected]", "[email protected]", "[email protected]"],
"message": "Hello everyone!"
}'
curl -X POST http://localhost:3000/send-message \
-H "x-api-key: your_api_key" \
-H "Content-Type: application/json" \
-d '{
"id": ["[email protected]"],
"message": "File from server",
"filePaths": ["/home/user/documents/file.pdf", "/path/to/image.jpg"]
}'
curl -X POST http://localhost:3000/send-message \
-H "x-api-key: your_api_key" \
-F "id[][email protected]" \
-F "message=Check this file" \
-F "files=@/path/to/your/file.jpg"
curl -X POST http://localhost:3000/send-message \
-H "x-api-key: your_api_key" \
-F "id[][email protected]" \
-F "id[][email protected]" \
-F "message=Multiple files example" \
-F "files=@/local/upload/file1.jpg" \
-F "filePaths[]=/server/path/file2.pdf" \
-F "filePaths[]=/server/path/file3.docx"
Response:
{
"status": "success",
"message": "Sent to all recipients",
"data": {
"total": 2,
"success": 2,
"failed": 0
}
}
Error Response (Partial Success):
{
"status": "success",
"message": "Sent to 1/2 recipients",
"data": {
"total": 2,
"success": 1,
"failed": 1,
"errors": [
{
"id": "[email protected]",
"error": "Chat not found"
}
]
}
}
- Images: jpg, jpeg, png, gif, webp
- Videos: mp4, avi, mov, 3gp
- Audio: mp3, wav, ogg, aac, m4a
- Documents: pdf, doc, docx, xls, xlsx, ppt, pptx, txt
Endpoint: GET /get-group-id?groupName=YourGroupName
Response:
{
"status": "success",
"message": "Group found successfully",
"data": {
"groupName": "YourGroupName",
"groupId": "[email protected]"
}
}
Parameter | Type | Required | Description | Content-Type |
---|---|---|---|---|
id |
array | β | Array of WhatsApp IDs | Both |
message |
string | β | Text message content | Both |
filePaths |
array | β | Array of server file paths | Both |
files |
file(s) | β | Upload file(s) via form-data | multipart only |
*At least one of: message
, files
, or filePaths
is required.
-
Use JSON (
application/json
) for:- Text-only messages
- Server-side file paths
- API integrations
- Lightweight requests
-
Use Form-Data (
multipart/form-data
) for:- File uploads from client
- Mixed file sources (upload + paths)
- Web form submissions
We welcome contributions! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
This project is licensed under the Apache-2.0 License. See the LICENSE
file for details.
Built with β€οΈ using modern JavaScript and clean architecture principles.