> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fetchlake.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Query a Knowledge Base

> Send questions and receive answers with citations

## Endpoint

```
POST https://api.fetchlake.com/kb/{kb_id}/query
```

## Path Parameters

**kb\_id** `string` *required*

The unique identifier of the knowledge base to query.

## Request Headers

**Authorization** `string` *required*

Bearer token with your API key: `Bearer YOUR_API_KEY`

**Content-Type** `string` *required*

Must be `application/json`

## Request Body

**query** `string` *required*

The question or search query to send to the knowledge base.

## Example Request

```bash cURL theme={null}
curl -X POST https://api.fetchlake.com/kb/kb_edu_123/query \
  -H "Authorization: Bearer ak_abc123" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "What are the key curriculum standards for grade 5 math?"
  }'
```

```python Python theme={null}
import requests

response = requests.post(
    "https://api.fetchlake.com/kb/kb_edu_123/query",
    headers={
        "Authorization": "Bearer ak_abc123",
        "Content-Type": "application/json"
    },
    json={
        "query": "What are the key curriculum standards for grade 5 math?"
    }
)

data = response.json()
print(data["answer"])
```

```javascript JavaScript theme={null}
const response = await fetch('https://api.fetchlake.com/kb/kb_edu_123/query', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer ak_abc123',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    query: 'What are the key curriculum standards for grade 5 math?'
  })
});

const data = await response.json();
console.log(data.answer);
```

## Response

**answer** `string`

The generated answer to your query based on the knowledge base content.

**citations** `array`

List of source documents that support the answer.

**Citation object**

**document\_id** `string`

Unique identifier of the source document.

**title** `string`

Title of the source document.

**excerpt** `string`

Relevant excerpt from the document.

**page** `integer`

Page number where the information was found (if applicable).

**tokens\_used** `integer`

Number of tokens consumed by this query.

## Example Response

```json theme={null}
{
  "answer": "The key curriculum standards for grade 5 math include operations with fractions, understanding of place value to the thousandths, and introduction to coordinate geometry. Students should be able to add and subtract fractions with unlike denominators, multiply and divide fractions, and understand the relationship between fractions and decimals.",
  "citations": [
    {
      "document_id": "doc_123",
      "title": "Common Core Math Standards",
      "excerpt": "Students should be able to add and subtract fractions with unlike denominators (including mixed numbers) by replacing given fractions with equivalent fractions...",
      "page": 42
    },
    {
      "document_id": "doc_456",
      "title": "Grade 5 Mathematics Framework",
      "excerpt": "Place value understanding extends to the thousandths place, with students reading, writing, and comparing decimals...",
      "page": 15
    }
  ],
  "tokens_used": 150
}
```

## Error Responses

| Status | Error | Description |
| - | - | - |
| `400` | `invalid_query` | Query is empty or malformed |
| `401` | `unauthorized` | Missing or invalid API key |
| `403` | `forbidden` | Key doesn't have access to this KB |
| `429` | `query_cap_exceeded` | Monthly query limit reached |
| `429` | `rate_limit_exceeded` | Too many requests per minute |
| `500` | `internal_error` | Server error, retry later |

## Next Steps

> [card] [Rate Limits](https://docs.fetchlake.com/api-reference/rate-limits.md)
>
> Understand usage caps

> [card] [Error Handling](https://docs.fetchlake.com/api-reference/error-handling.md)
>
> Handle errors gracefully


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.