Send Message

Send WhatsApp Message API

Send a text message to any valid WhatsApp number using your connected WhatsApp session.
This endpoint is part of the Baileys REST Mongo API and is secured via Hybrid Authentication, meaning it accepts both API Keys and JWT tokens.

Internally, this API uses the Baileys WhatsApp Web library to deliver messages and automatically triggers configured webhook updates (for delivery confirmation, errors, etc.).

πŸ”— Endpoint

POST https://api.walytic.com/api/whatsapp/:sessionId/send

πŸ” Required Headers

Header

Type

Required

Description

x-api-key

String

βœ… Yes

Your API key (for external API integrations).

Authorization

String

Optional

JWT Bearer token (for logged-in users via dashboard).

Content-Type

String

βœ… Yes

Must be application/json.

πŸ“€ Required Fields in Request (Body)

Field

Type

Description

number

String

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

message

String

The text message content to send.

⚠️ The WhatsApp number must be active and connected through a valid session (created using /api/sessions/start/:number).

🧩 Example Request (Node.js)

const axios = require("axios");

async function sendWhatsAppMessage() {
  try {
    const response = await axios.post(
      "https://api.walytic.com/api/whatsapp/session-919876543210/send",
      {
        number: "919812345678",
        message: "Hello from Walytic API!"
      },
      {
        headers: {
          "x-api-key": "YOUR_API_KEY",
          "Content-Type": "application/json"
        }
      }
    );

    if (response.data.success) {
      console.log("βœ… Message sent successfully!");
      console.log(response.data);
    } else {
      console.log("❌ Error:", response.data.error);
    }
  } catch (error) {
    console.error("Request failed:", error.response?.data || error.message);
  }
}

sendWhatsAppMessage();

🧾 Example Request (cURL)

$curl -X POST https://api.walytic.com/api/whatsapp/session-919876543210/send \

-H "x-api-key: YOUR_API_KEY" \

-H "Content-Type: application/json" \

-d '{

  "number": "919812345678",

  "message": "Hello from Walytic API!"

}'

πŸ“¦ Example Response (βœ… Success)

{
  "success": true,
  "message": "Text message sent",
  "data": {
    "messageId": "BAE1E57D7A3B7F3C",
    "to": "919812345678",
    "sessionId": "session-919876543210",
    "timestamp": 1734902336
  }
}

❌ Example Response (Error)

{
  "success": false,
  "error": "Number 918888888888 is not on WhatsApp"
}

πŸ“‘ Webhook Event (Delivery Update Example)

Once the message is successfully delivered or fails, your configured webhook_url will automatically receive a payload:

βœ… Message Sent Event

{
  "event": "message_sent",
  "sessionId": "session-919876543210",
  "to": "919812345678",
  "message": "Hello from Walytic API!",
  "status": "sent",
  "timestamp": 1734902336
}

πŸ”΄ Message Failed Event

{
  "event": "message_failed",
  "sessionId": "session-919876543210",
  "to": "919812345678",
  "error": "Recipient not found on WhatsApp"
}

🧠 Internal Workflow (For Developers)

  1. The request hits the WhatsApp Controller:
    controllers/whatsappController.js β†’ sendMessage().

  2. The system retrieves the active Baileys socket from sessionManager.

  3. A message is sent using:

    sock.sendMessage(`${number}@s.whatsapp.net`, { text: message });
    
  4. Message status is saved to the database (Message model).

  5. If a webhook is configured, it’s triggered asynchronously with message metadata.


πŸ”’ Authentication Flow

  • External Clients β†’ use x-api-key header (issued from Walytic dashboard).

  • Internal Dashboard Users β†’ use Authorization: Bearer <JWT_TOKEN>.

  • Middleware (hybridAuth) automatically validates either authentication method.


βš™οΈ Notes

  • Works only if the WhatsApp session is connected (use /api/whatsapp/:sessionId/status to check).

  • Supports multiple sessions per account (multi-number ready).

  • Text messages only β€” for media, use /send-media.

  • Uses Baileys for WhatsApp Web protocol handling.