Skip to content
Try Free →

Stream answers with the API

POST /v1/query/stream takes the same request as POST /v1/query but answers as a Server-Sent Events stream, so the first words reach your user as soon as they are written. The total time is the same; the user just is not left looking at an empty box.

Before you start

Starter+
  • You need an API key. See API keys.
  • The request body is the one described in Ask a question with the API: query is required, and top_k, session_id and conversation_id are optional.
  • Streaming uses the same message credits and the same per-key rate limits as the synchronous endpoint.

Request

Terminal window
curl -N -X POST https://api.askvault.co/v1/query/stream \
-H "Authorization: Bearer ak_xxx" \
-H "Content-Type: application/json" \
-d '{"query": "How does pricing work?"}'

The frames

Every line is a plain data: <json> line followed by a blank line. There are no named event: types, so read the done boolean to tell a token from the last frame.

Token frames carry the next piece of the answer. Join the token values in order.

{"token": "Our ", "done": false}

Status frames come first and describe what the agent is doing, so you can show progress. The stage is understanding, search, read or write. A backend consumer can ignore them.

{"event": "status", "stage": "understanding", "text": "Understanding your question…"}

The last frame has done set to true and carries the sources for a normal answer.

{"token": "", "done": true, "sources": [{"document_name": "Refund Policy", "chunk_text": "Refunds are available within 14 days...", "url": "https://yourstore.com/refund-policy", "relevance_score": 0.94}], "tokens_used": 187, "conversation_id": "conv_xxx", "message_id": "msg_xxx"}

Error frames and frames sent while a person is handling the conversation also have done: true but no sources, so read sources as an empty list when it is missing. A few extra frames, such as suggested follow-up questions, can arrive after done. Keep reading until the connection closes.

Error frames always have done: true. The error text is a short message and varies:

{"error": "The service is busy. Please try again.", "done": true}

Use it from a browser

The browser's built-in EventSource cannot send an Authorization header or a POST body, so it cannot call this endpoint. Either use fetch with a stream reader as in the Node example, or call AskVault from your server and relay the stream. Never put an API key in front-end code. For a chat window on your website, use the website widget, which uses a public token.

Handle failures

  • Before the stream starts, you get the usual HTTP error with a detail message. See API errors and rate limits.
  • During the stream, you get an error frame. Close the connection, wait with exponential backoff, and retry the same request with the same session_id so context carries over. Stop after a few attempts.
  • If your user's connection drops, show what arrived and offer a regenerate button instead of retrying on their behalf.

To stop reading early, close the connection. Plan as if a cancelled stream still counts against your limits.

Check it worked

Run the curl command with -N. You should see data: lines appear one by one and a final line with "done":true and the sources.

If it doesn't work

Nothing appears until the end. Something between you and AskVault is buffering. Use curl -N, or turn off response buffering in your HTTP client or proxy.

Some words are missing. A hand-written parser split a line across network chunks. Buffer until you have a full line, or use a tested SSE library.

No final frame. The connection dropped. Treat what you have as the answer and offer a regenerate option.