## Dialog ID by external identifier

`POST /v1/chats/dialog-id`

Returns the dialog ID as `chatN` for an external identifier: a user ID, a CRM entity, a workgroup or the chat itself — the v2 messenger method `im.v2.Chat.getDialogId`. **The method gets or creates a chat:** if the identifier has no chat yet, Bitrix24 creates one, so the endpoint is a `POST` and counts as a write. Bitrix24 marks the method as internal.

## Request fields (body)

| Field | Type | Required | Description |
|------|-----|:-----:|---------|
| `externalId` | string | yes | The external identifier — one of the forms below. Surrounding spaces are trimmed |

| `externalId` form | What Bitrix24 does |
|--------------------|----------------------|
| `chatN`, for example `chat42` | Returns the same value without checking that the chat exists |
| A user ID, for example `5` | Returns the personal dialog with this user; creates it if there was none |
| `crm\|<TYPE>\|<ID>`, for example `crm\|DEAL\|15` | Returns the CRM entity chat; if there is none, creates it and makes the caller a member |
| `sgN`, for example `sg3` | Returns the workgroup chat; creates it if there is none |

Any other form returns `422 BITRIX_ERROR` with `error.b24Code` set to `CHAT_NOT_FOUND`. Other body fields and query parameters are rejected with `400 INVALID_PARAMS` before any call to Bitrix24.

## Examples

### curl — personal key

```bash
curl -X POST https://vibecode.bitrix24.com/v1/chats/dialog-id \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "externalId": "crm|DEAL|15" }'
```

### curl — OAuth application

```bash
curl -X POST https://vibecode.bitrix24.com/v1/chats/dialog-id \
  -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "externalId": "crm|DEAL|15" }'
```

### JavaScript — personal key

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/dialog-id', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ externalId: 'crm|DEAL|15' }),
})

const { data } = await res.json()
console.log('Dialog:', data.dialogId) // chat264
```

### JavaScript — OAuth application

```javascript
const res = await fetch('https://vibecode.bitrix24.com/v1/chats/dialog-id', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'YOUR_APP_KEY',
    'Authorization': 'Bearer USER_SESSION_TOKEN',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ externalId: 'crm|DEAL|15' }),
})

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

## Response fields

| Field | Type | Description |
|------|-----|---------|
| `success` | boolean | Always `true` on success |
| `data.dialogId` | string | The dialog ID as `chatN` |

## Response example

```json
{
  "success": true,
  "data": {
    "dialogId": "chat42"
  }
}
```

## Error response example

400 — `externalId` was not passed:

```json
{
  "success": false,
  "error": {
    "code": "MISSING_PARAMS",
    "message": "Body field `externalId` (chat<N>, a user id, crm|<TYPE>|<ID> or sg<N>) is required. Nothing was sent to Bitrix24."
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|---------|
| 400 | `MISSING_PARAMS` | The `externalId` field is missing; a request without a body lands here too |
| 400 | `INVALID_PARAMS` | `externalId` is not a string or is empty, or another body field or a query parameter was sent |
| 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`. `CHAT_NOT_FOUND` — the `externalId` form was not recognized or the chat could not be obtained |
| 403 | `SCOPE_DENIED` | The API key does not have the `im` scope |
| 403 | `WRITE_BLOCKED_READONLY_KEY` | The key is read-only: the call may create a chat — 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

**A CRM chat is created even for a missing entity.** On a test Bitrix24 account a request with `crm|DEAL|<ID>` for a deal that does not exist created a chat and made the caller a member. Such a chat cannot be removed by [deleting the chat](/docs/chats/management/delete) — the response is `403 BITRIX_ACCESS_DENIED`. Check that the entity exists before the call.

**A personal dialog comes back as `chatN`.** For a user ID, the response is the personal dialog's `chatN`, not the other user's ID, which other endpoints use to address a personal dialog.

**Find a CRM entity chat without creating one.** If you need the chat only when it already exists, use [finding a CRM entity chat](/docs/chats/discovery/find): for an entity without a chat it returns `data: null` and creates nothing.

## See also

- [Chat discovery](/docs/chats/discovery)
- [Find a CRM entity chat](/docs/chats/discovery/find)
- [Dialog details](/docs/chats/discovery/get)
