Send Message to Group

πŸ’¬ Send WhatsApp Group Message API

Send a text message to any WhatsApp group that your connected WhatsApp session has already joined.
This endpoint is part of the Baileys REST Mongo API and supports Hybrid Authentication β€” it accepts both x-api-key (for external clients) and JWT Bearer tokens (for internal users).

Internally, the API uses the Baileys WhatsApp Web library to deliver group messages and automatically triggers webhook events for delivery updates, errors, or group notifications.


πŸ”— Endpoint

GET https://api.walytic.com/api/whatsapp/:sessionId/groups β†’ get all groups joined by that session
POST https://api.walytic.com/api/whatsapp/:sessionId/send-group

πŸ” Required Headers

Header

Type

Required

Description

x-api-key

String

βœ… Yes

Your Walytic API key (for external integrations).

Authorization

String

Optional

JWT Bearer token for dashboard-authenticated users.

Content-Type

String

βœ… Yes

Must be application/json.


πŸ“€ Required Fields (Request Body)

Field

Type

Required

Description

groupName

String

βœ… Yes

The exact WhatsApp group name (must be already joined by this session).

message

String

βœ… Yes

The text message you want to send to the group.


⚠️ Important Notes

  • The group name must match exactly (case-insensitive) with the group your WhatsApp session has joined.
    Example: "Test Group" and "test group" are considered the same.

  • The WhatsApp session must be active and connected.

  • Each group message counts as 1 message against your subscription quota.


🧩 Example Request (Node.js)

const axios = require("axios");

async function sendGroupMessage() {
  try {
    const response = await axios.post(
      "https://api.walytic.com/api/whatsapp/session-919876543210/send-group",
      {
        groupName: "My Team Group",
        message: "Hello everyone! πŸ‘‹ This is an automated group message."
      },
      {
        headers: {
          "x-api-key": "YOUR_API_KEY",
          "Content-Type": "application/json"
        }
      }
    );

    if (response.data.success) {
      console.log("βœ… Group 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);
  }
}

sendGroupMessage();

🧾 Example Request (cURL)

$curl -X POST https://api.walytic.com/api/whatsapp/session-919876543210/send-group \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
  "groupName": "My Team Group",
  "message": "Hello everyone! πŸ‘‹ This is an automated group message."
}'

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

{
  "success": true,
  "message": "Message sent to group 'My Team Group'"
}

❌ Example Response (Error)

Group not found:

{
  "success": false,
  "error": "Group 'My Team Group' not found or not joined by this number."
}

Session disconnected:

{
  "success": false,
  "error": "Session not found or not connected"
}

πŸ“‘ Webhook Events

When the group message is sent or fails, your configured webhook URL will automatically receive an event.

βœ… Group Message Sent Event

{
  "event": "group_message_sent",
  "sessionId": "session-919876543210",
  "groupName": "My Team Group",
  "groupId": "[email protected]",
  "message": "Hello everyone! πŸ‘‹ This is an automated group message.",
  "status": "sent",
  "timestamp": 1734902336
}

πŸ”΄ Group Message Failed Event

{
  "event": "group_message_failed",
  "sessionId": "session-919876543210",
  "groupName": "My Team Group",
  "error": "Group not found or session disconnected",
  "timestamp": 1734902340
}

🧠 Internal Workflow (For Developers)

  1. The request is handled by
    controllers/whatsappController.js β†’ sendGroupMessage().

  2. The system verifies session ownership and quota.

  3. The Baileys socket fetches all joined groups:

    const groups = await sock.groupFetchAllParticipating();
    
  4. It locates the target group by name:

    const targetGroup = Object.values(groups).find(
      g => g.subject.toLowerCase() === groupName.toLowerCase()
    );
    
  5. The message is sent using:

    sock.sendMessage(targetGroup.id, { text: message });
    
  6. Message details are stored in MongoDB (Message model).

  7. A webhook (groupMessageSent or groupMessageFailed) is triggered.


πŸ”’ Authentication Flow

Type

Header

Description

API Key

x-api-key

Used by external integrations or partners.

JWT Token

Authorization: Bearer <token>

Used by internal logged-in dashboard users.

The Hybrid Auth middleware automatically validates both and sets req.userId for session ownership and quota tracking.


βš™οΈ Notes

  • Requires a connected WhatsApp session.
    Check status with /api/whatsapp/:sessionId/status.

  • Group name matching is case-insensitive.

  • If you need to list all available groups, use:

    GET /api/whatsapp/:sessionId/groups
    
  • Each message to a group consumes 1 message from the user’s quota (enforced by enforceFeatureQuota('message', 1)).

  • Supports multiple sessions for multi-number accounts.