# Public client: the PKCE flow

The main flow — [`GET /v1/connect/authorize`](/docs/partner-connect/authorize) and [`POST /v1/connect/token`](/docs/partner-connect/token) — is described for a confidential client: one that has a server and a `client_secret`. If there is no server and nowhere to hide a secret (a mobile application, a desktop program, a single-page web application), register a public client. It is not issued a secret: the PKCE mechanism proves its authenticity instead.

The PKCE flow differs from the main flow in three steps. Everything else is the same.

## Step 1. Create a pair of strings

Each time the flow runs, the application creates a random string, `code_verifier`, and its SHA-256 hash, `code_challenge`, encoded as base64url without `=` padding.

```javascript
import crypto from 'node:crypto'

const b64url = (buf) => buf.toString('base64url')
const verifier  = b64url(crypto.randomBytes(32))
const challenge = b64url(crypto.createHash('sha256').update(verifier).digest())
```

Keep `verifier` until the flow ends. `challenge` goes into the first request.

## Step 2. Pass the hash to `/v1/connect/authorize`

| Parameter | Value |
|----------|----------|
| `code_challenge` | The hash from step 1 |
| `code_challenge_method` | Always `S256`. Other values are rejected |

For a public client both parameters are **required**: without them the flow ends in a redirect with `error=invalid_request`.

## Step 3. Pass `code_verifier` to `/v1/connect/token`

The exchange takes `code_verifier` instead of `client_secret`:

```bash
curl -X POST https://vibecode.bitrix24.com/v1/connect/token \
  -H "Content-Type: application/json" \
  -d '{
    "grant_type": "authorization_code",
    "client_id": "YOUR_CLIENT_ID",
    "code": "RECEIVED_CODE",
    "redirect_uri": "http://127.0.0.1:9999/callback",
    "code_verifier": "CODE_VERIFIER"
  }'
```

The response is the same as for a confidential client — the fields are described on the [`POST /v1/connect/token`](/docs/partner-connect/token#response-fields) page.

**A public client must not send `client_secret`** — the platform responds with `400 invalid_client`. A secret shipped inside a distributed application is not a secret, and the flow is designed so that such a secret cannot be used.

## Returning to localhost

A program without a website of its own has no public address to receive the code at. For such cases a redirect back to itself is allowed: register a `redirect_uri` such as `http://127.0.0.1:9999/callback`, start a listener on that port for the duration of the flow, and close it as soon as the code arrives.

The addresses `127.0.0.1`, `localhost` and `[::1]` are accepted, with either the `http` or `https` scheme. Every other address must be `https`. The scheme and the path are always matched, while the port and the localhost address itself are matched depending on the client type:

| Client type | How a localhost address is matched |
|---|---|
| Public | The port is **not taken into account**: if it is in use, the application may listen on any other one without registering the address again. The addresses `127.0.0.1`, `localhost` and `[::1]` are interchangeable: a registered `http://127.0.0.1/callback` also accepts `http://localhost:5123/callback` |
| Confidential | The whole address is matched, port included. This is how a server application is debugged on your own computer: register the address with the port your server listens on, for example `http://localhost:3030/callback` |

The name `localhost` can point somewhere other than this computer if it is overridden in the network settings, so `127.0.0.1` is more reliable.

## See also

- [Start user authorization](/docs/partner-connect/authorize)
- [Exchange the code for an API key](/docs/partner-connect/token)
- [Client types](/docs/partner-connect#client-types)
- [Partner Connect](/docs/partner-connect)
