Skip to content

How to find out the number of messages sent from an instance?#

Counting the number of messages sent from an instance can be implemented in the following ways:

  1. Via incoming notifications
  2. Via the outgoing messages journal

1. Via incoming notifications#

This option requires creating a counter on the user's integration side for each instance. Messages sent from an instance can be tracked using two types of notifications:

  1. The incoming webhook of type outgoingAPIMessageReceived only includes messages that were sent via the API.

    Webhook example:

    {
        "typeWebhook": "outgoingAPIMessageReceived", // webhook type - notification on sent message
        "instanceData": {
            "idInstance": 1101111111,
            "wid": "79876543210@c.us",
            "typeInstance": "whatsapp"
        },
            "timestamp": 1727698517,
            "idMessage": "3EB0608D6A2901063D63",
            "senderData": {
                "chatId": "79001234567@c.us",
                "sender": "79876543210@c.us",
                "chatName": "John",
                "senderName": "John",
                "senderContactName": "John Doe"
            },
            "messageData":{
        // Depend on typeMessage = textMessage || imageMessage || videoMessage || documentMessage || audioMessage || locationMessage || contactMessage || extendedTextMessage || pollMessage || editedMessage || deletedMessage 
        ...
        }
    }
    
    2. In the incoming webhook for the outgoing message status outgoingMessageStatus, you need to check that:

    • the sendByApi field is true (indicates the message was sent via API)
    • the status field is sent (to track that the message was sent)

      If you need to track delivered messages specifically, check that the status field is set to delivered. This status means the message has reached the recipient's phone.

    Webhook example:

    {
    "typeWebhook": "outgoingMessageStatus", // webhook type - notification on sent message
            "chatId": "79876543210@c.us",
            "instanceData": {
                "idInstance": 1101111111,
                "wid": "77654321098@c.us",
                "typeInstance": "whatsapp"
            },
            "timestamp": 1727698517,
            "idMessage": "3EB0608D6A2901063D63",
            "status": "delivered", // message status
            "sendByApi": true // field indicating sending a message from the API
    }
    

2. Via the sent messages journal#

This option involves using the lastOutgoingMessages method.
The time in minutes can be specified in the request parameters. After receiving the list of messages from this method, it is necessary to count the messages that have:

  • sendByApi field with value true (indicating that the message was sent from the API)
  • status field with value delivered/read, to track that the message was delivered or read on the device.

lastOutgoingMessages responce example:

[
    {
        "type": "outgoing",
        "idMessage": "BAE5143000000000",
        "timestamp": 1706761454,
        "typeMessage": "textMessage",
        "chatId": "79876543210@c.us",
        "textMessage": "text",
        "statusMessage": "delivered", // Sent message status
        "sendByApi": true // Message sent from API
    },
        {
        "type": "outgoing",
        "idMessage": "BAE5143000000000",
        "timestamp": 1706761345,
        "typeMessage": "textMessage",
        "chatId": "79876543210@c.us",
        "textMessage": "text",
        "statusMessage": "read", // Sent message status
        "sendByApi": true // Message sent from API
    },
        {
        "type": "outgoing",
        "idMessage": "BAE5143000000000",
        "timestamp": 1706761256,
        "typeMessage": "textMessage",
        "chatId": "79876543210@c.us",
        "textMessage": "text",
        "statusMessage": "sent", // Sent message status
        "sendByApi": true // Message sent from API
    },
        {
        "type": "outgoing",
        "idMessage": "BAE5143000000000",
        "timestamp": 1706761234,
        "typeMessage": "textMessage",
        "chatId": "79876543210@c.us",
        "textMessage": "text",
        "statusMessage": "sent",
        "sendByApi": false // Message NOT sent from API
    },
]