Preview. Stack v4.0 has not been released, and what is documented here can still change before it does. The current release is v3.2.Go to v3.2
Ledger

Quick Start

Create a ledger, record your first transactions, and verify the resulting balances.

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:

bash
set -eo pipefail

export LEDGER_URL="https://stack.example.com/api/ledger"
export LEDGER_TOKEN="<bearer-token>"
CURL_AUTH=(-H "Authorization: Bearer $LEDGER_TOKEN")

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:

bash
export RUN_ID="$(date -u +%Y%m%dT%H%M%SZ)-$$"
export LEDGER_NAME="quickstart-$RUN_ID"
export TRANSACTION_REFERENCE="quickstart-deposit-$RUN_ID"

export CREATE_LEDGER_KEY="quickstart:$RUN_ID:create-ledger"
export TRANSACTION_KEY="quickstart:$RUN_ID:deposit"
export DELETE_LEDGER_KEY="quickstart:$RUN_ID:delete-ledger"

export QUICKSTART_DIR="${TMPDIR:-/tmp}/formance-ledger-$RUN_ID"
mkdir -p "$QUICKSTART_DIR"

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:

curl -X GET $LEDGER_URL/clusterz \
  --fail --silent --show-error --output "$QUICKSTART_DIR/clusterz.json"
GET/clusterz
Assert the cluster reports ok
jq -e '.data.status == "ok"' "$QUICKSTART_DIR/clusterz.json"

The response must report an ok status:

json
{
  "data": {
    "status": "ok"
  }
}

Then prove that your credentials can read Ledger data:

curl -X GET $LEDGER_URL/v3/ \
  "${CURL_AUTH[@]}" --fail-with-body --silent --show-error --output "$QUICKSTART_DIR/ledgers.json"
GET/v3/
Assert the response carries a data array
jq -e '.data | type == "array"' "$QUICKSTART_DIR/ledgers.json"

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.

curl -X POST "$LEDGER_URL/v3/\$LEDGER_NAME" \
  -H "Idempotency-Key: $CREATE_LEDGER_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "mode": "NORMAL"
  }' \
  "${CURL_AUTH[@]}" --fail-with-body --silent --show-error --output "$QUICKSTART_DIR/create-ledger.json"
POST/v3/$LEDGER_NAME

Verify the ledger independently instead of relying only on the write response:

curl -X GET "$LEDGER_URL/v3/\$LEDGER_NAME" \
  "${CURL_AUTH[@]}" --fail-with-body --silent --show-error --output "$QUICKSTART_DIR/ledger.json"
