## Add a block to a message

`POST /v1/chats/messages/:messageId/blocks`

Adds a block to the end of a constructor message — a message assembled from blocks.

## Parameters

| Parameter | Type | Required | Default | Description |
|----------|-----|:-----:|-----------|---------|
| `messageId` (path) | number | yes | — | ID of the constructor message, a positive integer — `data.id` of the [v2 send](/docs/chats/messages/send) response or the `id` of a message with a non-null `block` in [Messages around a message](/docs/chats/messages/context) |

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|---------|
| `element` | object | yes | A block in the Bitrix24 constructor format: `type` and the fields of that type, for example `{ "type": "text", "text": "Build passed" }` |

Other body fields and query parameters are rejected with `400 INVALID_PARAMS`.

## Examples

### curl — personal key

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/chats/messages/1001/blocks" \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "element": { "type": "text", "text": "Build passed" } }'
```

### curl — OAuth application

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/chats/messages/1001/blocks" \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "element": { "type": "text", "text": "Build passed" } }'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/messages/1001/blocks', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ element: { type: 'text', text: 'Build passed' } }),
})

const { data } = await res.json()
console.log('Block added:', data.result)
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/messages/1001/blocks', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ element: { type: 'text', text: 'Build passed' } }),
})

const { data } = await res.json()
console.log('Block added:', data.result)
```

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | Always `true` on success |
| `data.result` | boolean | `true` — the block was added |

## Response example

```json
{
  "success": true,
  "data": { "result": true }
}
```

## Error response example

422 — the message has no blocks:

```json
{
  "success": false,
  "error": {
    "code": "BITRIX_ERROR",
    "message": "BLOCK_NOT_FOUND",
    "b24Code": "BLOCK_NOT_FOUND"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `INVALID_PARAMS` | `messageId` is not a positive integer, the body is not a JSON object, `element` is missing or not an object, or an extra body field or query parameter is passed. Checked before any call to Bitrix24 |
| 404 | `ENTITY_NOT_FOUND` | Bitrix24 returned a generic "not found" code or an error with the text "not found". The portal code is in `error.b24Code` |
| 422 | `BITRIX_ERROR` | Bitrix24 returned an error, the portal code is in `error.b24Code`. `MESSAGE_NOT_FOUND` — there is no message with this ID, `BLOCK_NOT_FOUND` — the message has no blocks, `WRONG_ELEMENT_TYPE` and `EMPTY_TEXT_FIELD` — the block is malformed, `BLOCK_LENGTH_EXCEEDED` — the message blocks exceed the length limit, `MESSAGE_ACCESS_DENIED` — you have no permission to edit the message, for example it belongs to someone else |
| 403 | `SCOPE_DENIED` | The API key does not have the `im` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The key is read-only — see [access rights](/docs/access-rights) |
| 401 | `TOKEN_MISSING` | The API key has no Bitrix24 tokens configured |

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

## Known specifics

**Only constructor messages have blocks.** A message becomes a constructor message when it is sent with the `block` field in [v2 send mode](/docs/chats/messages/send) or when blocks are set on it by [editing the message](/docs/chats/messages/update) in v2 mode. A plain message has no blocks.

**The response does not include the new block's ID.** Block IDs come in the `block.elements[].id` field of the message, for example in the response of [Messages around a message](/docs/chats/messages/context).

## See also

- [Replace a block of a message](/docs/chats/messages/blocks-update)
- [Remove a block from a message](/docs/chats/messages/blocks-delete)
- [Messages around a message](/docs/chats/messages/context)
- [Send a message](/docs/chats/messages/send)
- [Edit a message](/docs/chats/messages/update)
