# API conventions

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

Stack: v3.2

Pagination, filtering, idempotency and bi-temporality are the four conventions shared
across the platform APIs — but only pagination applies everywhere. Filtering, idempotency
and the bi-temporal fields are Ledger-centric, with partial support elsewhere. Each
section below opens with the modules it covers; check it before assuming a parameter
carries over. The resource pages link here rather than repeating them.

| Convention     | Applies to                                                                                          |
| -------------- | --------------------------------------------------------------------------------------------------- |
| Pagination     | Every module's list endpoints                                                                       |
| Filtering      | Ledger (JSON DSL; Ledger 3.0 adds a textual form); Wallets, Webhooks, Payments via query parameters |
| Idempotency    | Ledger and Wallets data-plane writes only; Flows partially; Payments and Reconciliation not at all  |
| Bi-temporality | Ledger only                                                                                         |

## Pagination

**Applies to every module.** A few small fixed-size collections (such as
`GET /api/webhooks/configs`) return the full set without a cursor.

List endpoints return a cursor envelope. Read `cursor.data` for the current page, then
pass `cursor.next` back as `cursor` to advance. The cursor encodes the filter and page
size — send it alone, without any other query parameters.

| Parameter        | Type    | Notes                                                                          |
| ---------------- | ------- | ------------------------------------------------------------------------------ |
| `pageSize`       | integer | Results per page. Endpoint-specific — see below. Ignored when `cursor` is set. |
| `cursor`         | string  | Opaque page token. Do not construct or parse it.                               |
| `cursor.hasMore` | boolean | Whether another page exists. Prefer this over comparing lengths.               |

The default page size is **not** platform-wide. Ledger list endpoints default to 15; the
Webhooks delivery and attempt lists default to 100. The advertised ceiling is 1000, but the
Ledger also has an operator-configurable runtime clamp (`--max-page-size`, itself defaulting
to 100), so the effective maximum depends on the deployment. Do not derive a page count from
an assumed default: send an explicit `pageSize` when the size matters, read back the value
the response reports, and loop on `cursor.hasMore` rather than on arithmetic.

```bash
# page 1
curl "$FORMANCE_API_URL/api/ledger/v2/ledger001/transactions?pageSize=100" \
  -H "Authorization: Bearer $FORMANCE_TOKEN"

# page 2 — cursor only, no other params
curl "$FORMANCE_API_URL/api/ledger/v2/ledger001/transactions?cursor=aHR0…" \
  -H "Authorization: Bearer $FORMANCE_TOKEN"
```

## Filtering

**The JSON DSL below is Ledger-only.** Wallets, Webhooks and Payments filter via query
parameters instead — the clauses in this section do not apply to them; check the
individual resource pages for which fields those modules expose.

