> Documentation index: https://docs.zernio.com/llms.txt. Every page is also Markdown: add .md to its URL.

# OAuth quickstart

Register an OAuth client, send a user through the Zernio consent screen, exchange the code with PKCE, call the API, refresh the token and revoke it.

Every request on this page runs as written. Replace the redirect URI with your own callback.

## Before you start

- A Zernio account to approve the consent screen with.
- An https callback URL in your app. Native and CLI apps can use `http://127.0.0.1` on any port.
- `curl`, `openssl` and `jq`.

## Step 1: Register a client

Register once and store the `client_id` and `client_secret`. The secret is shown only in this response.

```bash
curl -sS -X POST https://zernio.com/api/oauth/register \
  -H 'Content-Type: application/json' \
  -d '{
    "client_name": "Acme Scheduler",
    "redirect_uris": ["https://app.acme.com/oauth/zernio/callback"],
    "token_endpoint_auth_method": "client_secret_post",
    "grant_types": ["authorization_code", "refresh_token"],
    "scope": "posts:read posts:write accounts:read email"
  }'
```

```json
{
  "client_id": "late_cid_928aeb6e...",
  "client_secret": "62d699...",
  "token_endpoint_auth_method": "client_secret_post",
  "scope": "posts:read posts:write accounts:read email"
}
```

A browser, mobile or CLI app cannot keep a secret: register it with `"token_endpoint_auth_method": "none"` and leave `client_secret` out of every later request.

```bash
CLIENT_ID=late_cid_...
CLIENT_SECRET=...
REDIRECT_URI=https://app.acme.com/oauth/zernio/callback
```

## Step 2: Create a PKCE pair

Make a new random verifier for every authorization, and keep it on your server until the callback.

```bash
CODE_VERIFIER=$(openssl rand -base64 48 | tr -d '=+/\n' | cut -c1-64)
CODE_CHALLENGE=$(printf '%s' "$CODE_VERIFIER" | openssl dgst -sha256 -binary | openssl base64 -A | tr '+/' '-_' | tr -d '=')
STATE=$(openssl rand -hex 16)
```

## Step 3: Send the user to consent

Redirect the user's browser to this URL. `redirect_uri` and `scope` must be URL-encoded.

```bash
echo "https://zernio.com/oauth/authorize?response_type=code&client_id=$CLIENT_ID&redirect_uri=$(printf %s "$REDIRECT_URI" | jq -sRr @uri)&scope=posts%3Aread%20posts%3Awrite%20accounts%3Aread%20email&state=$STATE&code_challenge=$CODE_CHALLENGE&code_challenge_method=S256"
```

The user signs in, picks a workspace, adjusts the access and clicks **Authorize**. See [Consent screen](/oauth/consent-screen).

## Step 4: Handle the callback

Zernio redirects to your `redirect_uri` with a one-time code:

```
https://app.acme.com/oauth/zernio/callback?code=late_ac_3b1f...&state=9f2c...&iss=https%3A%2F%2Fzernio.com
```

Check that `state` is the one you sent and that `iss` is `https://zernio.com`. If the user clicks **Deny**, the callback carries `error=access_denied` instead.

```bash
CODE=late_ac_...   # the code query parameter
```

## Step 5: Exchange the code for tokens

Do this from your server within 10 minutes. The code works once.

```bash
curl -sS -X POST https://zernio.com/api/oauth/token \
  -d grant_type=authorization_code \
  -d "code=$CODE" \
  -d "redirect_uri=$REDIRECT_URI" \
  -d "client_id=$CLIENT_ID" \
  -d "client_secret=$CLIENT_SECRET" \
  -d "code_verifier=$CODE_VERIFIER"
```

```json
{
  "access_token": "late_at_7d14...",
  "token_type": "bearer",
  "expires_in": 3600,
  "refresh_token": "late_rt_565a...",
  "scope": "posts:read posts:write accounts:read email"
}
```

