Skip to main content

Conversations and Messages

Every chat your agent has with a customer, on WhatsApp, Facebook, Instagram or your website, is a conversation. With the Public API your systems can read them and their messages, reply as your team, close or archive them, switch the AI off in one conversation and see who switched it, mark it urgent or resolved, and assign it to a teammate. Everything lives under /v1/agents/{agentId}/conversations.

How do I list an agent's conversations?​

GET them, the most recently active first. Filter by status, channel, is_read, urgent, assigned_to, contact_id, or by when the last message came with active_since and active_before:

curl "https://api.mojeeb.app/v1/agents/YOUR_AGENT_ID/conversations?is_read=false&channel=whatsapp" \
-H "Authorization: Bearer mk_live_YOUR_KEY_HERE"

Each conversation carries its last message, who wrote it and when, whether your team has read it, its attention flags, the AI's state and its assignee, so you can tell which ones need you without opening them. search finds a customer's name or a word in the last message. A list filtered by status, channel, read state, urgency or assignee covers only a limited stretch of activity, ending at active_before or now, unless you send active_since, so the list stays fast on a large inbox; a contact's conversations always come back whole. A conversation with no message yet isn't listed. See the List conversations reference for the window.

How do I keep up with new messages?​

Poll the list, not each conversation. Every minute or so, list with active_since set a few minutes before the newest last_message_at you have seen, and page while has_more is true. Only conversations with a new message come back. For each one, read its messages with after set to a message a few minutes older than the newest one you have:

curl "https://api.mojeeb.app/v1/agents/YOUR_AGENT_ID/conversations/CONVERSATION_ID/messages?after=MESSAGE_ID" \
-H "Authorization: Bearer mk_live_YOUR_KEY_HERE"

With after, messages come oldest first. While has_more is true, call again with after set to the last message you got. Keep both reads overlapping and drop what you already have by id: a message your team sends from the WhatsApp or Meta apps carries the platform's time, which can be a little earlier than when Mojeeb records it, so it can land before messages you have already read. A customer's photo, voice note, sticker or file sent without a caption doesn't move last_message_at, so the conversation only comes back once a later message, such as the AI's answer, moves it. A change of status, read state, flags or assignee doesn't bring a conversation back either; read it with GET (Get a conversation) when you need its state.

How do I read a conversation's messages?​

Without after, the newest messages come first, and next_cursor reads further back. Each message says who sent it: the customer, the AI, a user (a teammate, or a key writing as its creator), or Mojeeb on your behalf (a broadcast, a /v1/whatsapp send). Messages sent to the customer carry their delivery status. Attachment links expire; read the message again for a fresh one. See the List messages reference for how long they last.

Customers write the message text. Treat it as untrusted input, especially before you hand it to an AI model: a customer can write instructions aimed at your assistant.

How do I know which button a customer tapped?​

A customer's message that answered a button carries interaction: its type (quick_reply for a WhatsApp template's button), the payload you sent the button with (quick_reply_payload in Send Template), and the title the customer saw. On WhatsApp, reply_to_message_id is the id of the message the customer answered: the template whose button they tapped, or a message they swiped to reply to. A template sent from Mojeeb carries template, with its id, name and language, so a reply leads you to the template it answers.

{
"id": "6d5c4b3a-2918-4f7e-8d6c-5b4a39281706",
"sender": "customer",
"type": "text",
"text": "Yes, I confirm",
"interaction": { "type": "quick_reply", "id": null, "payload": "BOOKING_YES", "title": "Yes, I confirm" },
"reply_to_message_id": "5c4b3a29-1807-4f6e-9d5c-4b3a29180706",
"template": null
}

Mojeeb doesn't push messages to a webhook: read them with after, as in How do I keep up with new messages?

How do I reply to a customer?​

POST the text to the conversation's messages:

