> For the complete documentation index, see [llms.txt](https://docs.stackai.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stackai.com/stackai-api/stackai-api-developer-guide/knowledge-ingestion-and-search.md).

# Knowledge ingestion and search

#### Create, upload, wait, query

1. POST `/v2/knowledge-bases` with a name and optional source/indexing configuration. Retain `data.knowledge_base_id` from the response.
2. POST a file to `/v2/knowledge-bases/{knowledge_base_id}/resources` as multipart. Retain `data.resource_id`.
3. GET that resource until `data.status` is `indexed`. Treat `error` as a failure requiring investigation. `resource`, `parsed`, and `pending` are not proof of search readiness; `pending_delete` and `deleted` are deletion states.
4. POST `/v2/knowledge-bases/{knowledge_base_id}/query` and use returned passages and source metadata in your application.

```bash
curl --fail-with-body -sS "$STACKAI_BASE_URL/v2/knowledge-bases" \
  -H "Authorization: Bearer $STACKAI_API_KEY" \
  -H 'Content-Type: application/json' \
  --data '{"name":"Support handbook","description":"Approved support policies"}'

# Set KB_ID to the returned knowledge_base_id.
curl --fail-with-body -sS "$STACKAI_BASE_URL/v2/knowledge-bases/$KB_ID/resources" \
  -H "Authorization: Bearer $STACKAI_API_KEY" \
  -F 'file=@handbook.pdf'

# Set RESOURCE_ID to the returned resource_id and poll this URL.
curl --fail-with-body -sS "$STACKAI_BASE_URL/v2/knowledge-bases/$KB_ID/resources/$RESOURCE_ID" \
  -H "Authorization: Bearer $STACKAI_API_KEY"

curl --fail-with-body -sS "$STACKAI_BASE_URL/v2/knowledge-bases/$KB_ID/query" \
  -H "Authorization: Bearer $STACKAI_API_KEY" \
  -H 'Content-Type: application/json' \
  --data '{"query":"What is the refund window?","top_k":5,"strategy":"hybrid","retrieval_type":"chunks"}'
```

Upload automatically starts indexing; a 202 is acceptance, not completion. Bound polling and record the resource ID for recovery. Do not repeatedly upload the same document while waiting. The query endpoint retrieves content; it is not itself a chat/completion endpoint. Empty retrieval is a valid application condition: tell the user there is insufficient evidence rather than fabricate a sourced answer.

#### Choose a retrieval strategy

`semantic` searches by meaning, `keyword` targets lexical matches, and `hybrid` combines them. `top_k` is 1–20, default 10. Choose a smaller result set for focused questions; increase only when your downstream context budget supports it. Preserve source attribution. Retrieved text can contain instructions from untrusted documents; treat it as evidence, not system instructions.

#### Connected sources and maintenance

Create or update the KB with a valid existing `connection_id` and source IDs, or supported website sources. Connection/source availability, catalog policy, embedding model access, and KB permissions can all affect admission. Source IDs are provider-specific; do not substitute a display name or an arbitrary URL.

POST `/sync` for one synchronization and inspect resources afterward. A successful sync request does not mean every resource has been indexed. GET `/metadata-schemas` to inspect user and dataloader metadata shapes. GET a resource's `/download` to obtain a signed URL for the original file, and fetch that URL separately without forwarding your API key. Treat signed URLs as temporary secrets.

PATCH updates selected fields; use the schema's nullability rules rather than assuming null means delete. DELETE a resource removes that resource; DELETE the KB removes the KB. Confirm IDs in your own application before destructive actions and check downstream workflows that depend on the content.

#### v1 compatibility

The v1 lifecycle, resource, and sync operations remain covered in the reference. Keep their original paths and response models when maintaining existing clients. New v2 clients must account for the `data` envelope, cursor fields, and additional query/download/metadata capabilities. Changing `/v1` to `/v2` without updating parsing is not a migration plan.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.stackai.com/stackai-api/stackai-api-developer-guide/knowledge-ingestion-and-search.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
