For the complete documentation index, see llms.txt. This page is also available as Markdown.

Detect New Messages

To notify your system when a conversation has new messages (for example, to display notifications or alert human support agents), use Webhook delivery first; if polling is needed, read only updated co

To display a “new message” notification in your system or notify human support agents when a customer sends a message, choose an approach in this order:

  1. Use Webhook delivery first: The conversation platform’s message Webhook sends one notification per message, with no polling required (see Use Webhooks Instead of Polling).

  2. If you only need human handoff notifications: Use human handoff Webhook events, which are sent only when the handoff workflow changes state.

  3. If polling is required: Follow the recommended polling approach to find only updated conversations and read only the first page of each conversation’s message list.

Use Webhooks Instead of Polling

Requirement
Recommended approach
When notifications are sent

Receive a notification for every customer message

Use the conversation platform’s message Webhook with type Incoming

For every customer (contact) message

Synchronize replies sent by AI or support agents

Use the conversation platform’s message Webhook with type Outgoing

For every reply sent to the customer’s conversation platform

Receive a notification when a conversation needs a human agent

Only for human handoff, agent assignment, return to AI, or queue timeout; not for every message

Configuration path: Admin console → Customer Conversations → Conversation Platforms → select a conversation platform → Webhook Settings → Add Webhook. Set Type to Incoming or Outgoing (although the admin console describes Incoming as “Receive external requests,” it actually sends each customer message to your configured URL; Receive is the type that receives external messages). Set Payload Format to MaiAgent Standard (MaiAgent’s standard format). See Message Webhook for payload contents, source verification, and retry behavior.

The WhatsApp conversation platform’s settings page currently has no Webhook Settings tab. Use the polling approach below instead.

The two Webhook types are configured independently and can be enabled together; deduplicate their payloads using the message or event id. Message Webhooks automatically retry up to 3 times on timeouts, connection failures, 5xx, or 429 responses (the same message may arrive more than once). If all 3 retries fail, delivery stops; delivery is not guaranteed. The polling approach below can check for missed customer messages (Incoming) at a low frequency. If delivery of an AI or support agent reply (Outgoing) fails, the polling example may not retrieve it. Check the Webhook delivery logs for failed deliveries instead (see Message Webhook → Responses and Retries).

Recommended Polling Approach

If you cannot receive Webhooks (for example, because the receiving endpoint cannot be exposed publicly), poll using the following two APIs together.

1. Find Updated Conversations

The conversation list is always sorted by last message time, newest first (lastMessageCreatedAt in the response). For each conversation, record the last observed lastMessageCreatedAt and the last message’s lastMessage.id. A change to either means the conversation has been updated (timestamps have second precision, so if two messages arrive in the same second, only the id changes). In each polling round, read from the first entry down and stop when you reach a conversation older than “the latest time observed so far minus 10 minutes.” Usually, you only need the first page. Follow the response’s next to the next page only if more than page_size conversations were updated within that window.

The extra 10-minute lookback is necessary because, in API conversations, a conversation’s lastMessageCreatedAt may be written only after the AI reply finishes generating. The value is still the customer message’s timestamp, so it may be older than other conversations you saw in the previous round. Comparing only whether it is newer than the previous round’s latest time would miss these conversations.

2. Read Only the First Page of Messages for Each Updated Conversation

The first page of the message list contains the latest messages: the system selects the latest page_size messages, then sorts them from oldest to newest within the page. The last message is therefore the latest in the entire conversation. The response’s next points to older messages; do not read back through the history in every round when detecting new messages.

Use each message’s id to determine whether it has been processed: unprocessed id values on the first page identify new messages. Pagination is needed in only one case—when all messages on the first page are unprocessed (more than page_size messages arrived between rounds). Only then follow next to older pages until you encounter a processed message.

3. Polling Interval and Page Size

Setting
Recommendation
Description

Polling interval

30–60 seconds

Use Webhooks for more immediate notifications; do not shorten the interval to a few seconds

page_size

20

Default 20, maximum 100. Larger pages produce slower responses and more content

  • Response fields use camelCase. lastMessageCreatedAt and createdAt are timestamp strings in milliseconds (for example, "1790753081000"), but have only second precision. Multiple messages may arrive within the same second; deduplicate by message id, rather than comparing timestamps alone.

  • The example uses the local time as the starting point for detection. Enable network time synchronization (NTP) on the host to prevent a fast clock from skipping messages at startup.

  • The conversation list includes only conversations with customer messages. Conversations containing only an opening greeting, where the customer has not yet spoken, do not appear.

  • lastMessageCreatedAt always updates when a new customer message arrives, but the update may occur after the message itself is written (see step 1 above). An automatic AI reply does not always update it. This polling approach therefore detects only customer messages: an AI reply is retrieved along with them only if it has already been generated at the time of reading. If an AI reply is generated after the customer message has been read and does not change the conversation’s last message time or last message, subsequent polling skips that conversation and will not retrieve the reply. If generating a single AI reply takes more than 10 minutes (for example, due to a long-running tool), detection of the customer message may also be delayed until the conversation is updated again. To receive every customer message and reply promptly, use Incoming/Outgoing message Webhooks (temporary failures are retried up to 3 times, but delivery is still not guaranteed).

Request Examples

An API key has the same permissions as its owner’s role in the organization. The owner must be able to view conversations on the relevant conversation platform. See API Authentication → API Key Permission Scope.

Last updated

Was this helpful?