
## Request a device sign-in code

`POST /v1/connect/device/authorize`

Issues a confirmation code for signing in from a device without a browser: a TV, a set-top box, a terminal utility — anywhere typing a login and password is awkward. The user confirms the sign-in on a phone or computer, and the device receives access. The flow is defined by RFC 8628.

The application needs device sign-in enabled: a platform administrator turns it on, and only for verified applications. A self-registered client gets `400 unauthorized_client` — this protects users from confirmation codes displayed by unverified applications.

## Request fields (body)

The request accepts `application/json` and `application/x-www-form-urlencoded`.

| Field | Type | Required | Description |
|------|-----|:-----:|----------|
| `client_id` | string | yes | Identifier of the application for which a platform administrator has enabled device sign-in |
| `scope` | string | no | Requested permissions, separated by spaces or commas. If omitted, the permissions registered for the client apply |
| `code_challenge` | string | conditional | SHA-256 hash of `code_verifier` in `base64url` without `=`. Required if PKCE is enabled in the client settings. The platform stores the submitted hash and checks it against `code_verifier` during polling |
| `code_challenge_method` | string | conditional | `S256`, required together with `code_challenge` |
| `device_id` | string | no | Persistent identifier of the application installation: up to 128 characters, Latin letters, digits and the characters `.`, `:`, `-`, `_`. The platform adds it to `verification_uri_complete` as the `device_id` parameter to link the steps of one sign-in, even if the link is opened in another browser. A value of any other form is dropped without an error: the response stays `200`, and the link is returned without the parameter. Does not affect issuing a key to a third-party application |
| `login_source` | string | no | Your sign-in button label for statistics: a lowercase Latin letter followed by lowercase Latin letters, digits and `_`, up to 64 characters in total. Leading and trailing spaces are trimmed. An invalid label does not interrupt the sign-in and does not affect permissions or key issuance |
| `last_connect_clear` | string | no | Optional window-clear receipt: a JSON string of up to 1024 UTF-8 bytes. Accepted and recorded only for keys of the official Cowork client (`client_id=cowork-desktop`); ignored for other clients. Invalid known values or an oversized receipt cause the entire receipt to be discarded without a connection error. Unknown fields are ignored and never recorded. Omitting the parameter preserves the existing connection flow |

## Examples

The endpoint authenticates with the application identifier `client_id` in the request body. The `X-Api-Key` and `Authorization` headers are not needed.

Keep `code_verifier` until the exchange at `POST /v1/connect/token`. The curl example uses the RFC 7636 pair with `code_verifier` equal to `dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk`. Generate a new random pair for each sign-in, as in the JavaScript example.

### curl — OAuth application

```bash
curl -X POST https://vibecode.bitrix24.com/v1/connect/device/authorize \
  -H "Content-Type: application/json" \
  -d '{"client_id":"YOUR_CLIENT_ID","scope":"crm","code_challenge":"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM","code_challenge_method":"S256"}'
```

### JavaScript — OAuth application

```javascript
const base64url = bytes => btoa(String.fromCharCode(...bytes))
  .replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
const code_verifier = base64url(crypto.getRandomValues(new Uint8Array(32)))
const challenge = base64url(new Uint8Array(
  await crypto.subtle.digest('SHA-256', new TextEncoder().encode(code_verifier)),
))

const res = await fetch('https://vibecode.bitrix24.com/v1/connect/device/authorize', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    client_id: 'YOUR_CLIENT_ID',
    scope: 'crm',
    code_challenge: challenge,
    code_challenge_method: 'S256',
  }),
})

const data = await res.json()
// data.user_code and data.verification_uri — show to the user
// data.device_code — keep on the device, do not display
```

## Response fields

| Field | Type | Description |
|------|-----|----------|
| `device_code` | string | Device secret. Not displayed; sent when polling `POST /v1/connect/token` |
| `user_code` | string | Code shown to the user together with the URL |
| `verification_uri` | string | URL where the user enters the code |
| `verification_uri_complete` | string | The same URL with `user_code` already filled in |
| `expires_in` | integer | Number of seconds the user has to confirm |
| `interval` | integer | Polling interval for `POST /v1/connect/token`, in seconds |

## Response example

```json
{
  "device_code": "MYQnuWB3H0tR...",
  "user_code": "54BV-XT8F",
  "verification_uri": "https://vibecode.bitrix24.com/connect/device",
  "verification_uri_complete": "https://vibecode.bitrix24.com/connect/device?user_code=54BV-XT8F",
  "expires_in": 899,
  "interval": 5
}
```

## Error response example

