## Open a client chat

`POST /v1/openlines/sessions/open`

Finds an Open Channel chat by the client's code in the connector. The identifier of the found chat is needed to pick up the dialog and perform operator actions.

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|----------|
| `userCode` | string | yes | The client's code in the connector, in the form `connector\|lineId\|externalChatId\|userId`. Passed unchanged. Source: the `userCode` field in the [`POST /v1/openlines/sessions/search`](/docs/openlines/sessions/search) response |

## Examples

### curl — personal key

```bash
curl -X POST https://vibecode.bitrix24.com/v1/openlines/sessions/open \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"userCode": "doccheck|59|cloud-chat-2|1575"}'
```

### curl — OAuth application

```bash
curl -X POST https://vibecode.bitrix24.com/v1/openlines/sessions/open \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"userCode": "doccheck|59|cloud-chat-2|1575"}'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/openlines/sessions/open', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ userCode: 'doccheck|59|cloud-chat-2|1575' }),
})

const { data } = await res.json()
console.log('Chat ID:', data.chatId)
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/openlines/sessions/open', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ userCode: 'doccheck|59|cloud-chat-2|1575' }),
})

const { data } = await res.json()
```

## Response fields

| Field | Type | Description |
|------|-----|----------|
| `success` | boolean | `true` on a successful response |
| `data.chatId` | number | Open Channel chat identifier. See [Chat ID](/docs/openlines/operator#chat-id) |

## Response example

```json
{
  "success": true,
  "data": {
    "chatId": 5255
  }
}
```

## Error response example

400 — the client's code is missing:

```json
{
  "success": false,
  "error": {
    "code": "MISSING_REQUIRED_FIELDS",
    "message": "userCode is required and must be a non-empty string"
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|----------|
| 400 | `MISSING_REQUIRED_FIELDS` | The `userCode` field is missing, is not a string, is empty or consists of whitespace |
| 422 | `BITRIX_ERROR` | Bitrix24 rejected the request. The refusal text is in `message` |
| 403 | `BITRIX_ACCESS_DENIED` | The employee the call runs as has no permission for the action, or there is no client with this `userCode`. Both causes return the same response |
| 502 | `OPENLINE_OPERATOR_FAILED` | Bitrix24 did not return a chat identifier |
| 403 | `SCOPE_DENIED` | The key has no `imopenlines` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The key is read-only. Finding a chat by the client's code requires a key with write access |
| 401 | `MISSING_API_KEY` | The `X-Api-Key` header is missing |
| 401 | `INVALID_API_KEY` | The key is not recognized |
| 401 | `TOKEN_MISSING` | A personal key has no access to the Bitrix24 portal, or an application key was sent without a session token in `Authorization` |
| 502 | `BITRIX_UNAVAILABLE` | Bitrix24 is unavailable or the connection to it broke before the response was complete |

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

## Known specifics

- **The call does not start a session.** For a dialog with a closed session, the method returns `chatId`, and the session stays closed. To pick up the dialog, use [`POST /v1/openlines/sessions/start`](/docs/openlines/sessions/start).

## See also

- [Session search](/docs/openlines/sessions/search)
- [Operator actions and my pause](/docs/openlines/operator)
- [Open Channels](/docs/openlines)
