## Attach a file to a task

`POST /v1/tasks/:taskId/files/attach`

Attaches a file that is already on Bitrix24 Drive to a task: the task gets an attachment, and the file itself is not copied.

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|----------|
| `taskId` (path) | number | yes | Task ID. List: [`GET /v1/tasks`](/docs/entities/tasks/list) |

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|----------|
| `fileId` | number | yes | Drive file ID — a positive integer, not prefixed with the letter `n`. List: [`GET /v1/files`](/docs/entities/files/list). To upload a new file, use [`POST /v1/files/upload`](/docs/entities/files/upload) |

## Examples

### curl — personal key

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/tasks/4233/files/attach" \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"fileId":10867}'
```

### curl — OAuth application

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/tasks/4233/files/attach" \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"fileId":10867}'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/tasks/4233/files/attach', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ fileId: 10867 }),
})

const { success, data } = await res.json()
console.log('Attachment ID:', data?.attachmentId)
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/tasks/4233/files/attach', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ fileId: 10867 }),
})

const { success, data } = await res.json()
```

## Response fields

| Field | Type | Description |
|------|-----|----------|
| `success` | boolean | `true` if the request succeeded |
| `data.attachmentId` | number | Attachment ID — the ID of the link between the file and the task, not the Drive file ID. The same number appears in the task's `ufTaskWebdavFiles` list: [`GET /v1/tasks/:id`](/docs/entities/tasks/get) |

## Response example

Drive file `10867` is attached to task `4233`, and the attachment has ID `597`:

```json
{
  "success": true,
  "data": {
    "attachmentId": 597
  }
}
```

## Error response example

422 — Drive has no file with this ID:

```json
{
  "success": false,
  "error": {
    "code": "BITRIX_ERROR",
    "message": "File could not be found.",
    "b24Code": "ERROR_CORE"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|----------|
| 400 | `INVALID_PARAMS` | The request failed validation for one of these reasons, and the message names it. `taskId` is not a positive integer: message `taskId must be a positive integer`. The body is not a JSON object, for example an array: message `JSON object body is required`. `fileId` is missing or is not a positive integer, including the string `"10867"` or `"n10867"`: message `fileId must be a positive integer` |
| 400 | `FST_ERR_CTP_EMPTY_JSON_BODY` | An empty body with the `Content-Type: application/json` header |
| 401 | `MISSING_API_KEY` | `X-Api-Key` is missing |
| 401 | `TOKEN_MISSING` | The key has no Bitrix24 tokens |
| 403 | `SCOPE_DENIED` | The key lacks the `task` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The key is in read-only mode. `error.details.switchUrl` contains the URL of the section where the mode is changed |
| 415 | `FST_ERR_CTP_INVALID_MEDIA_TYPE` | A body with a `Content-Type` the API does not parse, for example `application/xml` |
| 422 | `BITRIX_ERROR` | Bitrix24 refused the call; `error.b24Code` tells the reasons apart. `ERROR_CORE`: Drive has no file with this `fileId`; the message is in the Bitrix24 account language, and on a `.com` account it is `File could not be found.` The same `ERROR_CORE` arrives when the employee on whose behalf the call is made has no access to the file; the message is in the Bitrix24 account language. `0`: the employee has no access to the task; the message is `Access denied`. This employee gets the same response for the `taskId` of a nonexistent task. The API specification declares `404 TASK_NOT_FOUND` for this case, but the Bitrix24 accounts we tested return this response instead |
| 429 | `RATE_LIMITED` | Request rate exceeded. The effective cap is in the `x-ratelimit-limit` header |

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

## Known specifics

**Attaching the same file again adds a duplicate.** Each call with the same `fileId` adds one more attachment of that file to the task, and the response returns the ID of the first attachment. All task attachments are listed in the `ufTaskWebdavFiles` field of the [`GET /v1/tasks/:id`](/docs/entities/tasks/get) response. To keep a single copy, [update the task](/docs/entities/tasks/update): setting the `ufTaskWebdavFiles` field to `["n10867"]` replaces the entire attachment list with one attachment.

**A nonexistent `taskId` does not always return an error.** The response depends on the employee on whose behalf the call is made: one gets `422 BITRIX_ERROR`, another gets `200` with `attachmentId`. In the second case the attachment is created but is not visible in any task. Before the call, check the task with [`GET /v1/tasks/:id`](/docs/entities/tasks/get).

## See also

- [Checklist actions and task file](/docs/entities/tasks/checklist-actions)
- [Get task](/docs/entities/tasks/get)
- [Update task](/docs/entities/tasks/update)
- [Attach Drive files to a checklist item](/docs/entities/tasks/checklist-actions/attachments-disk)
- [Files](/docs/entities/files)
- [Tasks](/docs/entities/tasks)
