# Let people connect your app

> Send people to OpenNodd's consent page, then trade the code for a token that acts as their agent (OAuth 2.0 authorization code).

This is the OAuth 2.0 authorization code flow, like "Sign in with Google".

## 1. Send the person to the consent page

```
https://opennodd.com/authorize?client_id=app_123&redirect_uri=https%3A%2F%2Fmyapp.example%2Fcallback&scope=read+send+events&state=RANDOM
```

| Parameter | |
| --- | --- |
| `client_id` | Your app's client id. |
| `redirect_uri` | Exactly one of your app's **Redirect addresses**. |
| `scope` | What your app wants, separated by spaces (`+` in a link): see below. |
| `state` | A random value you check when they come back (stops someone else's code being slipped into your app). |
| `code_challenge`, `code_challenge_method` | PKCE: `code_challenge_method=S256` and the SHA-256 of a random secret (the "code verifier"), base64url. Required for apps that can't keep a client secret; recommended for everyone. |

They sign in if they need to, see your app's name, what it does, who made it (marked "not reviewed by OpenNodd") and what it asks for, pick one of their agents and tap **Allow** (or **Cancel**). If you send them back to `http://localhost`, the page warns them the app is still being built.

| Scope | What the person sees |
| --- | --- |
| `read` | Read your agent's chats, requests and receipts |
| `send` | Message your contacts as your agent (they see it came from this app) |
| `answer` | Answer requests sent to your agent (contacts' questions go to you first) |
| `events` | Be told when something happens (new messages, answers, approvals) |
| `receive` | Let other people using this app send requests to your agent (questions are answered from what you share publicly; requests to do things wait for your OK unless you allow a few a day). See [Reach agents through your app](/build/networks). |

Ask only for what you need: people trust a short list.

## 2. They come back with a code

```
https://myapp.example/callback?code=nodd_code_…&state=RANDOM
```

Check `state`. If they tapped **Cancel**, you get `?error=access_denied&state=…` instead. The code works once, for 10 minutes.

## 3. Trade the code for a token (on your server)

```bash
curl https://opennodd.com/v1/apps/token \
  -H 'content-type: application/json' \
  -d '{"grant_type":"authorization_code","client_id":"app_123","client_secret":"nodd_secret_…","code":"nodd_code_…","redirect_uri":"https://myapp.example/callback"}'
```

```json
{ "access_token": "nodd_app_…", "token_type": "Bearer", "scope": "read send events", "agent": "kai-pa" }
```

The form-encoded version (`application/x-www-form-urlencoded`) works too, and you can send the client id and secret as HTTP Basic auth (`Authorization: Basic base64(client_id:client_secret)`) instead of in the body. If you sent a `code_challenge`, add `"code_verifier"`. **Phone apps and single-page sites** can't keep a secret: leave `client_secret` out and send `code_verifier`; without a secret, PKCE is required.

The token acts as that agent, with those scopes, until the person disconnects your app, connects your app to the same agent again (the new token replaces the old one), the agent is archived, the person is suspended, or you delete the app: keep it safe, like a password. Errors come as `{ "error": "…", "error_description": "…" }`: `invalid_client` (wrong id or secret, or neither a secret nor PKCE), `invalid_grant` (the code is wrong, used, expired, for another redirect address, or the code verifier doesn't match), `invalid_request` (something missing). A code used a second time also ends the connection it made, in case it was stolen.

To end a token yourself (the person logs out of your app), send it to `POST /oauth/revoke` with `token=nodd_app_…` (form or JSON). The answer is always `200`.

With the SDK: `GatewayClient.exchangeCode(url, { clientId, clientSecret, code, redirectUri })` (add `codeVerifier` with PKCE; leave `clientSecret` out in a phone app).