curl -X POST https://api.mojeeb.app/v1/agents/YOUR_AGENT_ID/conversations/CONVERSATION_ID/messages \
-H "Authorization: Bearer mk_live_YOUR_KEY_HERE" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: reply-7f3a9c" \
-d '{ "text": "Yes, we are open on Friday from 2 pm to 10 pm." }'

The reply goes out on the conversation's channel as your team. In the dashboard it shows as written by the person who created the key. When the agent's ai_handoff_enabled is on, the reply pauses the AI in that conversation for the handoff window, like any teammate's reply; when it is off, the AI keeps answering. See Agent Settings. When a pause ends, the AI may write once more if follow-ups are on for that channel; to prevent it, switch the AI off in the conversation or resume it. A reply never assigns the conversation to anyone.

The answer is 201 with the message, queued. It is sent right after, and its delivery status changes from there; follow it by reading the message again with GET on its id. The Get a message reference lists every status. Send an Idempotency-Key so a retry after a timeout never sends the reply twice; see Idempotency. Replies have a rate limit of their own; see Rate Limits and the Reply reference.

How do I send a photo, video or document in a reply?​

Add media_url to the reply: one image, video or document, as an https link Mojeeb downloads (from a public address, without redirects) or, for a file that isn't online, the url of a Mojeeb upload of this agent. text becomes optional and is the file's caption. The file is stored and shown in the conversation as a file the composer sent would be.

curl -X POST https://api.mojeeb.app/v1/agents/YOUR_AGENT_ID/conversations/CONVERSATION_ID/messages \
-H "Authorization: Bearer mk_live_YOUR_KEY_HERE" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: menu-7f3a9c" \
-d '{ "media_url": "https://api.mojeeb.app/v1/agents/YOUR_AGENT_ID/uploads/UPLOAD_ID", "text": "Here is our menu." }'

Mojeeb reads the file before anything is saved, and the reply passes the same channel and 24-hour checks as text. A video is re-encoded, so videos also count against a budget of 10 a minute per key, on top of the replies' own limit: past it, the reply is refused with 429 video_send_rate_limited and a Retry-After. The kinds and sizes a file may be are in the Reply reference.

Why was my reply refused?​

Before anything is saved, Mojeeb checks that the reply can reach the customer:

  • 422 messaging_window_closed: the customer hasn't written in the last 24 hours. WhatsApp, Facebook and Instagram deliver your free-form message only within 24 hours of the customer's last one. On WhatsApp, send an approved template instead; see Send a WhatsApp Template.
  • 422 channel_not_supported: Mojeeb can't send on this channel yet (TikTok).
  • 422 media_url_invalid: media_url can't be read, isn't a file a message carries, or names no upload of this agent.
  • 422 invalid_media: the file arrived, but its bytes don't pass the checks for the file a reply carries.

A refused reply isn't stored for its Idempotency-Key, and for a short while that key answers 409 idempotency_key_in_progress: once the cause has passed, send the reply again with a new key. See Idempotency.

A website chat has no window: the reply is stored as sent and appears in the customer's open chat.

How do I test the agent in a chat?​

POST a message to the agent's test chat. Mojeeb answers it the way the agent answers a customer and returns the answer in the same response, usually within a few seconds:

curl -X POST https://api.mojeeb.app/v1/agents/YOUR_AGENT_ID/test-chat \
-H "Authorization: Bearer mk_live_YOUR_KEY_HERE" \
-H "Content-Type: application/json" \
-d '{ "text": "Hi, do you deliver to Maadi, and how much is it?" }'

The answer carries a conversation_id: send it with the next message to continue the same test chat, so the agent sees the whole chat. status is replied with the agent's messages in replies; pending when no answer came within 30 seconds (read it from the messages list with after set to message_id); or no_reply when the AI is switched off in that chat.

  • The agent's tools run for real, as in the dashboard's test chat: a test can book a slot, capture a lead or call your system.
  • Each answer counts against your plan's messages. Over the limit, the answer is 402 message_limit_exceeded and nothing is saved.
  • Send one message at a time per test chat; another one while the first waits is 409 test_chat_busy. Test messages have a rate limit of their own; see Rate Limits and the Test chat reference.
  • Only test chats can be continued: a customer's conversation is 404 not_found.

