# Ask a question with the API

> Send a question to POST /v1/query and get a JSON answer grounded in your content, with the sources it came from. Request fields, response and error codes.

`POST /v1/query` sends one question to your agent and returns a JSON answer grounded in your content, with the sources it was drawn from. It is synchronous: you get the whole answer in one response.

## Before you start

- Create an API key. See [API keys](/developers/api-keys/). The key identifies the workspace, so there is no workspace id in the body.
- API access starts on Starter. The Free plan does not include it.
- Every successful answer uses message credits, exactly like an answer on any other channel. Base models cost 1 credit, premium models cost more.

## Endpoint

```
POST https://api.askvault.co/v1/query
```

Send `Authorization: Bearer ak_xxx` and `Content-Type: application/json`.

## Request body

```json
{
  "query": "What is your refund policy?",
  "session_id": "user_123",
  "top_k": 8
}
```

- **`query`** (required). The question, up to 4,000 characters. An empty string is rejected.
- **`session_id`** (optional). Your key for one end user, up to 200 characters. Use a stable value per person so each user's conversation stays separate under one API key.
- **`top_k`** (optional). How many passages from your knowledge are given to the agent, 1 to 20. The default is 8. A higher value gives the agent more to read.
- **`conversation_id`** (optional). The id of a conversation, as returned by an earlier answer. Prefer `session_id` to keep a person's thread together.

Extra fields in the body are ignored.

## Example

## Response

```json
{
  "answer": "Refunds are available within 14 days of purchase.",
  "sources": [
    {
      "document_name": "Refund Policy",
      "chunk_text": "Refunds are available within 14 days of purchase...",
      "relevance_score": 0.92,
      "url": "https://yourstore.com/refund-policy"
    }
  ],
  "tokens_used": 450,
  "query_id": "msg_xxx",
  "conversation_id": "conv_xxx"
}
```

Those five fields are the whole body.

- **`sources`** holds up to `top_k` passages. Each has the `document_name`, the exact `chunk_text`, a `relevance_score` and the page `url`. The `url` is `null` for uploaded files and for older content stored without one.
- **`query_id`** identifies this answer. Log it and quote it when you contact support.
- **`conversation_id`** is `null` when the question did not start or continue a conversation.

## When the answer is not in your content

With strict answering on, which is the default, a question that matches nothing in your knowledge is not sent to the model. You get an empty `sources` array and a short fixed reply saying the agent couldn't find an answer and suggesting the visitor ask another way. To detect a gap in code, check for the empty `sources` array, not the wording of the reply, which can change. Then add the missing content and ask again.

If something was retrieved but only partly covers the question, the agent answers from what it found. Judge coverage from `sources` and their relevance scores, not from the tone of the answer.

An empty `answer` string is not a knowledge gap. It means no text was generated. Retry once, and if it repeats, contact support with the `query_id`.

## Errors

Errors are JSON with a `detail` field: a string for most errors, and an object with an `error_code` (`messages_exhausted` or `model_locked`) for a 402. See [API errors and rate limits](/developers/errors-and-limits/) for retry advice.

| Code | Meaning | What to do |
|---|---|---|
| 200 | Success | Use `answer`. |
| 401 | API key missing, malformed or revoked | Check the Bearer token. |
| 402 | Message credits are used up, or the workspace's model is not on your plan | Buy extra credits, upgrade or pick another model. |
| 403 | Your plan does not include API access | Upgrade to Starter or above. |
| 404 | The key's workspace or its owner no longer exists | Create a new key. |
| 422 | Malformed JSON, or a field failed validation | Check the JSON, the `query` length and the `top_k` range. |
| 429 | The key's rate limit was hit, or your monthly message quota is used up | Read `detail`. Back off on a rate limit. A quota block does not clear by retrying. |
| 500 | Server error | Retry with exponential backoff. |

Requests rejected before an answer is written (401, 403, 422, 429) do not use message credits.

## Common questions

### Can I cache answers?

AskVault sends no cache headers on this endpoint. The first question in a new conversation can be answered from AskVault's answer cache, which is cleared when you change your knowledge. If you cache on your side, clear it when you update your knowledge.

### How do I stream the answer?

Call `POST /v1/query/stream` with the same body. See [Stream answers with the API](/developers/streaming/).

### How do I attach my user's identity?

`/v1/query` takes no user object. Use `POST /v1/agent/ask` for that, described in [Conversations API](/developers/conversations/). For visitors on your website, see [Identify signed-in visitors](/developers/identity-verification/).

## Related

- [AskVault API: get started](/developers/)
- [Stream answers with the API](/developers/streaming/)
- [API errors and rate limits](/developers/errors-and-limits/)

Source: https://docs.askvault.co/developers/query/
