Skip to main content

UC-015 — Two-Way Conversation

Real-world scenarios​

  • TechStore — Customer support chat: A customer writes on WhatsApp to ask about an order; the system receives the message, assigns it to an operator and replies with the shipping status.
  • Studio Dentistico Bianchi — Booking confirmation by reply: After an appointment reminder is sent, the patient confirms or asks to move the visit by replying to the message.
  • FashionOutlet — Post-sale feedback collection: A post-purchase survey asks the customer to reply with a score from 1 to 5; the system collects and catalogs the replies automatically.

Prerequisites​

Before you begin, make sure you have:

Interaction flow​

Step-by-step guide​

Step 1 — Configure the webhook for inbound messages​

First, register a webhook to receive incoming messages. Despite its name, the delivery-status webhook receives every event of your account: DELIVERY, READ and INBOUND. See the Webhooks guide for the full configuration.

curl -X POST "https://api.qlara.ai/api/partner-gateway/webhooks/delivery-status" \
-H "X-Api-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"callbackUrl": "https://api.techstore.it/webhooks/qlara"
}'

Response: 201 Created

{
"companyId": 189,
"callbackUrl": "https://api.techstore.it/webhooks/qlara"
}
One webhook per account

Each account has a single callback URL, shared by SMS, RCS and WhatsApp. If a webhook is already configured, the call answers 409 Conflict and leaves the existing one in place: check it with GET /webhooks/delivery-status and change the URL with PUT on the same path. The URL must be HTTPS on port 443 or 8443 and publicly reachable, otherwise the call answers 400.

Step 2 — Receive an inbound message​

When a customer sends a message, your endpoint receives a payload like this one. INBOUND events exist for RCS and WhatsApp only.

Inbound TEXT:

{
"eventType": "INBOUND",
"channel": "WHATSAPP",
"messageId": "msg-wa-in-001",
"source": "+393471234567",
"destination": "+393209998877",
"receivedDate": "2026-04-09T11:15:22+02:00",
"messageType": "TEXT",
"text": "Buongiorno, vorrei sapere lo stato del mio ordine ORD-2026-1584"
}

Inbound IMAGE:

{
"eventType": "INBOUND",
"channel": "WHATSAPP",
"messageId": "msg-wa-in-002",
"source": "+393471234567",
"destination": "+393209998877",
"receivedDate": "2026-04-09T11:16:05+02:00",
"messageType": "IMAGE",
"mediaKey": "b394ab72/efd4987c/39b4879f6fed01f0d622453be1488c93"
}
FieldDescription
eventTypeAlways INBOUND for a message written by the end user
channelWHATSAPP or RCS
messageIdIdentifier of the message in the conversation
sourceThe end user's phone number
destinationYour WhatsApp number (WhatsApp) or your agent id (RCS)
receivedDateWhen the message was received. INBOUND events have no eventDate
messageTypeTEXT for text messages; media messages (e.g. IMAGE) carry a mediaKey
textText of the message
mediaKeyKey of the attachment, present for media messages

Null fields are left out of the payload, so a text message has no mediaKey.

Download media

To download an attached media file, use GET /api/files/{mediaKey}?expireMinutes=180. The response contains a temporary pre-signed URL. See UC-017: Download Media from Inbound Messages.

Example handler (Node.js) and how to try it from your computer

The same address receives every event of your account: INBOUND, and the DELIVERY and READ of the messages sent through the send APIs. A minimal handler:

webhook-handler.js
const express = require('express');
const app = express();
app.use(express.json());

// Final failures: SMS = rejected, deleted, undeliverable
// RCS/WhatsApp = general error, disabled, unsupported, conversation closed
const SMS_FAILURES = [2, 5, 6];
const RCS_WA_FAILURES = [9, 10, 11, 12];