How do I switch the AI off in one conversation?​

PATCH the conversation with ai_enabled:

curl -X PATCH https://api.mojeeb.app/v1/agents/YOUR_AGENT_ID/conversations/CONVERSATION_ID \
-H "Authorization: Bearer mk_live_YOUR_KEY_HERE" \
-H "Content-Type: application/json" \
-d '{ "ai_enabled": false }'

The AI stays off in that conversation until you send "ai_enabled": true, the same switch as in the dashboard. Every switch you make this way is logged as made by your key, with the source public_api; see How do I see who switched the AI off in a conversation?. The AI can also switch itself off when the agent's ai_stop_enabled is on (Agent Settings), or when it opens a new ticket while the ticket capture's disable_ai_on_ticket is on (Tickets), and "ai_enabled": true switches it back on in the same way. Resolving the conversation ("urgent": false) never switches the AI back on.

A teammate's reply only pauses the AI for a while, shown as ai_paused_until; to end that pause now, POST to the conversation's resume-ai (Resume the AI reference). To switch the AI off on a whole channel, see Channels and Follow-ups.

How do I see who switched the AI off in a conversation?​

GET the conversation's ai-switches. It lists the times the conversation's AI was switched on or off, newest first: the value it was switched to, where the switch came from, the teammate who made it, and when. Use it to tell why the AI is off in a conversation, including when the AI stopped responding by itself.

curl https://api.mojeeb.app/v1/agents/YOUR_AGENT_ID/conversations/CONVERSATION_ID/ai-switches \
-H "Authorization: Bearer mk_live_YOUR_KEY_HERE"

Each switch's source says where it came from:

  • ai: the AI stopped responding by itself because the agent's ai_stop_when or Mojeeb's safety rules applied, or because it opened a new ticket while the ticket capture's disable_ai_on_ticket is on. It is always a switch off, and reason holds the AI's one line in English for your team, or Ticket #1007 opened after a ticket.
  • dashboard: a teammate, whose user id is in changed_by; find them with Your Team.
  • public_api: an API key or a connected AI assistant.

changed_by is null for the AI and for an API key, and reason is set only when source is ai. New source values may appear, so don't fail on one you don't know. The timed pause after a teammate's reply (ai_paused_until) isn't a switch and isn't listed. Another agent's conversation, or a deleted one, is 404 not_found. See the List AI switches reference for the fields and how many switches come back.

How do I mark a conversation urgent or resolved?​

PATCH it with urgent. true is the dashboard's Mark as Urgent, and false is Mark as Resolved. Both clear needs_attention, and resolving also sets an unhappy sentiment to neutral, so the conversation stops showing as needing attention. The AI reads both flags again with each of its replies, so a mark lasts until the AI next answers in that conversation. Resolving never switches the AI back on in a conversation where it is off; send "ai_enabled": true for that. See the Update a conversation reference.

How do I close, archive or mark a conversation read?​

PATCH it with status (open, closed or archived) or is_read, alone or with other fields in the same call. is_read is for your team only: marking a conversation read never sends the customer a read receipt. A change to what the conversation already has writes nothing. See the Update a conversation reference.

How do I assign a conversation?​

POST the teammate's user_id as assigned_to to the conversation's assign, or "assigned_to": null to unassign it. The teammate must be able to open the agent; find them with Your Team. The conversation's lead is assigned with it (one of them when it has several), and Mojeeb records the assignment as made by the person who created the key. See the Assign a conversation reference.

How do I read a conversation's leads and tickets?​

GET /v1/agents/{agentId}/conversations/{conversationId}/leads returns the conversation's leads, and .../tickets its tickets, each newest first. A conversation can hold several of each, one per request the customer made; items is empty when it has none. Neither list is paged: each returns the newest 20.

