## Create a document

`POST /v1/note/documents`

Creates a document in a knowledge base. A non-empty `markdown` creates a document with ready-made content. Without it, an empty document is created for collaborative editing.

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|---------|
| `collectionId` | integer | yes | Knowledge base identifier. List: `GET /v1/note/collections` |
| `title` | string | yes | Document title |
| `parentId` | integer | no | Parent document for nesting. Belongs to the same knowledge base. Document tree: `GET /v1/note/collections/:collectionId/documents` |
| `markdown` | string | no | Initial content in Markdown. Maximum `1 048 576` bytes (1 MiB) |

## Examples

### curl — personal key

```bash
curl -X POST https://vibecode.bitrix24.com/v1/note/documents \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "collectionId": 42,
    "title": "Chapter 1",
    "parentId": 10,
    "markdown": "# Chapter 1\n\nText"
  }'
```

### curl — OAuth application

```bash
curl -X POST https://vibecode.bitrix24.com/v1/note/documents \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "collectionId": 42,
    "title": "Chapter 1",
    "parentId": 10,
    "markdown": "# Chapter 1\n\nText"
  }'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/note/documents', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    collectionId: 42,
    title: 'Chapter 1',
    parentId: 10,
    markdown: '# Chapter 1\n\nText',
  }),
})
const { success, data } = await res.json()
console.log(data.id)
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/note/documents', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    collectionId: 42,
    title: 'Chapter 1',
    parentId: 10,
    markdown: '# Chapter 1\n\nText',
  }),
})
const { success, data } = await res.json()
```

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | Always `true` on success |
| `data.id` | number | Identifier of the created document |

## Response example

```json
{
  "success": true,
  "data": { "id": 77 }
}
```

## Error response example

400 — a required parameter is missing:

```json
{
  "success": false,
  "error": {
    "code": "INVALID_PARAMS",
    "message": "`collectionId` is required and must be a positive integer"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `INVALID_PARAMS` | `collectionId` or `title` is missing, or a parameter has the wrong type |
| 403 | `BITRIX_ACCESS_DENIED` | No permission to create documents in the knowledge base |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The access key is read-only |
| 404 | `ENTITY_NOT_FOUND` | The knowledge base or the parent document does not exist |
| 422 | `BITRIX_ERROR` | `parentId` from a different knowledge base, or the `markdown` size exceeds the limit. Error text in `error.message` |
| 502 | `BITRIX_UNAVAILABLE` | Bitrix24 is unavailable |
| 403 | `SCOPE_DENIED` | The key lacks the `note` scope |
| 401 | `TOKEN_MISSING` | The API key has no tokens configured |

Full list of common API errors — [Errors](/docs/errors).

## See also

- [Update a document](/docs/note/documents/update)
- [Get a document](/docs/note/documents/get)
- [Knowledge bases](/docs/note/collections)
- [Knowledge base overview](/docs/note)
