Skip to content
QwryAI
Documentation
Pages

Guides

User Conversations

Tie Chat API conversations to your own customer ids and keep each customer's history together

When your app knows who it's talking to, pass that customer's id as userId when a conversation starts. QwryAI stores it on the conversation as the customer's identifier and returns it with every answer, so you can keep a customer's conversations together in your own system and continue any of them later.

Overview

A conversation belongs to one agent and, optionally, one userId. You start it with the chat endpoint, keep the conversationId from the answer, and send it with every follow-up. Which conversations belong to which customer lives in your system, keyed by userId.

Setting a User ID

Pass userId on the request that starts the conversation, the one without conversationId:

ConstraintValue
Max length128 characters
Allowed charactersa-z, A-Z, 0-9, ., _, -
When appliedOnly when a conversation starts (no conversationId in the request)
MutabilityFixed for the life of the conversation
JavaScript
const response = await fetch(
  "https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/chat",
  {
    method: "POST",
    headers: {
      Authorization: "Bearer YOUR_API_KEY",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      message: "Hi, I'd like to change my delivery address.",
      userId: "customer_1042",
    }),
  }
);
Python
import requests

response = requests.post(
    "https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/chat",
    headers={
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json",
    },
    json={
        "message": "Hi, I'd like to change my delivery address.",
        "userId": "customer_1042",
    },
    stream=True,
)

The answer's metadata includes the userId:

JSON
{
  "type": "finish",
  "finishReason": "stop",
  "messageMetadata": {
    "messageId": "5b0e7c1a-2f4d-4c55-9a51-6d8e3f2b9c10",
    "conversationId": "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3",
    "userMessageId": "0c4f6a2e-8d13-4b7a-b0e9-1a2c3d4e5f60",
    "userId": "customer_1042",
    "usage": { "credits": 1 }
  }
}

Continuing a Conversation

To send a follow-up in the same conversation, pass the conversationId from the earlier answer:

JavaScript
const response = await fetch(
  "https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/chat",
  {
    method: "POST",
    headers: {
      Authorization: "Bearer YOUR_API_KEY",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      message: "It's 14 Harbour Street, Wellington.",
      conversationId: "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3",
    }),
  }
);
Python
response = requests.post(
    "https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/chat",
    headers={
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json",
    },
    json={
        "message": "It's 14 Harbour Street, Wellington.",
        "conversationId": "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3",
    },
    stream=True,
)

Reading Conversations

The Chat API sends messages; it doesn't list them. To read conversations from your server, use the Tools API with a key that holds Manage conversations and helpdesk:

ToolReturns
query_conversationsConversations in a date range, 20 per page, from every channel, with their AI summary, channel, state and sentiment.
get_conversation_detailOne conversation and its messages, 20 per page.
search_conversationsConversations whose messages contain a phrase. See Conversation Search.

Each row from query_conversations has two ids: conversationId is the one the Chat API uses, and _id is the one the Tools API's conversation tools take as their conversationId argument.

Bash
curl -X POST 'https://api.qwryai.com/public/v1/tools/query_conversations' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "chatbotId": "YOUR_AGENT_ID",
    "startDate": "2026-11-01",
    "endDate": "2026-11-30",
    "page": 1,
    "sources": ["api"]
  }'

Arguments

ArgumentTypeRequiredDescription
chatbotIdstringYesThe agent's id from list_agents.
startDate, endDatestringYesCreation dates, YYYY-MM-DD in UTC, both included, at most 184 days apart.
pageintegerYesThe page, from 1.
sourcesstring[]NoChannels, such as api, web or whatsapp. get_conversation_facets lists the values in use.
activity_statesstring[]Noongoing, ended, taken_over, paused.
contactstringNoMatches the contact's name or email.
feedbackstringNogood or bad.
escalatedbooleanNotrue for conversations handed to a person.

Response

JSON
{
  "success": true,
  "data": {
    "items": [
      {
        "conversationId": "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3",
        "channel": "api",
        "lifecycle": "open",
        "metadata": { "aiSummary": "Customer asked to change the delivery address on order 1042." },
        "timestamps": { "created": "2026-11-30T19:23:43.046Z", "lastActivity": "2026-11-30T19:24:45.362Z" },
        "metrics": { "messageCount": 4, "aiMessageCount": 2, "agentMessageCount": 0 },
        "insights": { "sentiment": "neutral" }
      }
    ],
    "total": 1,
    "page": 1,
    "hasMore": false,
    "range": { "startDate": "2026-11-01", "endDate": "2026-11-30", "page": 1 },
    "timezone": "UTC"
  }
}

Response Fields

FieldDescription
conversationIdThe id the Chat API uses.
_idThe id the Tools API's conversation tools take as conversationId (not shown above).
channelWhere the conversation happened, such as api, web or whatsapp.
lifecycleopen while it's going on.
metadata.aiSummaryQwryAI's one-line summary, once it has one.
timestampsWhen it started and when it was last active.
metricsMessage counts, in total and from the AI and your team.
insightsSentiment and topics, once analysed.

Pagination Examples

Raise page until hasMore is false. See Pagination for the full loop.

In mobile apps

The iOS and Android SDKs list a signed-in customer's own conversations directly, without an API key: the customer's identity token decides whose history comes back. Conversations a customer had before signing in on that device move to their account when they sign in. See User identity.

Full Example

  1. Start a conversation for the customer
    JavaScript
    const API = "https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/chat";
    const headers = { Authorization: "Bearer YOUR_API_KEY", "Content-Type": "application/json" };
    
    const first = await fetch(API, {
      method: "POST",
      headers,
      body: JSON.stringify({ message: "Can I change my delivery address?", userId: "customer_1042", stream: false }),
    }).then((r) => r.json());
  2. Save the conversation id against the customer
    JavaScript
    const conversationId = first.data.metadata.conversationId;
    // Store conversationId with customer_1042 in your database
  3. Continue it later
    JavaScript
    const second = await fetch(API, {
      method: "POST",
      headers,
      body: JSON.stringify({ message: "It's 14 Harbour Street, Wellington.", conversationId, stream: false }),
    }).then((r) => r.json());
    
    console.log(second.data.parts);

Explore the documentation