# Context for bots

Passes an arbitrary context object to the chat's bots. Bitrix24 adds the chat data to it and calls the context handlers of the bots connected to the chat.

## Send context

`POST /v1/chats/:dialogId/bot-context`

The v2 messenger method `im.v2.Chat.Bot.sendContext`.

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|----------|
| `dialogId` (path) | string | yes | Dialog ID: `chat123` for a group chat, a user ID for a private chat, `me` for the chat with yourself |

The request body is a JSON object:

| Field | Type | Required | Description |
|------|-----|:-----:|----------|
| `context` | object | yes | Data for the bots — any JSON object. An array, a string, a number and `null` are rejected |

Any other body field and any query parameter are rejected with `400 INVALID_PARAMS`.

## Examples

### curl — personal key

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/chats/chat42/bot-context" \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"context": {"page": "deal", "dealId": 128}}'
```

### curl — OAuth application

```bash
curl -X POST "https://vibecode.bitrix24.com/v1/chats/chat42/bot-context" \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"context": {"page": "deal", "dealId": 128}}'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/chat42/bot-context', {
  method: 'POST',
  headers: { 'X-Api-Key': 'YOUR_API_KEY', 'Content-Type': 'application/json' },
  body: JSON.stringify({ context: { page: 'deal', dealId: 128 } }),
})

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

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/chat42/bot-context', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ context: { page: 'deal', dealId: 128 } }),
})

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

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | Always `true` on success |
| `data.result` | boolean | `true` — the context reached the bots' handlers |

## Response example

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

## Error response example

422 — the chat has no bots with a context handler:

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

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `INVALID_PARAMS` | `context` is missing or is not a JSON object, or another body field or query parameter was passed |
| 401 | `TOKEN_MISSING` | The API key has no Bitrix24 tokens configured |
| 403 | `SCOPE_DENIED` | The API key does not have the `im` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The key is read-only |
| 404 | `ENTITY_NOT_FOUND` | Bitrix24 returned "not found"; the portal code is in `error.b24Code` |
| 422 | `BITRIX_ERROR` | Bitrix24 returned an error; the portal code is in `error.b24Code`. `BOT_CONTEXT_ERROR` — the chat has no bots that accepted the context |
| 502 | `ME_ALIAS_RESOLUTION_FAILED` | Failed to resolve the current user for `me` |
| 502 | `BITRIX_UNAVAILABLE` | Bitrix24 is unavailable or returned a server error |

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

## Known specifics

- `context` is passed to the bots as is: the platform does not rename its keys.
- Bitrix24 adds to the context the chat type, the caller's ID, the chat author and the entity the chat is bound to.
- The call may make the caller a chat member if the chat allows auto-join.

## See also

- [Bot platform](/docs/bots)
- [Service methods](/docs/chats/service)