GET/v3/$LEDGER_NAME
Assert the ledger exists in NORMAL mode
jq -e --arg ledger "$LEDGER_NAME" '
  .data
  | (.name == $ledger and ((.mode // "NORMAL") == "NORMAL"))
' "$QUICKSTART_DIR/ledger.json"

jq '.data | {name, mode: (.mode // "NORMAL")}' \
  "$QUICKSTART_DIR/ledger.json"

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:

json
{
  "name": "quickstart-<run-id>",
  "mode": "NORMAL"
}

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:

bash
jq -n --arg reference "$TRANSACTION_REFERENCE" '{
  reference: $reference,
  postings: [
    {
      source: "world",
      destination: "wallets:alice",
      asset: "USD/2",
      amount: 10000
    }
  ]
}' > "$QUICKSTART_DIR/transaction-request.json"
curl -X POST "$LEDGER_URL/v3/\$LEDGER_NAME/transactions" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $TRANSACTION_KEY" \
  "${CURL_AUTH[@]}" --fail-with-body --silent --show-error --data-binary @"$QUICKSTART_DIR/transaction-request.json" --output "$QUICKSTART_DIR/transaction-first.json"
POST/v3/$LEDGER_NAME/transactions

USD/2 has two decimal places, so the integer 10000 represents USD 100.00. Store the transaction ID for the next read:

bash
export TRANSACTION_ID="$(jq -er '
  .data.transaction.id | select(. != null)
' "$QUICKSTART_DIR/transaction-first.json")"

test -n "$TRANSACTION_ID"

Read the transaction and account

Read the transaction by its server-assigned ID:

curl -X GET "$LEDGER_URL/v3/\$LEDGER_NAME/transactions/\$TRANSACTION_ID" \
  "${CURL_AUTH[@]}" --fail-with-body --silent --show-error --output "$QUICKSTART_DIR/transaction-read.json"
GET/v3/$LEDGER_NAME/transactions/$TRANSACTION_ID
Assert the stored transaction matches what was sent
jq -e \
  --arg id "$TRANSACTION_ID" \
  --arg reference "$TRANSACTION_REFERENCE" '
    .data.transaction
    | ((.id | tostring) == $id)
      and (.reference == $reference)
      and (.postings | length == 1)
  ' "$QUICKSTART_DIR/transaction-read.json"

Then read Alice's account:

curl -X GET "$LEDGER_URL/v3/\$LEDGER_NAME/accounts/wallets:alice" \
  "${CURL_AUTH[@]}" --fail-with-body --silent --show-error --output "$QUICKSTART_DIR/account-first.json"
GET/v3/$LEDGER_NAME/accounts/wallets:alice
Assert Alice holds one uncolored USD volume of 10000
jq -e '
  .data.address == "wallets:alice"
  and ([
    .data.volumes[]
    | select(.asset == "USD/2" and .color == "")
    | .volumes
  ] == [{input: "10000", output: "0", balance: "10000"}])
' "$QUICKSTART_DIR/account-first.json"

jq '.data | {address, volumes}' "$QUICKSTART_DIR/account-first.json"

The account contains one uncolored USD volume. Its input and balance are both the string "10000", and its output is "0".

json
{
  "address": "wallets:alice",
  "volumes": [
    {
      "asset": "USD/2",
      "color": "",
      "volumes": {
        "input": "10000",
        "output": "0",
        "balance": "10000"
      }
    }
  ]
}

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.

curl -X POST "$LEDGER_URL/v3/\$LEDGER_NAME/transactions" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $TRANSACTION_KEY" \
  "${CURL_AUTH[@]}" --fail-with-body --silent --show-error --data-binary @"$QUICKSTART_DIR/transaction-request.json" --output "$QUICKSTART_DIR/transaction-retry.json"
POST/v3/$LEDGER_NAME/transactions

Compare the two transaction IDs:

Assert the retry returned the first transaction ID
jq -e --arg first "$TRANSACTION_ID" '
  (.data.transaction.id | tostring) == $first
' "$QUICKSTART_DIR/transaction-retry.json"

jq --arg first "$TRANSACTION_ID" '
  {
    sameTransaction: ((.data.transaction.id | tostring) == $first),
    first: $first,
    retry: (.data.transaction.id | tostring)
  }
' "$QUICKSTART_DIR/transaction-retry.json"

sameTransaction must be true. Verify the accounting result as a separate check:

curl -X GET "$LEDGER_URL/v3/\$LEDGER_NAME/accounts/wallets:alice" \
  "${CURL_AUTH[@]}" --fail-with-body --silent --show-error --output "$QUICKSTART_DIR/account-after-retry.json"
GET/v3/$LEDGER_NAME/accounts/wallets:alice
Assert the balance is still 10000, not 20000
balance="$(jq -er '
  .data.volumes[]
  | select(.asset == "USD/2" and .color == "")
  | .volumes.balance
' "$QUICKSTART_DIR/account-after-retry.json")"

test "$balance" = "10000"

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:

bash
status="$(curl --fail-with-body --silent --show-error \
  --request DELETE "$LEDGER_URL/v3/$LEDGER_NAME" \
  "${CURL_AUTH[@]}" \
  --header "Idempotency-Key: $DELETE_LEDGER_KEY" \
  --output "$QUICKSTART_DIR/delete-ledger.json" \
  --write-out '%{http_code}')"

test "$status" = "204"

read_status="$(curl --silent --show-error \
  "${CURL_AUTH[@]}" \
  --output "$QUICKSTART_DIR/ledger-after-delete.json" \
  --write-out '%{http_code}' \
  "$LEDGER_URL/v3/$LEDGER_NAME")"

test "$read_status" = "404"

rm -rf "$QUICKSTART_DIR"

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#

ResultWhat it meansWhat to check
/clusterz returns 503The 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 401Ledger did not accept the bearer token.Check the token issuer, audience, expiration, and whether the Authorization header reached Ledger.
A request returns 403The token is valid but lacks the required permission.Use ledger:read for reads and ledger:write for writes.
A write returns 409The 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 bodyThe 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 404The ledger, transaction, or account was not found.Check LEDGER_NAME, TRANSACTION_ID, and the account address before retrying.

Continue from here#