Simple Whatsapp API - Just connect with QR and start sending messages instantly.
1.2K
A multi-session, easy-to-deploy API that allows you to send text messages and attachments from multiple WhatsApp accounts with minimal setup.
git clone https://github.com/Codegres-com/Simple-Whatsapp-API.git
cd Simple-Whatsapp-API
npm install
Create a .env file in the root of the project and add your Master API Key. You can invent any secret strings for the keys.
# .env
# This key protects the entire server. All API requests must include it.
MASTER_API_KEY=your_global_master_key_here
npm start
You can also run this application using Docker and Docker Compose for a more isolated and reproducible setup.
This is the easiest way to get started.
Create an Environment File:
Create a .env file in the root of the project. This file will be used by Docker Compose to set the environment variables inside the container.
# .env
# The master key to protect the server
MASTER_API_KEY=yoursecretkey
# The port to run the server on (optional, defaults to 3000)
PORT=3000
Build and Run the Container: Run the following command to build the Docker image and start the container in the background:
docker compose up --build -d
The server will now be running on the port you specified (or the default, 3000).
To Stop the Server:
docker compose down
If you prefer not to use Docker Compose, you can build and run the container manually.
Build the Docker Image:
docker build -t whatsapp-api .
Run the Docker Container:
You must pass the MASTER_API_KEY and map the port. You also need to create and mount volumes to persist the sessions and uploads data.
docker run -d \
-p 3000:3000 \
-e MASTER_API_KEY="yoursecretkey" \
-e PORT="3000" \
--name whatsapp-api-container \
-v whatsapp_sessions:/usr/src/app/sessions \
-v whatsapp_uploads:/usr/src/app/uploads \
whatsapp-api
-d: Run in detached mode.-p: Map port 3000 on your host to port 3000 in the container.-e: Set environment variables.--name: Assign a name to the container.-v: Mount named volumes to persist data.All endpoints are prefixed with /api.
This API uses a two-key system for security and session management:
Master Key (X-MASTER-KEY):
.env file.Session Key (X-API-KEY):
user1_phone, work_account, a random hash, etc.).X-API-KEY is used with the /connect endpoint, a new session will be created for it.To use a WhatsApp account, you must first connect it to a session key.
Choose a unique X-API-KEY for the account you want to connect (e.g., my-personal-whatsapp).
Make a request to one of the connection endpoints with both the master key and your chosen session key. The server will generate a QR code for that specific session.
/api/connect: Returns the QR code as a string./api/connect/image: Returns the QR code as a PNG image.Open WhatsApp on your phone, go to Settings > Linked Devices, and scan the QR code.
Once connected, the server will save the session data in the ./sessions folder. You won't need to scan the code again for this session unless you log out. Repeat this process for each WhatsApp account you want to use, assigning a different X-API-KEY to each.
GET /connectX-MASTER-KEY: your_global_master_key_hereX-API-KEY: your_unique_session_key200 OK with the QR string in the body.200 OK
{
"sessionId": "your_unique_session_key",
"status": "Connected"
}
GET /connect/imageX-MASTER-KEY: your_global_master_key_hereX-API-KEY: your_unique_session_key200 OK with Content-Type: image/png.POST /upload/send or /send-attachment (URL method) endpoints.X-MASTER-KEY: your_global_master_key_heremultipart/form-data with a single field named file.{
"message": "File uploaded successfully.",
"url": "http://localhost:3000/uploads/1678886400000-123456789.jpg"
}
curl Request:
curl -X POST http://localhost:3000/api/upload \
-H "X-MASTER-KEY: your_global_master_key_here" \
-F "file=@/path/to/your/image.jpg"
GET /sendX-MASTER-KEY: your_global_master_key_hereX-API-KEY: your_unique_session_keynumber: The recipient's phone number (e.g., +1234567890).message: The text message to send.attachmentUrl (optional): A URL to a file to send as an attachment. The message will be used as the caption.curl Request:
curl "http://localhost:3000/api/send?number=+1234567890&message=Hello&attachmentUrl=http://localhost:3000/uploads/file.jpg" \
-H "X-MASTER-KEY: your_global_master_key_here" \
-H "X-API-KEY: your_unique_session_key"
POST /send-messageX-MASTER-KEY: your_global_master_key_hereX-API-KEY: your_unique_session_keyapplication/json
{
"to": "+1234567890",
"message": "Hello from the API!"
}
POST /send-attachmentX-MASTER-KEY: your_global_master_key_hereX-API-KEY: your_unique_session_keymultipart/form-datato: The recipient's phone number.file: The file to be sent.caption (optional): A caption for the file.curl Request:
curl -X POST http://localhost:3000/api/send-attachment \
-H "X-MASTER-KEY: your_global_master_key_here" \
-H "X-API-KEY: your_unique_session_key" \
-F "to=+1234567890" \
-F "file=@/path/to/your/document.pdf" \
-F "caption=Here is the document you requested."
application/json{
"to": "+1234567890",
"file": "url_or_base64_string",
"type": "image/png", // Required only for Base64
"caption": "Optional caption"
}
file is a URL, the server will download it.file is a Base64 string, you must provide the correct type (MIME type).curl Request (URL):
curl -X POST http://localhost:3000/api/send-attachment \
-H "Content-Type: application/json" \
-H "X-MASTER-KEY: your_global_master_key_here" \
-H "X-API-KEY: your_unique_session_key" \
-d '{"to": "+1234567890", "file": "https://i.imgur.com/some-image.jpeg", "caption": "From a URL"}'
whatsapp-web.js), which may have a risk of your number being banned by WhatsApp if used for spamming. Use responsibly.Content type
Image
Digest
sha256:bd528791c…
Size
771 MB
Last updated
about 1 year ago
docker pull codegres/simple-whatsapp-api