## Publish uploaded files

`POST /v1/chats/:chatId/files/uploads/publish`

Creates one message with previously uploaded files. `uploadIds` contains IDs from the upload route responses. Repeating the call after an unknown outcome can create a second message: the client needs its own idempotency ledger.

## Parameters

| Parameter | Type | Required | Description |
|-----------|------|:--------:|-------------|
| `chatId` (path) | number | yes | Chat ID |

## Request fields (body)

| Field | Type | Required | Description |
|-------|------|:--------:|-------------|
| `uploadIds` | number[] | yes | 1 to 10 positive uploaded file IDs. Get them: `data.file.id` in the response of [Upload a file to a chat folder](/docs/chats/files/upload-to-folder) |
| `message` | string | no | Caption |
| `replyId` | number | no | Message ID to reply to |
| `asFile` | boolean | no | Send as a file |
| `templateId` | string | no | Template ID |

Requires the `im` scope and a write-enabled key. Other fields and query parameters are rejected with `400 INVALID_PARAMS`.

The `message` text is limited to 20000 Unicode code points. The complete outgoing string is counted, including markup, URLs and mentions, without truncation or trimming. For example, `😀` is one code point, while a compound emoji may occupy several. The request is rejected before any call to Bitrix24.

If the caption exceeds the length limit, `uploadIds` remain available for publication with a corrected caption.

## Examples

### curl — personal key

```bash
curl -X POST https://vibecode.bitrix24.com/v1/chats/42/files/uploads/publish \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"uploadIds":[7701],"message":"Caption"}'
```

### curl — OAuth application

```bash
curl -X POST https://vibecode.bitrix24.com/v1/chats/42/files/uploads/publish \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"uploadIds":[7701],"message":"Caption"}'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/42/files/uploads/publish', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({"uploadIds": [7701], "message": "Caption"}),
})
const { success, data, error } = await res.json()
console.log(success ? data : error)
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/42/files/uploads/publish', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({"uploadIds": [7701], "message": "Caption"}),
})
const { success, data, error } = await res.json()
console.log(success ? data : error)
```

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | `true` on success |
| `data.messageId` | number | ID of the created message |

## Response example

```json
{
  "success": true,
  "data": {
    "messageId": 52601
  }
}
```

## Error response example

```json
{
  "success": false,
  "error": {
    "code": "INVALID_PARAMS",
    "message": "`uploadIds` must be an array of 1 to 10 positive integers."
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `MESSAGE_TOO_LONG` | More than 20000 Unicode code points in the outgoing text. `error.details` contains only `field: "message"`, `used`, `limit: 20000`, `unit: "unicode-code-points"`. No Bitrix24 call is made |
| 400 | `INVALID_PARAMS` | `uploadIds` is not an array of 1–10 positive integers; `message`, `replyId`, `asFile`, or `templateId` has the wrong type; or another body field or a query parameter was sent |
| 400 | `INVALID_CHAT_ID` | `chatId` in the path is not a positive integer |
| 401 | `TOKEN_MISSING` | No Bitrix24 access tokens |
| 403 | `SCOPE_DENIED` | The API key does not have the `im` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The key is read-only |
| 403 | `BITRIX_ACCESS_DENIED` | No access to the chat or file in Bitrix24 |
| 404 | `ENTITY_NOT_FOUND` | Chat or file not found |
| 422 | `BITRIX_ERROR` | Other Bitrix24 refusal |
| 429 | `QUEUE_TIMEOUT` | Bitrix24 request queue is busy |

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

## Known specifics

**The platform does not retry the call.** After an unknown outcome, check your own state before another call so you do not create a second message.

**The Bitrix24 code is not always present.** A named Bitrix24 refusal can arrive as `400`, `403`, or `404`. The `error.b24Code` field is not guaranteed in such a response.

## See also

- [Upload to folder](/docs/chats/files/upload-to-folder)
- [Check existence](/docs/chats/files/upload-status)
- [Discard file](/docs/chats/files/upload-discard)
- [Chat files](/docs/chats/files)
