## Replace a block of a message

`PATCH /v1/chats/messages/:messageId/blocks/:blockId`

Replaces a block of a constructor message entirely.

## 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) |
| `blockId` (path) | string | yes | — | Block ID — the value of `block.elements[].id` of the message. Blocks of a message — [Messages around a message](/docs/chats/messages/context) |

## Request fields (body)

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

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

## Examples

### curl — personal key

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

### curl — OAuth application

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

### JavaScript — personal key

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

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

### JavaScript — OAuth application

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

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

## Response fields

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

## Response example

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

## Error response example

422 — there is no block with this ID:

```json
{
  "success": false,
  "error": {
    "code": "BITRIX_ERROR",
    "message": "ELEMENT_NOT_FOUND",
    "b24Code": "ELEMENT_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 a 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, `ELEMENT_NOT_FOUND` — there is no block with this ID, `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

**`element.id` does not change the block ID.** The block keeps the ID from the path, even if a different one is passed in `element.id`.

**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.

## See also

- [Add a block to a message](/docs/chats/messages/blocks-create)
- [Remove a block from a message](/docs/chats/messages/blocks-delete)
- [Messages around a message](/docs/chats/messages/context)
- [Edit a message](/docs/chats/messages/update)
