Retrieve chats & messages

Learn how to retrieve chats, groups and messages on WhatsApp using Unipile API.

Get individual and group chats

All chats are grouped into a single inbox. To fetch them, use the List all Chats method.
You can apply filters such as archive status, unread status, or chat type (individual or group).

const { data, error } = await messagingApi.getChatsList({
  path: {
    account_id: "acc_123456789",
  },
  query: {
    limit: 20,
  },
});
chats = messaging_api.get_chats_list(
    "acc_123456789",
    limit=20,
)

To get a single chat, use Get a Chat.

const { data, error } = await messagingApi.getChat({
  path: {
    account_id: "acc_123456789",
    chat_id: "chat_id",
  },
});
chat = messaging_api.get_chat("chat_id", "acc_123456789")

WhatsApp user and chat IDs

WhatsApp may return a user's primary ID as an LID ending in @lid, rather than as a phone-number JID ending in @s.whatsapp.net. This is expected: use the id values returned by Unipile unchanged in subsequent requests, including chat_id and user_id.

When the phone number is available, it is exposed separately as public_identifier. For a new conversation where you only know the phone number, continue to use the [email protected] format. Do not derive or replace an ID from its other form yourself; Unipile resolves the LID/phone-number mapping when needed.


Get Messages

To list messages of a Chat, use List all Chat Messages method and provide the chat ID.

const { data, error } = await messagingApi.getMessagesList({
  path: {
    account_id: "acc_123456789",
    chat_id: "chat_id",
  },
  query: {
    limit: 20,
  },
});
messages = messaging_api.get_messages_list(
    "chat_id",
    "acc_123456789",
    limit=20,
)
curl --request GET \
     --url https://api.unipile.com/v2/account_id/chats/chat_id/messages \
     --header 'accept: application/json'

To get a single Message, use Get a Message method and provide the chat ID with the message ID.

const { data, error } = await messagingApi.getMessage({
  path: {
    account_id: "acc_123456789",
    chat_id: "chat_id",
    message_id: "message_id",
  },
});
message = messaging_api.get_message(
    "chat_id",
    "message_id",
    "acc_123456789",
)
curl --request GET \
     --url https://api.unipile.com/v2/account_id/chats/chat_id/messages/message_id \
     --header 'accept: application/json'

Be notified about new messages

To receive new upserted messages, setup a Webhook that listen for message.new events.

You'll be notified about any received message, but also any sent messages. This can be useful for your application to be aware of messages sent by the account owner from other devices. To ignore those messages, filter events on the is_sender value of the Message object.


Did this page help you?