`scope` is what the user granted, which can be less than you asked for. Store both tokens encrypted.

```bash
ACCESS_TOKEN=late_at_...
REFRESH_TOKEN=late_rt_...
```

## Step 6: Call the API

Send the access token as a Bearer token, exactly like an API key.

```bash
curl -sS https://zernio.com/api/v1/accounts \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

```json
{
  "accounts": [
    {
      "_id": "69d7c7820a24f7221798692e",
      "platform": "instagram",
      "username": "acme",
      "profileId": { "_id": "69d7c41ff9eb7875b7cb39c8", "name": "Acme" }
    }
  ]
}
```

With the `email` scope, `/api/oauth/userinfo` tells you who connected:

```bash
curl -sS https://zernio.com/api/oauth/userinfo \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

```json
{ "sub": "69d7c41af9eb7875b7cb39c7", "name": "Ada Lovelace", "email": "ada@acme.com", "email_verified": true }
```

Use `sub` as the stable id of the Zernio user. A user can connect several workspaces, so read the workspace from `GET /v1/auth/verify`:

```bash
curl -sS https://zernio.com/api/v1/auth/verify \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

```json
{ "valid": true, "userId": "69d7c41af9eb7875b7cb39c7", "authType": "oauth", "scope": "posts:read posts:write accounts:read email", "workspaceId": "6b004775db5770c0e8fc230c" }
```

Key each stored connection on `userId` and `workspaceId`.

## Step 7: Refresh the access token

Access tokens expire after 1 hour. Refresh before or after expiry:

```bash
curl -sS -X POST https://zernio.com/api/oauth/token \
  -d grant_type=refresh_token \
  -d "refresh_token=$REFRESH_TOKEN" \
  -d "client_id=$CLIENT_ID" \
  -d "client_secret=$CLIENT_SECRET"
```

The response has the same shape as step 5, with a new refresh token. Save it and drop the old one. Add `-d scope=posts:read` to get tokens with less access.

## Step 8: Revoke on disconnect

When the user disconnects Zernio in your app, revoke the refresh token:

```bash
curl -sS -X POST https://zernio.com/api/oauth/revoke \
  -d "token=$REFRESH_TOKEN" \
  -d "client_id=$CLIENT_ID" \
  -d "client_secret=$CLIENT_SECRET"
```

It answers `200` with an empty body. Every access and refresh token of that authorization stops working at once.

## How it behaves

- HTTP Basic works too: `curl -u "$CLIENT_ID:$CLIENT_SECRET"` instead of the two body fields.
- A refresh token you already used keeps working for 60 seconds, then until the new one is used. Concurrent refreshes and lost responses do not log the user out.
- Each refresh token expires 30 days after it was issued. If your app does not refresh for 30 days, the user has to authorize again.
- Users can revoke your app at any time in **Settings > Connected apps**. Your next call answers `401`.

## If it fails

| Error | Cause | Fix |
|---|---|---|
| `invalid_request`: code_challenge is required for public clients | No PKCE challenge on the authorize URL | Send `code_challenge` and `code_challenge_method=S256` |
| `invalid_grant`: PKCE code_verifier verification failed | Wrong verifier for this code | Send the verifier you made in step 2 |
| `invalid_grant`: redirect_uri mismatch | `redirect_uri` differs from step 3 | Send the same string |
| `invalid_grant`: Authorization code already used | The code was exchanged before | Start again from step 3 |
| `invalid_client` (400 or 401) | Unknown client, or wrong or missing client secret | Check the client ID and secret, or register again |
| `insufficient_permissions` (403) | The grant does not cover the endpoint | Ask for the scope in `WWW-Authenticate` |

More in [Errors](/oauth/errors).

## Related

- [Scopes](/oauth/scopes)
- [Security](/oauth/security)
- [List accounts](/accounts/list-accounts)
- [Token endpoint reference](/oauth-server/exchange-oauth-token)