# OAuth 2.0 overview

OAuth 2.0 lets your application call the Bito API **on behalf of a merchant account**. The tokens
Bito issues carry the tenant context, so every call is automatically scoped to that merchant's
data — you never pass an account id yourself.

::: tip
If you only ever serve **one** account — your own — you do not need OAuth. Send the `api-key`
header instead; see [Calling the API](https://docs.bito.uz/en/guide/api.md).
:::

## The two ways a code reaches you

There are two ways your application can obtain an authorization code. Both end at the same token
endpoint, and both give you the same `access_token` + `refresh_token` pair.

| You are building | Use | Who starts it |
|---|---|---|
| A marketplace integration merchants install from inside Bito | [Install flow (Flow A)](https://docs.bito.uz/en/guide/oauth/install-flow.md) | The merchant, in the Bito admin panel |
| Your own product, where the merchant clicks "Connect Bito" in *your* UI | [Connect flow (Flow B)](https://docs.bito.uz/en/guide/oauth/connect-flow.md) | Your application |
| A background job with no user present, for a merchant that already installed you | [Client credentials grant](https://docs.bito.uz/en/guide/oauth/tokens.md#client-credentials) | Your server |

```text
                    Flow A                         Flow B
                (Bito-initiated)             (you-initiated)

  Merchant clicks Install in Bito     Merchant clicks Connect in your app
                |                                   |
                v                                   v
  Bito POSTs the code to your          You redirect to id.bito.uz, the
  redirect_uri                         merchant logs in, Bito redirects
                |                      back with ?code=
                |                                   |
                +-----------------+-----------------+
                                  |
                                  v
                     POST /auth/oauth/token
                     (grant_type=authorization_code)
                                  |
                                  v
                   access_token + refresh_token
```

## Scopes

Scopes control how much access the merchant grants you.

| Scope | Value | Description |
|---|---|---|
| Admin | `admin` | Full administrative access to all resources |
| Read | `read` | Read-only access |
| Write | `write` | Create and update resources |
| Delete | `delete` | Delete resources |

## Permissions: the real control

Every Integration API request — with an OAuth token or with an `api-key` — is checked like this:

1. **`required_permissions` → `granted_permissions`.** Your app declares the ERP permissions it uses
   on the Integration API (`required_permissions`, the same keys roles are built from). The merchant
   sees exactly that list before installing, and installing writes a copy of it onto the integration
   as `granted_permissions`. Every call is checked against that copy; a call outside it is refused.
   If you change `required_permissions` later, an account that already installed you keeps the old
   copy until the merchant approves the new list — by reinstalling or, for a widget, by accepting its
   consent screen again. An empty list means all permissions. Once the list is not empty, resources
   with no permission key are closed too (except `organization/get-all` and `get-by-id`).
2. **`available_scopes`.** A legacy check for clients that have `available_scopes` set: the scope
   matching the call's action (`read`, `write` or `delete`; `admin` covers all) must be on that
   list, or the call is refused.

Either refusal is `403` with `code: 10003`. Declare the narrowest `required_permissions` list that
works — it is what the merchant reads to know what they are agreeing to before installing. See
[Create an integration](https://docs.bito.uz/en/guide/create.md).

## Endpoints

| Method | Path | Auth | Purpose |
|---|---|---|---|
| `POST` | `/auth/oauth/auth` | None | Client credentials grant |
| `POST` | `/auth/oauth/token` | None | Exchange an authorization code, or refresh a token pair |
| `POST` | `/auth/get-one-time-token` | Bearer (user session) | Mint a [one-time token](https://docs.bito.uz/en/guide/oauth/one-time-tokens.md) |

Base URL: `https://api.bito.uz/auth-api` in production, `https://api.systematicdev.uz/auth-api` on dev — for example, `POST https://api.bito.uz/auth-api/auth/oauth/token`.
