π¬ WhatsApp API β Session Connection & Management
Seamlessly integrate your WhatsApp Business accounts with your applications and automated workflows using the Baileys REST Mongo API.
This API enables you to programmatically connect, manage, and automate WhatsApp sessions with webhook updates and message control.
https://yourdomain.com/api/sessions
π 1. Create a WhatsApp Session (Login / Connect)
Endpoint:
POST https://yourdomain.com/api/sessions/start/:whatsapp_number
Purpose:
Create a new WhatsApp Web session for the given number.
This will generate a QR code to scan with your WhatsApp app and establish a secure session.
πΈ Request Type
POST
πΈ Required Fields in Request
Field |
Type |
Description |
|---|---|---|
|
|
Your unique client ID (found on API Keys page). |
|
|
Your API key for authentication. |
|
|
WhatsApp number with country code (e.g., |
|
|
Your server endpoint where connection events will be sent. |
Example
Use this API to programmatically create a new WhatsApp client connection. The webhook URL will receive status updates about the connection process.
$curl -X POST https://yourdomain.com/api/sessions/start/919876543210 \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"client_id": 12345,
"api_key": "YOUR_API_KEY",
"webhook_url": "https://yourapp.com/webhook"
}'
π§βπ» Example (Node.js using Axios)
const axios = require("axios");
async function connectWhatsApp() {
try {
const response = await axios.post(
"https://yourdomain.com/api/sessions/start/919876543210",
{
client_id: 12345,
api_key: "YOUR_API_KEY",
webhook_url: "https://yourapp.com/webhook"
},
{
headers: { Authorization: "Bearer YOUR_API_KEY" }
}
);
console.log("β Session Response:", response.data);
} catch (error) {
console.error("β Error:", error.response?.data || error.message);
}
}
connectWhatsApp();
π§© Fields in Response
Field |
Type |
Description |
|---|---|---|
|
|
|
|
|
Response message describing result (e.g., "Scan QR to connect"). |
|
|
Base64-encoded QR image or URL to scan with WhatsApp. |
|
|
Unique ID for this WhatsApp client session. |
|
|
WhatsApp number linked to the session. |
π’ Example Successful Response
{
"status": 1,
"message": "Scan the QR code to connect your WhatsApp account",
"qr": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
"whatsapp_client_id": "session-919876543210",
"number": "919876543210"
π΄ Example Error Response
{
"status": 0,
"message": "Invalid API key or missing parameters"
}
β Connection Successful
{
"event": "connected",
"sessionId": "session-919876543210",
"number": "919876543210",
"status": "connected"
}
π 4. Logout / Disconnect Session
Endpoint:
DELETE https://yourdomain.com/api/sessions/session-919876543210
Description:
Terminates the active WhatsApp Web session.
Removes session from memory and deletes authentication files.
Response:
{
"success": true,
"message": "Session disconnected successfully"
}
π§ 5. Check Session Status
Endpoint:
GET https://yourdomain.com/api/whatsapp/:sessionId/status
Response Example:
{
"sessionId": "session-919876543210",
"status": "connected",
"number": "919876543210"
}
βοΈ Technical Notes
Multi-session ready: Each WhatsApp number has its own session stored under
/auth/{sessionId}.Auto reconnect: Sessions reconnect automatically using Baileysβ stored credentials.
Webhooks: Triggered for all important events β connection, disconnection, message receive, and errors.
-
Database-backed: Sessions and messages are persisted in MongoDB for analytics and dashboards.
π‘ Tip
To refresh the session, simply DELETE the existing one and re-initiate the
/start/:numberrequest.
This will generate a new QR for re-login.