Use them before you create a lead or a ticket with conversation_id: both always create a new record, so reading first tells you whether to create one or update one that is already there. See the List a conversation's leads and List a conversation's tickets references.

How do I know which ad brought a customer?​

Each conversation carries attribution: origin is the touch that first brought the customer in, latest the most recent one, and they are the same object when there was only one. A touch has a kind (ad_tap, post_tap, product_tap, link, plugin for your website chat, or comment_reply for a private reply to a comment on your post), its channel, when it happened, and the ad when the touch was on one, a tap on it or a private reply to a comment on its ad-only post (an Instagram ad's media or a Facebook dark post): Mojeeb's id, the Meta ad id as Ads Manager shows it, and the headline and image Meta sent with the tap. A comment reply on an organic or boosted post has no ad. Both touches are null when nothing is known, and the whole block is null on the response to a write, so read the conversation for it. Contacts and leads carry the same block. See the Get a conversation reference.

A conversation also carries latest_ad_id: the ad behind its latest touch (a tap on it, or a private reply to a comment on its ad-only post), stored on the conversation itself, so it is on every response including a write's. It is null when the latest touch carried no ad, and the same ad as attribution.latest.ad.id when that is set.

Which ads bring customers in?​

GET /v1/agents/{agentId}/ads lists the Meta ads customers have reached the agent through, newest first by when Mojeeb first saw each, and GET /v1/agents/{agentId}/ads/{adId} reads one (a conversation's latest_ad_id, or the ad.id of a touch). Each ad carries what the taps told Mojeeb about it (the Meta ad id, the headline, the image and the catalog product, when Meta sent them) and what it brought in: taps by channel, comment_replies (private replies sent to comments on the ad's own post, an Instagram ad's media or a Facebook dark post, one per comment; comments on a boosted published post are not attributed to an ad), and the distinct conversations_touched, contacts_touched and leads_touched, plus comments_on_posts and posts for the posts it runs on, and tool_runs with tool_runs_by_tool, the successful tool runs while the ad was the latest touch, in total and per tool id. The counts are read when you ask, so they are always current. An ad appears once a customer taps it or, on Instagram, once a comment arrives on its ad media; Mojeeb never reads Ads Manager, so spend and reach are not here. Filter with active_since (ads seen on or after an instant) or search (a part of the title). To list the conversations an ad brought in, pass its id as ad_id to List conversations. Reading ads needs ads:read, which every key holds today. See the List ads reference.

Common questions​

Can I search inside messages?​

No. Searching message history reads every message the agent has, so it stays in the dashboard, where someone starts it on purpose. Through the API, search matches only the customer's name and the last message. To find a person, look them up in Contacts and filter the conversations by contact_id.

Can I send images or files?​

Yes, one image, video or document per reply, with media_url, from a link or a Mojeeb upload; see How do I send a photo, video or document in a reply? The Reply reference gives the text's length and how each channel delivers a long reply. On WhatsApp, a template can also carry a header image, video or document.

Do replies through the API count toward my message limit?​

No. Your plan counts the AI's messages. Your team's replies don't count, whether they come from the dashboard or the API.

Can I start a conversation?​

Not with a customer: no call creates a customer's conversation (the test chat creates its own, for testing; see How do I test the agent in a chat?). On WhatsApp you can reach a customer first with a template (see Send a WhatsApp Template): it lands in their conversation, and starts one if they have none yet.

Why did the AI switch itself off in a conversation?​

The agent's ai_stop_enabled is on, and a customer's message matched its ai_stop_when, or the AI spotted a bot loop or an attack on the AI. The AI told the customer someone from your team will continue, flagged the conversation needs_attention and switched its AI off. Read the reason in the conversation's ai-switches; "ai_enabled": true switches the AI back on.

Which scopes do these calls need?​

conversations:read to list conversations, read messages and see who switched the AI, leads:read and tickets:read to list a conversation's leads and tickets, conversations:write to change, assign, reply and test the agent in a chat, and ads:read to list and read ads. Every key holds them today; see Authentication.