# Chat with an agent > Send a customer's message to an agent and get its answer, streamed or in one response This is the Chat API's main endpoint. Send the customer's message and QwryAI answers with the agent's live instructions, knowledge and actions, just as it would in the Chat bubble. Leave out `conversationId` to start a new conversation; send it to continue one. The answer streams by default; send `"stream": false` to get it as one JSON object. Conversations started here appear in the **Inbox** under the **API** channel, and each answer uses message credits. ```http POST https://api.qwryai.com/api/v2/agents/{agentId}/chat ``` ## Authentication Send your API key as `Authorization: Bearer `. The key needs **Chat with agents**. See [Authentication](/docs/api-reference/v2/authentication). ## Path parameters | Name | Type | Required | Description | | - | - | - | - | | `agentId` | `string` | Yes | The agent's id, from `list_agents`. | ## Body parameters | Name | Type | Required | Description | | - | - | - | - | | `message` | `string` | Yes, unless `conversationId` is sent | The customer's message. Leave it out only to let the agent continue after you [submit a tool result](/docs/api-reference/v2/agents/submit-a-tool-result). | | `conversationId` | `string` | No | The conversation to continue, from an earlier answer's metadata. Leave it out to start a new conversation. | | `stream` | `boolean` | No | Stream the answer as Server-Sent Events. Defaults to `true`. | | `userId` | `string` | No | Your id for the customer, on a new conversation. Up to 128 characters from `a-z A-Z 0-9 . _ -`. Ignored when `conversationId` is sent. See [User Conversations](/docs/api-reference/v2/user-conversations). | Any other field is refused with `VALIDATION_INVALID_BODY`. The body can be at most 1 MB. ## Request example > [tab] cURL > > ```bash > curl -X POST 'https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/chat' \ > -H 'Authorization: Bearer YOUR_API_KEY' \ > -H 'Content-Type: application/json' \ > -d '{ > "message": "Where is my order 1042?", > "userId": "customer_1042", > "stream": false > }' > ``` > [tab] JavaScript > > ```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: "Where is my order 1042?", > userId: "customer_1042", > stream: false, > }), > } > ); > const { data } = await response.json(); > console.log(data.parts, data.metadata.conversationId); > ``` > [tab] Python > > ```python > import requests > > response = requests.post( > "https://api.qwryai.com/api/v2/agents/YOUR_AGENT_ID/chat", > headers={"Authorization": "Bearer YOUR_API_KEY"}, > json={ > "message": "Where is my order 1042?", > "userId": "customer_1042", > "stream": False, > }, > ) > data = response.json()["data"] > print(data["parts"], data["metadata"]["conversationId"]) > ``` ## Response > [tab] Non-streaming > > With `"stream": false`, you get the whole answer once it's finished: > > ```json > { > "data": { > "id": "5b0e7c1a-2f4d-4c55-9a51-6d8e3f2b9c10", > "role": "assistant", > "parts": [ > { "type": "text", "text": "Order 1042 shipped on Monday with UPS and should arrive by Friday." } > ], > "metadata": { > "conversationId": "fd5ae9e1-9b30-4503-b346-ccd4d7ef16a3", > "userMessageId": "0c4f6a2e-8d13-4b7a-b0e9-1a2c3d4e5f60", > "userId": "customer_1042", > "finishReason": "stop", > "usage": { "credits": 1 } > } > } > } > ``` > [tab] Streaming > > By default the response is `text/event-stream`. Each event is a `data:` line with a JSON object, and the stream ends with `data: [DONE]`: > > ``` > data: {"type":"start","messageId":"5b0e7c1a-2f4d-4c55-9a51-6d8e3f2b9c10"} > > data: {"type":"start-step"} > > data: {"type":"text-start","id":"text_0"} > > data: {"type":"text-delta","id":"text_0","delta":"Order 1042 shipped"} > > data: {"type":"text-end","id":"text_0"} > > data: {"type":"finish-step"} > > data: {"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}}} > > data: [DONE] > ``` > > See [Streaming](/docs/api-reference/v2/streaming) for every event type. | Field | Type | Description | | - | - | - | | `data.id` | `string` | The answer's id. Pass it as `messageId` to [Retry a message](/docs/api-reference/v2/agents/retry-a-message). | | `data.role` | `string` | Always `assistant`. | | `data.parts` | `array` | The answer in order: `text` parts, a `tool-call` part for each action the agent called, and a `tool-result` part for each action QwryAI ran. | | `data.metadata.conversationId` | `string` | The conversation's id. Send it with the next message. | | `data.metadata.userMessageId` | `string` | The id of the customer message this answer replies to. | | `data.metadata.userId` | `string \| null` | The conversation's `userId`, or `null`. | | `data.metadata.finishReason` | `string` | `stop` when the answer is complete, `tool-calls` when it waits for a [client action](/docs/api-reference/v2/client-actions) result, `error` if it failed. | | `data.metadata.usage.credits` | `number` | Message credits this answer used. | > [!NOTE] > > Once a stream has started, a failure can't change the HTTP status. It arrives as an `error` event, then `finish` with `finishReason: "error"`. An answer still unfinished after 10 minutes ends that way. ## Errors | HTTP status | Code | When | | - | - | - | | 400 | `VALIDATION_INVALID_BODY` | No `message` on a new conversation, an unknown field, a bad `userId`, or a body over 1 MB. `details` names each field. | | 400 | `VALIDATION_INVALID_JSON` | The body isn't valid JSON. | | 401 | `AUTH_MISSING_API_KEY` | No `Authorization` header. | | 401 | `AUTH_INVALID_API_KEY` / `AUTH_EXPIRED_API_KEY` | The key is malformed, unknown, deleted or expired. | | 402 | `CHAT_CREDITS_EXHAUSTED` | The workspace doesn't have enough message credits for this answer. | | 403 | `AUTH_INSUFFICIENT_PERMISSIONS` | The key lacks **Chat with agents**, or its creator left the workspace. | | 403 | `SUBSCRIPTION_API_RESTRICTED_PLAN` | The workspace's plan doesn't include API access. | | 403 | `ACCESS_COUNTRY_BLOCKED` | The request comes from a country the agent blocks in **Guardrails**. | | 403 | `CHAT_CONVERSATION_MISMATCH` | The `conversationId` doesn't exist or belongs to another agent. | | 404 | `AGENT_NOT_FOUND` | No agent with that id in your workspace. | | 409 | `CHAT_CONVERSATION_NOT_ONGOING` | The conversation ended, a teammate took it over, its AI replies are paused, or another answer is still being written. | | 429 | `RATE_LIMIT_TOO_MANY_REQUESTS` | Too many requests, or too many open streams. Wait for `Retry-After` seconds. | | 500 | `INTERNAL_SERVER_ERROR` | Something failed on our side. Quote the `x-request-id` header to support. | See [Error Handling](/docs/api-reference/v2/error-handling) for every code.