Get from zero to your first transaction, and test that a retry did not record it twice. Create a ledger, record a posting into an account, read the balance back to confirm it, and send the same write a second time without changing the result.
Estimated effort: 15 minutes
Every step goes through the HTTP API, which accepts an Idempotency-Key header on each write. That header is what makes the last step possible. ledgerctl over gRPC is the usual tool for day-to-day interactive work; the retry is easier to follow when each request is written out in full.
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 a run identifier. The disposable ledger name, transaction reference, idempotency keys, and temporary files are derived from it:
If you repeat this tutorial, make sure to generate a new RUN_ID. Idempotency keys identify a write across the complete Ledger service until their configured retention period expires, even after the disposable ledger is deleted.
Record and verify the transaction#
Verify the connection
Check cluster availability before sending a write:
The response must report an ok status:
Then prove that your credentials can read Ledger data:
The data field contains an array. An empty array is a valid result when the service does not contain a ledger yet.
Create a normal ledger
Create a normal ledger explicitly. The idempotency key makes this creation safe to retry if the response is lost.
Verify the ledger independently instead of relying only on the write response:
NORMAL is the default mode. The current server can omit mode for a normal ledger, so the display command normalizes an absent value to NORMAL. Expected result:
Record a posting
Record USD 100.00 entering the modeled system and arriving in Alice's wallet. The HTTP API uses world as the source account address. Numscript represents the same special account as @world.
Build the request once so a retry can reuse byte-identical content:
USD/2 has two decimal places, so the integer 10000 represents USD 100.00. Store the transaction ID for the next read:
Read the transaction and account
Read the transaction by its server-assigned ID:
Then read Alice's account:
The account contains one uncolored USD volume. Its input and balance are both the string "10000", and its output is "0".
Retry the write safely
Send the exact same body with the exact same idempotency key. This models a client retry after a timeout or lost response.
Compare the two transaction IDs:
sameTransaction must be true. Verify the accounting result as a separate check:
The balance must remain 10000, not 20000.
Reuse an idempotency key only for an identical request. Ledger rejects the same key with different content because it cannot safely decide which write you intended.
If the first request times out, the connection closes, or a 5xx arrives after the body was sent, the transaction may already be committed. Retry the saved transaction-request.json with the same TRANSACTION_KEY until you receive a definitive response. Do not generate a new key while the outcome is unknown.
Clean up the test ledger
Delete only the disposable ledger created for this guide:
If the delete response is lost, repeat the same DELETE with DELETE_LEDGER_KEY, then perform the 404 read check again.
Ledger deletion is not a way to correct production history. Use transaction correction workflows for recorded business events.
Troubleshoot the workflow#
| Result | What it means | What to check |
|---|---|---|
/clusterz returns 503 | The endpoint cannot currently serve cluster-dependent traffic. | Wait for the service to become available or ask the operator to check cluster health. Do not send the write yet. |
A request returns 401 | Ledger did not accept the bearer token. | Check the token issuer, audience, expiration, and whether the Authorization header reached Ledger. |
A request returns 403 | The token is valid but lacks the required permission. | Use ledger:read for reads and ledger:write for writes. |
A write returns 409 | The request conflicts with existing state. | Check whether the ledger or transaction reference already exists, or whether an idempotency key was reused with different content. |
A write times out, loses the connection, or returns 5xx after sending the body | The outcome is unknown; Ledger may already have committed the write. | Retry the byte-identical request with the same idempotency key. Never switch keys until a definitive response or an independent read establishes the outcome. |
A read returns 404 | The ledger, transaction, or account was not found. | Check LEDGER_NAME, TRANSACTION_ID, and the account address before retrying. |