# Connect an app with OAuth

> Let an app call the API as whoever connects it, step by step with curl or Postman.

With OAuth, each member connects the app in their own name and picks the account. The app then calls the [API](https://pulso.run/docs/api/overview.md) as them, with their role and the permissions they agreed to. For a script that runs on its own, an [API key](https://pulso.run/docs/api/overview.md#api-keys) is simpler.

Pulso follows OAuth 2.1: authorization code with PKCE (`S256`). Apps are public clients, with no secret. Most OAuth libraries handle all of this once given the addresses below; the steps show what happens underneath.

| | |
|---|---|
| Discovery | `https://pulso.run/.well-known/oauth-authorization-server` |
| Register | `POST https://pulso.run/oauth/register` |
| Authorize | `https://pulso.run/oauth/authorize` |
| Token | `POST https://pulso.run/oauth/token` |
| Revoke | `POST https://pulso.run/oauth/revoke` |

Only admins can connect apps, unless an admin ticks **Members who aren't admins can connect apps and create API keys** in the account's settings.

## 1. Register the app

Tell Pulso the app's name and where to send members back after they connect. That address must be `https`, or `http` on `localhost` or `127.0.0.1`.

```bash
curl -X POST https://pulso.run/oauth/register \
  -H "Content-Type: application/json" \
  -d '{"client_name": "Billing sync", "redirect_uris": ["https://billing.example.com/pulso/callback"]}'
```

Keep the `client_id` from the answer. Register once per app, not per member.

## 2. Send the member to Pulso

Make a PKCE pair for this sign-in: a random `code_verifier` the app keeps, and its SHA-256 as `code_challenge`.

```bash
VERIFIER=$(openssl rand -base64 48 | tr -d '=+/' | cut -c1-64)
CHALLENGE=$(printf %s "$VERIFIER" | openssl dgst -sha256 -binary | openssl base64 | tr '+/' '-_' | tr -d '=')
```

Then open this address in the member's browser:

```
https://pulso.run/oauth/authorize?response_type=code
  &client_id=CLIENT_ID
  &redirect_uri=https://billing.example.com/pulso/callback
  &code_challenge=CHALLENGE
  &code_challenge_method=S256
  &scope=signals:read%20tasks:read
  &state=RANDOM
```

`scope` lists the [permissions](https://pulso.run/docs/api/overview.md#permissions) the app needs, separated by spaces; without it, the app asks for all of them. `state` comes back unchanged, to check that the answer is for this sign-in.

The member signs in if needed, picks the account, sees what the app will be able to do and where it sends them back, and clicks **Connect**. Pulso then sends the browser to the `redirect_uri` with `?code=…&state=…`. The code works once, within 5 minutes.

## 3. Get the tokens

```bash
curl -X POST https://pulso.run/oauth/token \
  -d grant_type=authorization_code \
  -d code=CODE \
  -d client_id=CLIENT_ID \
  -d redirect_uri=https://billing.example.com/pulso/callback \
  -d code_verifier=$VERIFIER
```

The answer holds an `access_token`, which lasts an hour (`expires_in`), and a `refresh_token`.

## 4. Call the API

```bash
curl https://pulso.run/api/v1/signals \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

The token only works in the account the member picked.

## 5. Refresh the token

When the access token expires the API answers `401`. Get a new one with the refresh token:

```bash
curl -X POST https://pulso.run/oauth/token \
  -d grant_type=refresh_token \
  -d refresh_token=REFRESH_TOKEN \
  -d client_id=CLIENT_ID
```

Each refresh returns a new refresh token as well; keep the latest.

## Disconnect

An app can end its own connection:

```bash
curl -X POST https://pulso.run/oauth/revoke -d token=REFRESH_TOKEN -d client_id=CLIENT_ID
```

Members see the apps they connected under **Settings → Connected apps**, with their permissions, where they send sign-ins back and what they did in the account, and can disconnect them there. Admins see and disconnect everyone's. A connection also stops working when its member leaves the account.

## Postman and Insomnia

Register the tool's callback address first, as in step 1 (Postman's is `https://oauth.pstmn.io/v1/callback`). Then, under the request's **Authorization**, choose **OAuth 2.0** and fill in:

- **Grant type**: Authorization Code (With PKCE), with SHA-256
- **Auth URL**: `https://pulso.run/oauth/authorize`
- **Access token URL**: `https://pulso.run/oauth/token`
- **Client ID**: the one you registered, and no client secret, sent in the body
- **Scope**: for example `signals:read tasks:read`

**Get New Access Token** opens Pulso to connect.
