
## Change the CoPilot role

`PUT /v1/chats/:dialogId/copilot/role`

Sets the role CoPilot uses when answering in a chat — the v2 messenger method `im.v2.Chat.Copilot.updateRole`. The role defines the style and specialization of the answers. Works only in a CoPilot chat.

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|---------|
| `dialogId` (path) | string | yes | Dialog ID of a CoPilot chat — `chatN`. Get it from `data.chat.dialogId` of the [draft chat](/docs/chats/copilot/draft) or from the [recent dialogs](/docs/chats/discovery/recent) with `onlyCopilot=true` |

Query parameters are not accepted and are rejected with `400 INVALID_PARAMS`.

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|---------|
| `role` | string\|null | yes | A role code, or `null` for the default role. Role codes are the keys of `data.copilot.roles` returned by [Load a chat](/docs/chats/messages/load) |

Other body fields are rejected with `400 INVALID_PARAMS`.

## Examples

### curl — personal key

```bash
curl -X PUT https://vibecode.bitrix24.com/v1/chats/chat2001/copilot/role \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"role": "copilot_assistant"}'
```

### curl — OAuth application

```bash
curl -X PUT https://vibecode.bitrix24.com/v1/chats/chat2001/copilot/role \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"role": "copilot_assistant"}'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/chat2001/copilot/role', {
  method: 'PUT',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ role: 'copilot_assistant' }),
})

const { success, data } = await res.json()
console.log('Role set:', data.result)
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/chat2001/copilot/role', {
  method: 'PUT',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ role: 'copilot_assistant' }),
})

const { success, data } = await res.json()
console.log('Role set:', data.result)
```

## Response fields

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

## Response example

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

## Error response example

400 — the `role` field was not sent:

```json
{
  "success": false,
  "error": {
    "code": "INVALID_PARAMS",
    "message": "`role` is required (a role code, or null for the default role)."
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `INVALID_PARAMS` | The `role` field is missing, is neither a non-empty string nor `null`, or another body field or a query parameter was sent. Checked before any call to Bitrix24 |
| 404 | `ENTITY_NOT_FOUND` | Bitrix24 returned "not found"; the portal code is in `error.b24Code` |
| 422 | `BITRIX_ERROR` | Bitrix24 refused; the portal code is in `error.b24Code`: `WRONG_MESSAGE_TYPE` — not a CoPilot chat, `IDENTICAL_ROLE` — the chat already has this role, `ROLE_NOT_FOUND` — no role with this code, `COPILOT_NOT_INSTALLED` — CoPilot is not installed on the portal |
| 502 | `ME_ALIAS_RESOLUTION_FAILED` | Failed to resolve the user when using the `me` alias |
| 403 | `SCOPE_DENIED` | The API key does not have the `im` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The key is read-only — the call changes the chat |
| 401 | `TOKEN_MISSING` | The API key has no Bitrix24 tokens configured |

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

## Known specifics

**`role` is required.** An empty body is rejected instead of silently resetting the role: for the default role, send `"role": null`.

**Setting the same role again is an error.** A role the chat already has is rejected with `IDENTICAL_ROLE`.

## See also

- [CoPilot](/docs/chats/copilot)
- [Change the CoPilot model](/docs/chats/copilot/engine)
- [Load a chat](/docs/chats/messages/load)
