# Authentication

Source: https://docs.formance.com/v4.0/reference/api/authentication

Stack: v4.0

Every request carries an OAuth2 bearer token. Tokens are scoped to one stack and expire;
the SDKs refresh them for you, curl does not.

## Get a token

For a human on a laptop, `fctl` mints a personal token against your logged-in Cloud
session. For a service, create an OAuth2 client in the stack and use the
client-credentials grant. Personal tokens are for exploration — never ship one.

```bash
curl -X POST "$FORMANCE_API_URL/api/auth/oauth/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=client_credentials" \
  -d "client_id=$FORMANCE_CLIENT_ID" \
  -d "client_secret=$FORMANCE_CLIENT_SECRET"

# => { "access_token": "eyJ…", "expires_in": 3600 }
```

## Send it

Set `Authorization: Bearer` on every call. On the Ledger 3.0 HTTP API, a missing or invalid
token returns `401` with a short `text/plain` reason. A valid token that lacks the scope for
the operation returns `403` with the body `missing required scope`; the HTTP response does
not say which scope. The gRPC API returns `PERMISSION_DENIED` and names the required scope.

| Header            | Required    | Notes                                                                                                                                                                                                                                                                                                                                                                                       |
| ----------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Authorization`   | yes         | `Bearer <access_token>`. Tokens last one hour by default.                                                                                                                                                                                                                                                                                                                                   |
| `Content-Type`    | with a body | `application/json`. Ledger 3.0 list calls take their filter in the `filter` query parameter, not in a body.                                                                                                                                                                                                                                                                                 |
| `Idempotency-Key` | some writes | Client-generated. Where honored, replays the original response on retry. Coverage is per-endpoint, not per-module. Ledger 3.0 accepts it on transaction and metadata writes, ledger creation and deletion, and the bulk route; Wallets writes accept it, and Webhooks delivery replay requires it. See [Conventions](https://docs.formance.com/v4.0/reference/api/conventions#idempotency). |

```bash
curl "$FORMANCE_API_URL/api/ledger/v3/" \
  -H "Authorization: Bearer $FORMANCE_TOKEN"
```

## Scopes

Scopes are granted per module and per verb. A client that only reads the ledger needs
`ledger:read`; posting transactions needs `ledger:write`. Membership roles are separate
and are managed from the Cloud console, not the stack.

In Ledger 3.0, `ledger:read` and `ledger:write` expand to granular scopes such as
`ledger:TransactionRead` and `ledger:TransactionWrite`. Neither grants cluster operations
such as backups: `ledger:admin` is the only aggregate scope that grants
`ledger:ClusterRead` and `ledger:ClusterWrite`. See
[Understand scopes](https://docs.formance.com/v4.0/modules/ledger/operations/authentication#understand-scopes) for the
full list.

| Scope                | Grants                                                                   |
| -------------------- | ------------------------------------------------------------------------ |
| ledger:read          | List and retrieve ledgers, transactions, accounts, balances and volumes. |
| ledger:write         | Create and revert transactions, write metadata, run bulk actions.        |
| payments:read        | Read connectors, payments, bank accounts and payment service users.      |
| payments:write       | Install connectors and initiate payments.                                |
| wallets:read         | List and retrieve wallets, balances and holds.                           |
| wallets:write        | Create wallets, debit, credit and manage holds.                          |
| orchestration:read   | Read workflow definitions and execution instances.                       |
| orchestration:write  | Create and trigger workflows.                                            |
| webhooks:read        | List webhook configurations and delivery attempts.                       |
| webhooks:write       | Create, update and delete webhook configurations.                        |
| reconciliation:read  | Read reconciliation policies and reports.                                |
| reconciliation:write | Create reconciliation policies and run reconciliations.                  |
| auth:read            | Read OAuth2 clients and their configuration.                             |
| auth:write           | Create and manage OAuth2 clients and secrets.                            |
| search:read          | Query the full-text search index (Enterprise Edition).                   |
| search:write         | Index documents into the full-text search service (Enterprise Edition).  |