app.post('/webhook', (req, res) => {
const { eventType, messageId, statusCode, channel } = req.body;

// Events can arrive more than once: deduplicate on eventType + messageId (+ numPart on SMS)

switch (eventType) {
case 'DELIVERY':
if (statusCode === 3) {
console.log(`Message ${messageId} delivered via ${channel}`);
// Update your database: mark message as delivered
} else if (statusCode === 4) {
// Expired: sent on SMS only (poll RCS and WhatsApp to see EXPIRED)
console.log(`Message ${messageId} expired via ${channel}`);
} else if (SMS_FAILURES.includes(statusCode) || RCS_WA_FAILURES.includes(statusCode)) {
console.log(`Message ${messageId} not delivered via ${channel}: ${req.body.description}`);
// Handle failure: retry or notify user
}
break;

case 'READ':
console.log(`Message ${messageId} read by recipient at ${req.body.eventDate}`);
break;

case 'INBOUND':
console.log(`Inbound from ${req.body.source} at ${req.body.receivedDate}: ${req.body.text}`);
// Process reply: auto-respond, forward to CRM, etc.
break;
}

// Always respond quickly (within 5 seconds) — process asynchronously if needed
res.sendStatus(200);
});

app.listen(3000, () => console.log('Webhook handler listening on port 3000'));

To try it from your computer, expose the local port with a tunnel such as ngrok and register the HTTPS address it gives you:

# Start the handler on port 3000
node webhook-handler.js

# In another terminal, expose it: ngrok prints an https:// address
ngrok http 3000

# Register that address (PUT instead of POST if a webhook is already configured)
curl -X POST "https://api.qlara.ai/api/partner-gateway/webhooks/delivery-status" \
-H "X-Api-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"callbackUrl": "https://abc123.ngrok.io/webhook"}'

Then write to your number or agent from a phone: the INBOUND event reaches the handler. A send with simulation: true sends nothing, so it produces no event.

Step 3 — List the conversations​

Retrieve the conversations, keeping only those with unread messages:

curl -X GET "https://api.qlara.ai/api/partner-gateway/inbox/conversations?unreadOnly=true" \
-H "X-Api-Key: YOUR_API_KEY"

Response:

[
{
"id": 1042,
"hasUnreadMessages": true,
"userName": "Marco Bianchi",
"phoneNumber": "+393471234567",
"unreadCount": 2,
"lastMessageText": "Buongiorno, vorrei sapere lo stato del mio ordine ORD-2026-1584",
"lastMessageDate": "2026-04-09 09:15:22.041+0000",
"socialMedia": "whatsapp",
"channelId": 42,
"companyProfileName": "TechStore Italia",
"companyPhoneNumber": "+393209998877",
"whatsappWindowExpire": "2026-04-10 09:15:22.041+0000",
"status": "OPEN",
"visibility": "ACTIVE"
}
]

The example shows the main fields; UC-016 shows a complete entry. socialMedia is lowercase (whatsapp, rcs, instagram, facebook or chatbot), status is OPEN, IN_PROGRESS or CLOSED, and visibility is the inbox section the conversation is in (ACTIVE, ARCHIVED or TRASHED).

Step 4 — Get the conversation messages​

Retrieve the latest messages of a conversation, up to 20 per call, oldest first (older ones are reached as shown in Message pagination):

curl -X GET "https://api.qlara.ai/api/partner-gateway/inbox/conversations/1042/messages" \
-H "X-Api-Key: YOUR_API_KEY"

Response:

{
"profile": {
"userName": "Marco Bianchi",
"profilePicture": null,
"additionalInfo": "Marco Bianchi",
"contactId": 18273,
"externalUserId": null
},
"messages": [
{
"id": 5001,
"text": "Buongiorno, vorrei sapere lo stato del mio ordine ORD-2026-1584",
"isRead": false,
"isUserReply": true,
"createdAt": "2026-04-09 09:15:22.041+0000",
"attachments": [],
"status": "RECEIVED"
}
]
}

isUserReply: true marks a message written by the contact, false one of your replies. profile.userName is the contact's name in your contact book when the conversation matches a contact (contactId), otherwise the name on the channel, which additionalInfo always carries. A chatId that does not exist or does not belong to your account is answered 404.

