Ledger 3.0 has three behaviors that decide how you model a product: cumulative volumes that a reversal never rewinds, colors that segregate balances inside a single asset, and typed metadata that makes a key queryable. This page proves all three against a live server, so you can see the semantics before committing to a design.
Every command runs against one disposable ledger, in one shell session, in order. Each part proves one behavior and links to the page that explains it.
If you have not recorded a transaction yet, start with the Quick Start first — it creates a ledger, records one transaction, and proves that retrying the write does not record it twice. This page assumes that ground.
Ledger's base URL depends on how it is deployed. A Ledger server you run yourself answers at the root of its host. Behind a Formance Stack, requests pass through the Stack Gateway, which serves Ledger under /api/ledger. If you reach Ledger through your Stack's domain you are going through the Gateway; if you started the server yourself, you are not.
Run the commands for your deployment in a shell, and keep that same shell for the rest of the guide. set -eo pipefail stops the shell at the first failing command instead of carrying on with an empty value:
The token needs ledger:read and ledger:write. ledger is the default auth service name. If your deployment uses another service name, replace ledger with your configured name.
Create one run identifier, then the disposable ledger every part below uses. Deriving names from RUN_ID makes the walkthrough safe to repeat:
Generate a new RUN_ID when you repeat this page. Idempotency keys identify a write across the complete Ledger service until their configured retention period expires, even after the disposable ledger is deleted.
Amounts, balances, and volumes#
Record three movements in EUR/2, then read the cumulative volumes they produce. world sends 10000 to Alice, Alice sends 2500 to a merchant, and the merchant sends 500 to platform fees.
Read all three accounts and assert the expected state, rather than relying only on the write responses:
Now revert the fee transaction and read the two affected accounts again. The balances recover, but input and output keep both movements — a reversal adds compensating postings rather than subtracting from cumulative volumes:
Finally, total a set of accounts with the aggregate endpoint. filter is the only account selector it accepts — an unrecognized parameter is ignored rather than rejected, so a misspelled selector returns a successful total over every account in the ledger, world included:
The response sums per asset and color across the selected accounts; it carries no addresses, so nothing in it says which accounts were selected. Prove the filter took effect by comparing it with the unfiltered aggregate, using the asset this section created. world sent 10000, Alice sent 2500, the fee movement sent 500, and reverting that fee added a compensating 500 — so the whole ledger holds 13500 of output while the customers: selection reports Alice's 2500:
If those two are equal, the selector was ignored and the "scoped" total is the whole ledger.
Colors: isolated balances within one asset#
Create three balances on one account in a single transaction — one uncolored, two colored. Color follows the value from source to destination, so a posting cannot debit one color and credit another.
Read the account without a collapse option. Ledger returns one volume row per (asset, color), including color: "" for the uncolored bucket:
Now prove the buckets are isolated. Attempt to spend 5001 from GRANTS, which holds 5000. Capture both accounts before the request so you can show the rejection changed neither:
The status code proves which rule rejected the request. The two independent reads prove the rejection was atomic.
Collapse the colored rows for a reporting view. This sums the buckets and returns the result with color: ""; it does not merge or recolor stored balances:
Never authorize a spend from a collapsed balance when the spend must respect color restrictions. A collapsed empty color is a summary across buckets; an uncollapsed empty color is the real uncolored bucket.
Reclassify value between colors#
There is no in-place recoloring primitive. Move value with one transaction holding two equal postings — retire the old color to world, issue the new color from world. Both postings must be in the same transaction so Ledger commits both or neither.
Read the starting balances first:
Submit both postings in one request:
Read the account again and assert both bucket changes and the invariant total:
These arithmetic assertions rely on the small values in this dataset. Production clients must parse Ledger's balance strings with arbitrary-precision integer arithmetic.
Metadata and typed values#
Save account metadata with its own idempotency key. Note externalSequence: a bare JSON integer above 2^53 - 1 loses precision in the HTTP parser, so the exact value goes over the wire as a string.
Read the account back through a separate request and assert what was stored:
A save merges keys; it never removes ones you omitted. Delete a key explicitly, then prove the others survived:
Do not rely on null in a save request to delete a key — the HTTP parser skips null entries. Use the DELETE .../metadata/{key} route so the deletion is explicit and auditable.
Declare types for indexed queries#
Declaring a type tells Ledger how to interpret a key when it builds and queries a metadata index. Declare riskTier as a bounded integer, the HTTP-safe externalSequence string as uint64, and a transaction timestamp for range queries:
Retrieve the declarations currently stored on the ledger:
Remove a declaration when the key no longer has one query type. Stored values are untouched, but the typed interpretation and its index are dropped, so update dependent queries first:
A declaration does not create an index, and an index is not ready the moment it is registered. Building one and gating on every read replica needs direct per-pod URLs, so it stays with the operational guidance in Metadata and Typed Values.
Clean up#
Delete the disposable ledger, then the working directory. Everything this page created derives from RUN_ID, so nothing outside this run is touched:
If the delete response is lost, repeat the same DELETE with handson:$RUN_ID:delete-ledger, then run the 404 read check again.
Deleting a ledger is not a way to correct history. To undo a recorded business event, use Revert a Transaction.