> ## Documentation Index
> Fetch the complete documentation index at: https://developer.lofty.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OAuth 2.0

> Set up OAuth 2.0 authentication for your Lofty API integration.

OAuth 2.0 is the recommended authentication method for multi-tenant integrations. Lofty supports three grant types:

| Grant Type | Use Case |
| - | - |
| **Authorization Code** | Web apps where a user authorizes access via browser |
| **Authorization Code + PKCE** | SPAs and native apps (no client secret required) |
| **Client Credentials** | Server-to-server, using an API Key the account holder gives you |

> [!NOTE]
>
> The first two are served by `https://crm.lofty.com/api/user-web/oauth/token`; Client Credentials is served by `https://api.lofty.com/oauth/token`. They differ in more than the host — see [Choosing between them](#choosing-between-them).

## Prerequisites

You need a **Developer Platform account** and a **registered application**. Sign up at the [Lofty Developer Portal](https://api.lofty.com/vendor/frontend/static/index.html#/login).

## Authorization Code flow

**1. Register your application**

Create a new application in the Developer Portal. Provide your app name, description, and redirect URI(s).

New applications start in **Development Mode** — you can test the full OAuth flow without review.

**2. Obtain credentials**

After registration, you'll receive a **Client ID** and **Client Secret**.

> [!WARNING]
>
> Never expose your Client Secret in client-side code, public repositories, or logs.

**3. Request an access token**

Exchange your credentials for an access token via the OAuth 2.0 authorization code flow.

```bash theme={null}
curl -X POST https://crm.lofty.com/api/user-web/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "Authorization: Basic <BASE64_OF_CLIENT_ID:CLIENT_SECRET>" \
  -d "grant_type=authorization_code" \
  -d "code=<AUTHORIZATION_CODE>" \
  -d "client_id=<CLIENT_ID>" \
  -d "redirect_uri=<REDIRECT_URI>"
```

The `Authorization` header uses HTTP Basic authentication: Base64-encode `<CLIENT_ID>:<CLIENT_SECRET>` to produce the credential string.

For complete token endpoint details, see the [OAuth 2.0 API Reference](https://help.lofty.com/hc/en-us/articles/47801571926811).

**4. Authenticate requests**

Pass the access token in the `Authorization` header:

```bash theme={null}
curl https://api.lofty.com/v1.0/leads \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <ACCESS_TOKEN>"
```

## PKCE flow

Use PKCE ([RFC 7636](https://datatracker.ietf.org/doc/html/rfc7636)) for public clients: SPAs, desktop apps, and command-line tools that cannot keep a client secret. A one-time code verifier proves that the app redeeming the code is the app that started the login.

> [!NOTE]
>
> If your app runs on a server and can keep its client secret, use the [Authorization Code flow](#authorization-code-flow) with the `Authorization: Basic` header instead. You can send PKCE parameters with it as well.

**1. Create a code verifier and challenge**

For every login, generate a random `code_verifier` (43–128 characters from `A-Z a-z 0-9 - . _ ~`) and a random `state`. Keep both in memory. Derive the challenge from the verifier:

```text theme={null}
code_challenge = BASE64URL(SHA256(code_verifier))   // no padding
```

Lofty accepts only the `S256` method.

**2. Send the user to the authorization page**

Open this URL in the user's browser. It is split across lines here for readability; send it as one URL with each value URL-encoded:

```text theme={null}
https://crm.lofty.com/page/vendor-auth.html
  ?client_id=<CLIENT_ID>
  &redirect_uri=<REDIRECT_URI>
  &state=<STATE>
  &code_challenge=<CODE_CHALLENGE>
  &code_challenge_method=S256
```

After the user approves, the browser is redirected to `redirect_uri` with `code` and `state` in the query string. If the user denies access, it is redirected there with `error=access_denied` instead. Check that `state` matches the value you sent before you use the code.

**3. Exchange the code for tokens**

```bash theme={null}
curl -X POST https://crm.lofty.com/api/user-web/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=authorization_code" \
  -d "client_id=<CLIENT_ID>" \
  -d "code=<AUTHORIZATION_CODE>" \
  -d "code_verifier=<CODE_VERIFIER>" \
  -d "redirect_uri=<REDIRECT_URI>"
```

Send the same `redirect_uri` you used in the authorization request. No `client_secret` is sent.

### Redirect URIs for desktop and command-line apps

The authorization code reaches your app only through the registered `redirect_uri`. There is no other endpoint that returns it. A native app receives it on a loopback address ([RFC 8252](https://datatracker.ietf.org/doc/html/rfc8252#section-7.3)):

* Listen on `127.0.0.1` and use `http://127.0.0.1:<PORT>/callback` as the `redirect_uri`.
* Lofty compares redirect URIs exactly, including the port. Register every port your app may use in the Developer Portal. There is no wildcard port.
* Pick a few fixed, uncommon ports and try them in order. An app can register at most five redirect URIs, so keep the list short. If none is free, or the browser runs on another machine (for example over SSH), let the user paste the callback URL from the browser's address bar into your app.

The Lofty CLI works this way: `lofty-cli auth login-browser` listens on the first free port of its registered set and falls back to a pasted callback URL. See [CLI troubleshooting](https://developer.lofty.com/cli/troubleshooting.md#browser-flow-opens-but-never-completes).

## Refresh an access token

Access tokens from the Authorization Code and PKCE flows last 10 days. Exchange the refresh token for a new pair before the access token expires — the user does not need to sign in again.

```bash theme={null}
curl -X POST https://crm.lofty.com/api/user-web/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "Authorization: Basic <BASE64_OF_CLIENT_ID:CLIENT_SECRET>" \
  -d "grant_type=refresh_token" \
  -d "refresh_token=<REFRESH_TOKEN>"
```

PKCE apps leave out the `Authorization` header and send `-d "client_id=<CLIENT_ID>"` instead. Use the same client ID the user authorized.

A successful response:

```json theme={null}
{
  "access_token": "<NEW_ACCESS_TOKEN>",
  "token_type": "bearer",
  "expires_in": 864000,
  "refresh_token": "<NEW_REFRESH_TOKEN>",
  "scope": "openApi"
}
```

> [!WARNING]
>
> **Every refresh replaces both tokens.** The access token and the refresh token you sent stop working immediately. Save the new refresh token before you use the new access token — if you lose it, the user has to authorize your app again.
>
> If several processes or servers share one authorization, let only one of them refresh. A second refresh with the old refresh token fails, and two refreshes that race each other can leave one process holding a pair that was already replaced.

* **The refresh token lasts 180 days, counted from the last refresh.** Each refresh starts a new 180 days. Refresh at least once every 180 days and the user never has to re-authorize.
* **Refresh keeps the scopes the user approved.** It does not add scopes your app gained afterwards — see [Permission scopes](#permission-scopes).
* **When to refresh:** track `expires_in` and refresh before it runs out. If a call is rejected because of its credentials, refresh once and retry the call. If the refresh itself fails, send the user through authorization again.

If the refresh token is unknown, expired, or already replaced, the response carries an `error` field instead of tokens — for example `"error": "invalid_request"` with `"error_description": "refresh token not found"`. Check the body for `error`; do not rely on the HTTP status alone.

### Revoked access

When a user disconnects your app, Lofty deletes the tokens for that user and your app. When your app is deactivated, Lofty deletes all of its tokens. Calls and refreshes with those tokens fail, and the user has to authorize your app again.

## Client Credentials flow

For server-to-server integrations where there is no browser to send a user to. Instead of a user approving your app, the account holder creates an **API Key** in their Lofty CRM (**Settings → Integrations → API Keys**) and gives it to you. You exchange it, together with your app credentials, for a short-lived access token.

> [!WARNING]
>
> This endpoint is **not** the one the other two flows use. Different host, JSON body instead of form fields, and credentials in the body instead of an `Authorization` header. Sending one flow's request shape to the other endpoint fails with a message that does not point at the mismatch.

```bash theme={null}
curl -X POST https://api.lofty.com/oauth/token \
  -H "Content-Type: application/json" \
  -d '{
        "grant_type": "client_credentials",
        "client_id": "<CLIENT_ID>",
        "client_secret": "<CLIENT_SECRET>",
        "customer_key": "<API_KEY>"
      }'
```

| Field | Notes |
| - | - |
| `grant_type` | Fixed value `client_credentials`. |
| `client_id`, `client_secret` | Your app's credentials, from the Developer Portal. In the **body**, not an `Authorization` header. |
| `customer_key` | **The account holder's API Key.** Omitting it returns `Missing required parameter: api_key`. |
| `scope` | Optional, defaults to `openapi`. |

A successful response:

```json theme={null}
{
  "scope": "openapi",
  "access_token": "oat_eyJhbGciOiJIUzI1NiJ9...",
  "token_type": "Bearer",
  "expires_in": 7200,
  "user_id": 123456789012345,
  "refresh_token": null,
  "refresh_expires_in": 0
}
```

The token is prefixed `oat_`, lasts **2 hours**, and carries **no refresh token** — mint a new one when it expires. Send it as `Authorization: Bearer <ACCESS_TOKEN>`, the same as any other access token.

> [!WARNING]
>
> **The token's scope comes from the API Key, not from your app.** An exchanged token can reach exactly the endpoints the account holder selected when creating their key. Permissions your app is registered for but the key does not carry are refused; permissions the key carries are reachable whether or not your app lists them.
>
> So an integration can be configured correctly on the Developer Portal and still get `403` on every call, simply because the key it was given is narrower. When a call is refused, check the key's permissions first.

## Choosing between them

| | Authorization Code (+ PKCE) | Client Credentials |
| - | - | - |
| Endpoint | `https://crm.lofty.com/api/user-web/oauth/token` | `https://api.lofty.com/oauth/token` |
| Body | `application/x-www-form-urlencoded` | `application/json` |
| Client authentication | HTTP Basic header **and** `client_id` in the body (PKCE: `code_verifier`, no secret) | `client_id` + `client_secret` in the body |
| User interaction | Browser consent screen | None — the account holder hands over an API Key |
| Token shape | Opaque string — do not parse it | Prefixed `oat_` |
| Access token lifetime | 10 days (`expires_in: 864000`) | 2 hours (`expires_in: 7200`) |
| Refresh token | Yes — lasts 180 days from the last refresh, replaced on every refresh | No |
| Scope comes from | The permissions your app requested and the user approved | The API Key's permissions |
| `403` error code | `200100` | `200127` |

## Permission scopes

Calling an endpoint a token is not scoped for returns `HTTP 403`. **The body differs by grant type**, because the two are checked against different records:

```json theme={null}
// Authorization Code — checked against the permissions the user approved for your app
{
  "code": 200100,
  "message": "Sorry, the vendor does not have permission for this API."
}
```

```json theme={null}
// Client Credentials — checked against the API Key the token was minted from
{
  "code": 200127,
  "message": "This API Key was not granted permission for this endpoint. Create a new API Key that includes it, or use a key that does."
}
```

Both mean the same thing — the call is outside the token's scope — but they point at different places to fix it. See [Error codes](https://developer.lofty.com/authentication/error-codes.md).

> [!WARNING]
>
> **A token's scopes are fixed when the user authorizes your app.** The token keeps the scopes the user approved through every refresh, and every call is checked against both those scopes and your app's current scopes.
>
> * **Adding scopes** does not break existing connections. Their tokens keep working for the endpoints they already had, but calls to the new endpoints return `403` (`200100`) until the user authorizes your app again. Ask existing users to re-authorize after you add a scope.
> * **Removing scopes** applies to every token. Calls to the removed endpoints start returning `403` (`200100`), which can take up to about 30 minutes to reach every token.

## App lifecycle

| Stage | Description |
| - | - |
| **Development** | Test the full OAuth flow without review. Rate limit: 100/min. |
| **Under Review** | Submitted for production access. |
| **Production** | Approved. Real users can authorize your app. Rate limit: 500/min. |
| **Declined** | Not approved. Edit and resubmit at any time. |

## Updating a production app

Once in production, edits require review before taking effect. Your app continues operating with the current approved configuration during review.

**Non-breaking changes** — app name, description, redirect URIs. Existing connections are unaffected.

**Adding scopes** — existing connections keep working with the scopes they have. Only users who authorize your app again get the new scopes. Notify users before you submit, so they know to re-authorize.

**Removing scopes** — applies to every existing connection once the edit is published. Calls to the removed endpoints return `403`.

> [accordion] Can I edit while a review is pending?
>
> No. Edit controls are locked during review. Cancel the pending edit first if you need to make urgent changes.

> [accordion] What happens if my edit is declined?
>
> Your app continues operating with its current configuration. You'll receive the decline reason via email and can resubmit after making adjustments.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.