400 — device sign-in is not enabled for this client:

```json
{
  "error": "unauthorized_client",
  "error_description": "Client is not enabled for the device flow"
}
```

## Errors

Rejections come in the RFC 6749 form — `{ error, error_description }`:

| HTTP | `error` | Condition |
|------|---------|---------|
| 400 | `invalid_request` | `client_id` is missing. `error_description` — `Missing client_id` |
| 401 | `invalid_client` | `client_id` is unknown or the client is disabled |
| 400 | `unauthorized_client` | Device sign-in is not enabled for this client. `error_description` — `Client is not enabled for the device flow` |
| 400 | `unauthorized_client` | The client is in `COMPANY` mode. `code` and `error_description` — `CONNECT_COMPANY_DEVICE_FLOW_FORBIDDEN` |
| 400 | `invalid_request` | The client requires PKCE, and `code_challenge` is missing, the method is not `S256`, or the hash is invalid |
| 400 | `invalid_scope` | An unknown permission or a permission outside the client's registered set is requested |

The endpoint is rate-limited — 30 requests per minute per caller IP address; `client_id` is not part of the key. On top of that, the platform entry limiter applies, shared across all of `/v1/`. Exceeding either returns `429` in the common API envelope with a `Retry-After` header. The presence of `X-RateLimit-Limit` shows which one fired.

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

## Known specifics

**The user opens the URL and confirms.** On the confirmation screen they see the application card, the requested permissions, a Bitrix24 account selector and a phishing warning: confirm only a sign-in you started yourself.

**The device polls `/v1/connect/token`.**

```bash
curl -X POST https://vibecode.bitrix24.com/v1/connect/token \
  -H "Content-Type: application/json" \
  -d '{
    "grant_type": "urn:ietf:params:oauth:grant-type:device_code",
    "client_id": "YOUR_CLIENT_ID",
    "device_code": "DEVICE_CODE",
    "code_verifier": "CODE_VERIFIER"
  }'
```

Until the user confirms, the request returns `400` with one of these states:

| `error` | What to do |
|---------|------------|
| `authorization_pending` | Wait and keep polling every `interval` seconds |
| `slow_down` | Polling is more frequent than allowed. Increase the interval by 5 seconds and continue |
| `access_denied` | The user declined. Stop polling |
| `access_denied` with `error_description=admin_approval_requested` | The user asked the Bitrix24 account administrator to allow AI for the application. Returned when administrator approval of AI is enabled on the platform. Stop polling, and after the administrator decides, request a new code via `POST /v1/connect/device/authorize` |
| `expired_token` | The code has expired. Request a new one via `POST /v1/connect/device/authorize` |
| `invalid_grant` | The user confirmed the sign-in, but `code_verifier` is missing or does not match `code_challenge` from the code request. Repeat the poll with the correct `code_verifier` |

Polling can also receive `429` — a rate limiter rejection, not an authorization state, and it **comes in two forms** because this route has two limiters. A rejection from the platform entry limiter comes in the RFC 6749 form (`error` equals `slow_down`) and without `X-RateLimit-*` headers. A rejection from the endpoint's own limiter comes in the common API envelope — `{ "success": false, "error": { "code": "RATE_LIMITED", "message": "..." } }` — and carries `X-RateLimit-*` headers. A single branch on `error === "slow_down"` is therefore not enough: it misses half of the rejections.

Handle both the same way: wait at least the `Retry-After` value and continue polling without resetting your growing pause. To tell them apart if needed, check for `X-RateLimit-Limit`.

After confirmation the same request returns `200` with the key and two permission sets:

```json
{ "api_key": "vibe_api_...", "scopes": ["crm"], "granted_scopes": ["crm"] }
```

Note: this object is **narrower** than in the main flow — without the `portal` and `user` blocks.

The key is returned **once**. A repeated poll with the same `device_code` returns `expired_token`, so save the key immediately.

**What the device must do:**

- **Show a countdown.** The code is valid for 15 minutes. When it expires, show a new one, not a dead screen.
- **Respect `interval` and react to `slow_down`.** Polling more often than allowed does not speed up issuance; it leads to rejections.
- **Read `Retry-After` and increase its own pause.** The header value is the minimum pause, not a guarantee that the next request is accepted: all running copies of the application compete for the per-IP limit. Wait at least `Retry-After` and increase your pause with each rejection.
- **Do not display `device_code`.** Only `user_code` goes on screen.

## See also

- [Exchange a code for an API key](/docs/partner-connect/token)
- [Public client: PKCE flow](/docs/partner-connect/pkce)
- [Partner Connect](/docs/partner-connect)
