# 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`: | Constraint | Value | | - | - | | Max length | 128 characters | | Allowed characters | `a-z`, `A-Z`, `0-9`, `.`, `_`, `-` | | When applied | Only when a conversation starts (no `conversationId` in the request) | | Mutability | Fixed for the life of the conversation | > [!WARNING] > > A conversation's `userId` is set once, when it starts. If you send `userId` together with `conversationId`, the `userId` is ignored. A `userId` with any other character, such as `@` or a space, is refused with `VALIDATION_INVALID_BODY`, so send your internal customer id, not an email address. ```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, ) ``` > [!TIP] > > The `conversationId` comes back in the stream's `finish` event under `messageMetadata`, and in the non-streaming response under `data.metadata`. See [Streaming](/docs/api-reference/v2/streaming). > [!NOTE] > > Once a teammate takes the conversation over, or it ends, the Chat API returns `409 CHAT_CONVERSATION_NOT_ONGOING`. Start a new conversation with the same `userId` instead. ## 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**: | Tool | Returns | | - | - | | `query_conversations` | Conversations in a date range, 20 per page, from every channel, with their AI summary, channel, state and sentiment. | | `get_conversation_detail` | One conversation and its messages, 20 per page. | | `search_conversations` | Conversations whose messages contain a phrase. See [Conversation Search](/docs/api-reference/v2/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 | Argument | Type | Required | Description | | - | - | - | - | | `chatbotId` | `string` | Yes | The agent's id from `list_agents`. | | `startDate`, `endDate` | `string` | Yes | Creation dates, `YYYY-MM-DD` in UTC, both included, at most 184 days apart. | | `page` | `integer` | Yes | The page, from `1`. | | `sources` | `string[]` | No | Channels, such as `api`, `web` or `whatsapp`. `get_conversation_facets` lists the values in use. | | `activity_states` | `string[]` | No | `ongoing`, `ended`, `taken_over`, `paused`. | | `contact` | `string` | No | Matches the contact's name or email. | | `feedback` | `string` | No | `good` or `bad`. | | `escalated` | `boolean` | No | `true` 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 | Field | Description | | - | - | | `conversationId` | The id the Chat API uses. | | `_id` | The id the Tools API's conversation tools take as `conversationId` (not shown above). | | `channel` | Where the conversation happened, such as `api`, `web` or `whatsapp`. | | `lifecycle` | `open` while it's going on. | | `metadata.aiSummary` | QwryAI's one-line summary, once it has one. | | `timestamps` | When it started and when it was last active. | | `metrics` | Message counts, in total and from the AI and your team. | | `insights` | Sentiment and topics, once analysed. | ### Pagination Examples Raise `page` until `hasMore` is `false`. See [Pagination](/docs/api-reference/v2/pagination) for the full loop. > [!NOTE] > > `query_conversations` filters by contact name or email, not by `userId`. Keep the `conversationId`s you start for each customer in your own system to find their history. ## In mobile apps The [iOS](/docs/ios-sdk/conversations) and [Android](/docs/android-sdk/conversations) 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](/docs/ios-sdk/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); ``` ## Related > [card] Streaming | /docs/api-reference/v2/streaming | Read answers as they're written. > [card] Pagination | /docs/api-reference/v2/pagination | Page through conversation lists. > [card] Conversation Search | /docs/api-reference/v2/conversation-search | Find conversations by what was said. > [card] Client Actions | /docs/api-reference/v2/client-actions | Let the agent ask your app to run code.