Reading marks the conversation as read

A call without idFrom and idTo, like this one, also marks the conversation as read: its unreadCount goes to 0 and the channel is told that every unread inbound message was read, so the contact can see a read receipt. The messages of this same response can still show isRead: false. To read the latest page without that effect, add idFrom=1: you get the same messages and the conversation stays unread.

Step 5 — Reply to the conversation​

Send a reply within the existing conversation:

curl -X POST "https://api.qlara.ai/api/partner-gateway/inbox/conversations/reply" \
-H "X-Api-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"chatId": 1042,
"message": "Buongiorno Marco! Il suo ordine ORD-2026-1584 è in fase di spedizione. Riceverà il tracking entro oggi pomeriggio."
}'

Response: 202 Accepted

chatId is required, together with a non-blank message, at least one attachment, or both: a reply with neither is answered 400 and nothing is sent. attachments is a list of {"mediaId": …}, each one a media item of your own library: a media item that does not exist or belongs to another account is answered 404 {"status":"fail","data":"Media not found"} before anything is sent. You can also send replyTo, the id of the message you are quoting. The text can be at most 4096 characters on WhatsApp, 3072 on RCS, 2000 on Facebook Messenger and 1000 on Instagram.

The reply goes out on the conversation's channel. 202 means it was accepted and queued; a refusal comes back at once as 400 or 502 (see Common errors). Track its delivery through the DELIVERY webhook event.

24-hour window

On WhatsApp, free-form replies are only possible within 24 hours of the customer's last message. Check the whatsappWindowExpire field of the conversation. Outside the window the reply can still be accepted (202) but is not delivered: the DELIVERY event carries statusCode: 12 (conversation closed). After the window expires, only a Meta-approved template reaches the customer: send it with a campaign (see UC-008).

Step 6 — Mark as read (if needed) and archive​

The GET of Step 4 already marked the conversation as read. Call PATCH /conversations/{chatId}/read only if you read the messages with idFrom or idTo, or to mark a conversation as read without fetching it; like the GET, it tells the channel that the unread messages were read:

curl -X PATCH "https://api.qlara.ai/api/partner-gateway/inbox/conversations/1042/read" \
-H "X-Api-Key: YOUR_API_KEY"

Response: 204 No Content

When the conversation is over, archive it:

curl -X PATCH "https://api.qlara.ai/api/partner-gateway/inbox/conversations/1042/archive" \
-H "X-Api-Key: YOUR_API_KEY"

Response: 204 No Content

Restore

An archived conversation no longer appears in GET /inbox/conversations: list it with section=ARCHIVED, and restore it at any time with PATCH /conversations/{chatId}/unarchive. If the contact writes again, the conversation comes back to the active inbox on its own, with status OPEN. Conversations also move on their own: one with no new message for 7 days gets status CLOSED and stays in the active inbox, and one that has been CLOSED for 30 days is archived.

Conversation filter parameters​

ParameterTypeDescription
channelIdsstringComma-separated CHANNEL-channelId values, built from the socialMedia and channelId fields of a conversation (e.g. WHATSAPP-42,INSTAGRAM-1308); CHANNEL is case-insensitive. A value that matches no channel, such as a bare number, returns an empty list, not an error
dateFromstringOnly conversations whose last message is at or after this instant (inclusive): yyyy-MM-dd (00:00:00Z of that day), yyyy-MM-ddTHH:mm:ss (UTC) or ISO-8601 with an offset (e.g. 2026-04-09T00:00:00Z, 2026-04-09T00:00:00%2B02:00: encode a + as %2B). Any other format is answered 400
orderBystringSort field with a +/- prefix (e.g. -lastMessageDate)
sectionstringInbox section: ACTIVE (the default), ARCHIVED or TRASHED, case-insensitive. Without it only ACTIVE conversations are returned; any other value is answered 400
unreadOnlybooleanIf true, returns only conversations with unread messages

