# Add a knowledge base document

> Adds a file of the workspace (uploaded with the files API) to a knowledge base as a document, and processes it. The file must be a document Stacksync can read (PDF, Word, Excel, CSV, JSON, Markdown or plain text) and not already a document of the knowledge base. With validate_only, the checks run and 204 answers, nothing is added.

Source: https://docs.stacksync.com/api-reference/knowledge-bases/documents/add_knowledge_base_document

`POST https://api.stacksync.com/v1/workspaces/{workspace_id}/knowledge-bases/{knowledge_base_id}/documents`

- Operation: `add_knowledge_base_document`
- Resource: Knowledge base documents
- Access: writes data

## Authentication

- `Authorization: Bearer <key>`: A workspace API key (workspace_…, created by a workspace owner under Workspace settings > Stacksync API) acts with editor rights on that workspace only; a user API key (user_…, created under Profile > Stacksync API) acts as that user in every workspace they belong to. POST, PUT and PATCH requests must send Content-Type: application/json. Every authentication failure answers 403.
- `Content-Type: application/json` on every request with a body.

## Path parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `workspace_id` | integer | Yes | The workspace. List the ones your key can reach with GET /v1/workspaces. |
| `knowledge_base_id` | string (uuid) | Yes | The knowledge base. |

## Query parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `region` | string (besg \| usnv) | Yes | Region of the resource: besg (Europe) or usnv (United States), as GET /v1/regions lists them |

## Header parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | string \| null | No | Makes the create retry-safe for 24 hours: a retry with the same key answers the resource the first call created, with the Idempotent-Replayed: true header, and creates nothing. 1 to 255 visible ASCII characters. The same key with a different request answers 422; while the first call still runs, 409. |

## Request body

`application/json`

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `file_id` | string (uuid) | Yes | Id of a file of the workspace (GET /files), added as the document |
| `validate_only` | boolean, default false | No | Run every check of the write and answer 204 without writing: a refused write answers the error the write would |

## Returns

### 201 Created

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `document_id` | string | Yes |  |
| `knowledge_base_id` | string | Yes |  |
| `title` | string | Yes |  |
| `mime_type` | string | Yes |  |
| `file_id` | string | Yes |  |
| `processing_status` | string \| null | Yes |  |
| `processing_error` | string \| null | Yes |  |
| `chunk_count` | integer \| null | Yes |  |
| `created_time` | string (date-time) | Yes |  |

### 204 No Content

No body.

## Example request

```bash
curl -X POST "https://api.stacksync.com/v1/workspaces/{workspace_id}/knowledge-bases/{knowledge_base_id}/documents?region=besg" \
  -H "Authorization: Bearer $STACKSYNC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "file_id": "3f6c1d2e-8a4b-4c7d-9e21-5b6f7a8c9d01"
}'
```

## Example response (201)

```json
{
  "document_id": "<document_id>",
  "knowledge_base_id": "<knowledge_base_id>",
  "title": "<title>",
  "mime_type": "<mime_type>",
  "file_id": "<file_id>",
  "processing_status": "<processing_status>",
  "processing_error": "<processing_error>",
  "chunk_count": 0,
  "created_time": "2027-01-15T09:00:00Z"
}
```

## Errors

| Status | Meaning | Body |
| --- | --- | --- |
| 400 | Bad Request | `{ "error": string, "msg": string, "field": string \| null }` |
| 403 | Forbidden | `{ "error": string, "msg": string, "field": string \| null }` |
| 404 | Not Found | `{ "error": string, "msg": string, "field": string \| null }` |
| 409 | Conflict | `{ "error": string, "msg": string, "field": string \| null }` |
| 422 | Unprocessable Content | `{ "error": string, "msg": string, "field": string \| null }` |

## Related

- [API Reference overview](https://docs.stacksync.com/api-reference): base URLs, regions, pagination, idempotency and errors
- [Authentication](https://docs.stacksync.com/api-reference/authentication): API keys and the 403 answers
- OpenAPI document: https://docs.stacksync.com/openapi.json
- [List knowledge base documents](https://docs.stacksync.com/api-reference/knowledge-bases/documents/list_knowledge_base_documents)
- [Get a knowledge base document](https://docs.stacksync.com/api-reference/knowledge-bases/documents/get_knowledge_base_document)
- [Remove a knowledge base document](https://docs.stacksync.com/api-reference/knowledge-bases/documents/remove_knowledge_base_document)