What's app Connection

πŸ’¬ 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

client_id

Integer

Your unique client ID (found on API Keys page).

api_key

String

Your API key for authentication.

whatsapp_number

String

WhatsApp number with country code (e.g., 919876543210).

webhook_url

String

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

status

Integer

1 if WhatsApp client session created successfully, 0 if error.

message

String

Response message describing result (e.g., "Scan QR to connect").

qr

String

Base64-encoded QR image or URL to scan with WhatsApp.

whatsapp_client_id

String

Unique ID for this WhatsApp client session.

number

String

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/:number request.
    This will generate a new QR for re-login.