# Authentication

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

Stack: v3.2

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. A missing or expired token returns `401` with
no body detail — that is deliberate. A valid token that lacks the scope for the operation
returns `403` and names the scope it wanted.

| Header            | Required    | Notes                                                                                                                                                                                                                                                                                                                                                            |
| ----------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Authorization`   | yes         | `Bearer <access_token>`. Tokens last one hour by default.                                                                                                                                                                                                                                                                                                        |
| `Content-Type`    | with a body | `application/json` — including on the GET calls that take a filter body.                                                                                                                                                                                                                                                                                         |
| `Idempotency-Key` | some writes | Client-generated. Where honored, replays the original response on retry. Coverage is per-endpoint, not per-module — Ledger v2 data-plane writes and Wallets writes accept it, Ledger admin operations and all of Ledger v1 do not, and Webhooks delivery replay requires it. See [Conventions](https://docs.formance.com/reference/api/conventions#idempotency). |

```bash
curl "$FORMANCE_API_URL/api/ledger/v2" \
  -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.

| 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).  |