Message pagination​

A call returns at most 20 messages: the most recent ones in the requested id range, ordered oldest first. idFrom and idTo are inclusive bounds on the message id. Messages are picked by creation time, so take the smallest id among all the messages of a page, not the id of the first one.

  • Older messages: call again with idTo set to the smallest id received minus 1, until a page comes back with fewer than 20 messages.
  • New messages after the last one you hold: call with idFrom set to its id plus 1. If 20 messages come back there may be more in between: keep idFrom and page backwards with idTo as above until a page has fewer than 20 messages.

A call with idFrom or idTo does not mark the conversation as read. This one returns at most the 20 newest messages with an id from 5000 to 5050:

curl -X GET "https://api.qlara.ai/api/partner-gateway/inbox/conversations/1042/messages?idFrom=5000&idTo=5050" \
-H "X-Api-Key: YOUR_API_KEY"
Behind the scenes

When a customer sends a message, the carrier (Meta for WhatsApp, Google for RCS) delivers it to the Qlara platform, which:

  1. Creates or updates the conversation -- If the contact already has a conversation on the same channel, the message is added to it, and an archived or trashed conversation goes back to the active inbox with status OPEN. Otherwise a new one is created. Only inbound messages create conversations: a reply needs an existing one.
  2. Notifies you via webhook -- The INBOUND payload is sent to your registered callback URL. Your server must answer with any 2xx within 5 seconds. Failed deliveries are retried, so the same event can arrive more than once: deduplicate on eventType + messageId.
  3. Manages the window -- On WhatsApp, the platform automatically moves the 24-hour window expiry forward each time the customer writes.
  4. Routes the reply -- When you send a reply with POST /conversations/reply, the platform automatically sends it on the same channel as the original conversation.

Expected result​

AspectDetail
Action completedInbound message received, reply sent, conversation archived
Channel usedWhatsApp / RCS
Delivery confirmationVia webhook (statusCode: 3) within 5-60 sec

Common errors​

ProblemProbable causeSolution
HTTP 401Missing or invalid API KeyCheck X-Api-Key header
HTTP 409 on POST /webhooks/delivery-statusA webhook is already configured for the accountCheck it with GET /webhooks/delivery-status; change the URL with PUT on the same path
HTTP 400 on POST /webhooks/delivery-statusThe callback URL is not a public HTTPS URL on port 443 or 8443Use a publicly reachable HTTPS URL, without credentials or fragment
HTTP 400 on GET /inbox/conversationssection is not ACTIVE, ARCHIVED or TRASHED, or dateFrom is in none of the accepted formatsThe data of the body names the parameter and the accepted values
HTTP 400 on replyMalformed body, missing chatId, an attachment without mediaId, or an empty reply (no non-blank message and no attachment); or the channel refused the reply: text over its limit, an attachment type or size it does not accept, or any RCS refusalSend chatId with a message, attachments or both, and keep the text within the channel limit. A refusal by the channel reads only "Bad request."
HTTP 404 — Conversation not foundThe chatId does not exist or does not belong to your accountGet a valid chatId from GET /inbox/conversations
HTTP 404 on reply"Media not found": an attachment is not a media item of your library. "Resource not found.": the conversation is unknown, or, on a WhatsApp reply with text, replyTo is not a message of the conversationSend media of your own library and quote a message of the same conversation
HTTP 502 on replyThe channel did not accept the reply, or an attachment could not be read from the media libraryRetry later. Only one case sends part of the reply: on WhatsApp the text and each attachment go out as separate messages, text first, so when WhatsApp refuses a later part the earlier ones are already out, and a retry sends them again
Reply accepted but not deliveredWhatsApp 24h window has closed (DELIVERY event with statusCode: 12)Check whatsappWindowExpire; use a Meta-approved template instead
No INBOUND webhook receivedWebhook not configured, or endpoint unreachable or not answering 2xx within 5 secondsVerify the registration with GET /webhooks/delivery-status and check your endpoint

Next steps​