The Ledger module accepts filters as a JSON DSL sent as the request body — including on
GET. Full syntax for the
Ledger DSL is in [Filtering queries](https://docs.formance.com/v3.2/modules/ledger/working-with/filtering-queries).

| Clause                       | Type            | Notes                                                                         |
| ---------------------------- | --------------- | ----------------------------------------------------------------------------- |
| `$match`                     | object          | Exact match on one filterable field. Multiple keys in one `$match` are ANDed. |
| `$and`, `$or`                | array of object | Boolean combinators. Nest freely.                                             |
| `$gte`, `$gt`, `$lte`, `$lt` | object          | Range comparisons. Mostly used on `timestamp`.                                |
| `$exists`                    | object          | Presence test, typically on a metadata key.                                   |

```json
{
  "$and": [
    { "$match": { "source": "user:john:wallet" } },
    { "$gte": { "timestamp": "2026-07-01T00:00:00Z" } },
    { "$exists": { "metadata": "order_id" } }
  ]
}
```

Browsers cannot send a body on a GET request, so filtered list calls cannot be exercised
from an in-page Try-it. Use curl or an SDK.

## Idempotency

**Mostly Ledger and Wallets, and not on every write.** Flows manages its own keys internally,
Payments and Reconciliation have no support at all, and Webhooks accepts the header on exactly
one surface — delivery replay, where it is _required_ rather than optional. See the module
table below before relying on retry safety.

Within Ledger and Wallets, the header is accepted on the data-plane writes and ignored
elsewhere. Sending it to an endpoint that does not honor it gets you no replay guarantee,
so a retry there can duplicate the effect:

| Accepts `Idempotency-Key`                                                                                       | Does not                                                                                                                                   |
| --------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| Ledger v2: create transaction, revert transaction, transaction and account metadata write/delete, create schema | Ledger v2 admin and control operations — ledger creation, `_bulk`, ledger-level metadata, buckets, exporters, pipelines, log import/export |
| Wallets: create, update, credit, debit, balance create, hold confirm, hold void                                 | Ledger v1 (`/api/ledger/{ledger}/…`) — no endpoint accepts the header                                                                      |

The endpoint's entry in the API reference is authoritative; check it before depending on
replay. Where the header is accepted, the Ledger stores it permanently against the log
entry. On a second request with the same key:

- **Same request body** — the Ledger replays the original response. Nothing new is created,
  so retrying a timed-out POST is safe.
- **Different request body** — the Ledger returns a `400 VALIDATION` error. The key is
  bound to exactly one input.

Module support:

| Module                   | Support                                                                                                                                                                                                                                                                                         |
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Ledger                   | **2.x:** data-plane writes — key stored on the log entry, no expiry. Not accepted on admin operations or on v1. **3.0 (Stack v4.0):** key scoped to the whole Ledger cluster and retained for a configurable period, 24 hours by default — after that it no longer prevents a second execution. |
| Wallets                  | All write endpoints — forwarded to the Ledger; covers debit, credit, hold confirm, void, and update.                                                                                                                                                                                            |
| Flows                    | Partial — generates its own internal keys per activity; does not accept a client-supplied header.                                                                                                                                                                                               |
| Webhooks                 | Delivery replay only, where the header is **required**. The replay result is cached for 24 hours; reusing a key for a different replay conflicts. No other Webhooks endpoint accepts it.                                                                                                        |
| Payments, Reconciliation | None — check state before retrying a timed-out write.                                                                                                                                                                                                                                           |

`Idempotency-Key` and `reference` are two separate guards. `reference` is a business
deduplication key — unique per ledger forever, and reusing it returns `409 CONFLICT`.
`Idempotency-Key` is a retry guard — if the original call landed, the Ledger hands back
that response without creating a duplicate. When you send both and the `reference` is
already taken, the Ledger returns `CONFLICT` rather than replaying the original response.

```bash
KEY=$(uuidgen)

# first call — creates transaction 1234
curl -X POST "$FORMANCE_API_URL/api/ledger/v2/ledger001/transactions" \
  -H "Authorization: Bearer $FORMANCE_TOKEN" \
  -H "Idempotency-Key: $KEY" \
  -H "Content-Type: application/json" \
  -d '{"script":{"plain":"send [USD/2 100] (source=@alice destination=@bob)"},"reference":"order-9981"}'

# retry after a timeout — returns transaction 1234 again, creates nothing
curl -X POST "$FORMANCE_API_URL/api/ledger/v2/ledger001/transactions" \
  -H "Authorization: Bearer $FORMANCE_TOKEN" \
  -H "Idempotency-Key: $KEY" \
  -H "Content-Type: application/json" \
  -d '{"script":{"plain":"send [USD/2 100] (source=@alice destination=@bob)"},"reference":"order-9981"}'
```

## Time: effective vs recorded

**Ledger only.** `timestamp`, `insertedAt`, `pit` and `oot` are Ledger fields; no other
module exposes a bi-temporal query surface.

Every ledger record has two clocks. `timestamp` is the effective date — when the money
moved in the world; it can be set on creation and backdated, then is immutable. `insertedAt`
is the recorded date — when the Ledger received the write, server-assigned and immutable.
Reports run on effective time; audits run on recorded time. `pit` pins the recorded clock
so a query is reproducible regardless of later writes.

All date-time values are ISO 8601 / RFC 3339 strings (e.g. `2026-01-15T10:30:00Z`).

| Field        | Type               | Notes                                                                                                             |
| ------------ | ------------------ | ----------------------------------------------------------------------------------------------------------------- |
| `timestamp`  | string (date-time) | Effective date. Set on creation; immutable after that. May be backdated.                                          |
| `insertedAt` | string (date-time) | Recorded date. Server-assigned, immutable.                                                                        |
| `pit`        | query, date-time   | Point-in-time. Freezes the recorded clock so the same query returns the same answer forever.                      |
| `oot`        | query, date-time   | Oldest-of-time. Lower bound on the date filter, for windowed historical queries. **`/volumes` only** — see below. |

`pit` is accepted on the Ledger v2 list, count and aggregate endpoints — `accounts`,
`transactions`, `logs`, `aggregate/balances` and `volumes`.

`oot` is narrower: only `GET /api/ledger/v2/{ledger}/volumes` applies it, where it becomes a
lower bound on the same column `pit` bounds from above (`effective_date`, or `insertion_date`
when `insertionDate=true`). The other endpoints parse `oot` and then ignore it, so passing it
there silently returns an unbounded result rather than an error — filter on `timestamp` or
`insertedAt` in the query builder instead. `oot` is also absent from the OpenAPI contract, so
it is not surfaced by the generated SDKs; on `/volumes` the legacy `startTime` and `endTime`
parameters remain accepted as aliases for `oot` and `pit`.
