# POST /api/v1/oauth2/exchange

> Exchange Code

- **Tag:** oauth
- **Operation ID:** `exchange_code`

## Description

Exchange a single-use code or refresh token for access + refresh tokens.

Called from the SPA — no client credentials required.
The code was already authenticated when created via POST /oauth2/token.

**Code exchange:** Provide ``code`` from the /token response.
**Token refresh:** Provide ``refresh_token`` from a previous exchange.

## Authentication

Bearer token in `Authorization` header.
Required header: `x-business-id: <business uuid>`.

## Responses

### 200 — Successful Response

Schema: `OAuthTokenResponse`

- `access_token` (string · required) — The access token (JWT)
- `token_type` (string) — Token type (always 'Bearer')
- `expires_in` (integer · required) — Access token lifetime in seconds
- `refresh_token` (string) — Refresh token for obtaining new access tokens
- `scope` (string) — Granted scope (may differ from requested)
- `business_id` (string) — MCP grants bound to one business: that business's id. Absent when the grant reaches every business the user can.
- `business_name` (string) — MCP grants bound to one business: that business's name

### 422 — Validation Error

Schema: `ErrorResponse`

- `error` (string · required) — Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`.
- `message` (string · required) — Human-readable explanation, safe to show to end users.
- `code` (string · required) — Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`).
- `request_id` (string) — Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied.
- `errors` (object) — Field-level validation messages keyed by field name. Present on 400/422 validation failures only.

## Code samples

### cURL

```bash
curl -X POST 'https://api.ondayzero.com/api/v1/oauth2/exchange' \
  -H 'Authorization: Bearer dz_your_token' \
  -H 'x-business-id: YOUR_BUSINESS_ID' \
  -H 'Content-Type: application/json' \
  -d ''
```

### JavaScript

```javascript
const response = await fetch('https://api.ondayzero.com/api/v1/oauth2/exchange', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer dz_your_token',
    'x-business-id': 'YOUR_BUSINESS_ID',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({}),
});
const data = await response.json();
```

### Python

```python
import httpx

headers = {
    "Authorization": "Bearer dz_your_token",
    "x-business-id": "YOUR_BUSINESS_ID",
}

payload = {}

response = httpx.post("https://api.ondayzero.com/api/v1/oauth2/exchange", headers=headers, json=payload)
data = response.json()
```

## See also

- HTML version: https://www.ondayzero.com/docs/reference/oauth/exchange-code
- OpenAPI slice: https://www.ondayzero.com/docs/reference/oauth/exchange-code/openapi.json
- Other endpoints in **oauth**: https://www.ondayzero.com/docs/reference/oauth (markdown bundle: https://www.ondayzero.com/docs/reference/oauth.md, OpenAPI: https://www.ondayzero.com/docs/reference/oauth/openapi.json)
- Endpoint catalog: https://www.ondayzero.com/docs/reference/index.md
