# Add a file to a knowledge base

Source: [Add a file to a knowledge base](<https://docs.gptunnel.ru/en/rag/file-add/>)

<a id="_top"></a>
> **Note: API key access**
>
> Get an API key from your [personal or business profile](<https://docs.gptunnel.ru/en/#get-an-api-key>) and send it in the `Authorization` header.

 <a id="добавление-файла-в-rag"></a>
 
`POST https://gptunnel.ru/v1/database/file/add`

Send the text and the `databaseId` from the [knowledge base list](<https://docs.gptunnel.ru/en/rag/list/>). The endpoint queues the file for processing. Save the response’s `id` and send it as `fileId` when you [check the file’s status](<https://docs.gptunnel.ru/en/rag/file-list/>).

### Request headers

| Parameter | Type | Description |
| --- | --- | --- |
| `Authorization` (required) | `string` | API key |

### Request parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `databaseId` (required) | `string` | Knowledge base ID |
| `text` (required) | `string` | File contents as text |
| `name` | `string` | File name |
| `chunkSize` | `number` | Chunk size in tokens. Integer from 0 to 32767. The default is 1024, which is also used when you send 0. |
| `overlapSize` | `number` | Overlap between adjacent chunks in tokens. Integer from 0 to 32767. The default is 128. Set it to 0 to disable overlap. |
| `splitVariable` | `string` | Text delimiter, up to 12 characters. If this field is nonempty, splitting ignores chunkSize and overlapSize. |

When processing the file, the service raises chunk sizes below 256 tokens to 256 and caps overlap at half the chunk size. With `splitVariable`, it splits the text at the delimiter first. Longer pieces are then split into chunks of up to 4096 tokens without overlap.

<a id="example-request"></a>
## Example request

Set `GPTUNNEL_API_KEY` to your API key and `GPTUNNEL_DATABASE_ID` to the ID from the [knowledge base list](<https://docs.gptunnel.ru/en/rag/list/>). The cURL example requires version 8.3 or later.

<a id="tab-panel-0-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

fields = {
    'databaseId': os.environ['GPTUNNEL_DATABASE_ID'],
    'name': 'Sample document',
    'text': 'The sample document code is NORTH-17.',
}
request = Request(
    'https://gptunnel.ru/v1/database/file/add',
    headers={'Authorization': os.environ['GPTUNNEL_API_KEY'], 'Content-Type': 'application/json'},
    method='POST',
    data=json.dumps(fields).encode('utf-8'),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response))
```

<a id="tab-panel-0-1"></a>
**cURL**

```bash
curl --fail-with-body \
  'https://gptunnel.ru/v1/database/file/add' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --variable '%GPTUNNEL_DATABASE_ID' \
  --header 'Content-Type: application/json' \
  --expand-data '{"databaseId": "{{GPTUNNEL_DATABASE_ID:json}}", "name": "Sample document", "text": "The sample document code is NORTH-17."}'
```

<a id="tab-panel-0-2"></a>
**JavaScript (Node.js)**

```javascript
const key = process.env.GPTUNNEL_API_KEY
if (!key) throw new Error('Set GPTUNNEL_API_KEY')
const databaseId = process.env.GPTUNNEL_DATABASE_ID
if (!databaseId) throw new Error('Set GPTUNNEL_DATABASE_ID')

const url = new URL('https://gptunnel.ru/v1/database/file/add')
const response = await fetch(url, {
  method: 'POST',
  headers: { Authorization: key, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    databaseId,
    name: 'Sample document',
    text: 'The sample document code is NORTH-17.',
  }),
})
if (!response.ok) throw new Error(await response.text())
console.log(await response.json())
```

<a id="example-response"></a>
## Example response

Identifiers and time are given for example.

<a id="tab-panel-1-0"></a>
**200 OK**

```json
{
  "id": "66a346f64dcb57627c03d596",
  "createDate": "2024-07-26T06:49:27.430Z",
  "status": "idle",
  "name": "Sample document",
  "databaseId": "669b1824c0d8dbde07d07d10",
  "chunkSize": 1024,
  "overlapSize": 128,
  "splitVariable": null,
  "tokenCount": 0,
  "tokenCost": "0",
  "error": null,
  "progress": null,
  "chunkCount": 0
}
```

<a id="tab-panel-1-1"></a>
**401 Unauthorized**

```json
{
  "error": {
    "message": "Unauthorized",
    "type": "unauthorized_request_error",
    "param": null,
    "code": null
  }
}
```
