Use explicit postings when your application has already decided every source, destination, asset, and amount. Ledger commits the postings as one transaction or rejects the request without applying a partial transaction.
Use Numscript instead when the result depends on balances, fallback accounts, or percentage splits that Ledger should resolve at execution time.
Prerequisites#
You need:
- an existing ledger in normal mode;
- a token with
ledger:writefor the transaction andledger:readfor the account checks; - the base URL of the Ledger HTTP service, without a trailing slash;
- amounts converted to integer minor units.
These HTTP write examples assume that the cluster accepts unsigned requests. If your cluster requires Ed25519 request signatures, follow Configure Request and Response Signing and use a signed gRPC client or ledgerctl configured with a signing key; the HTTP write handlers submit unsigned requests.
If the ledger enforces account types, replace the demo addresses with values that match its configured patterns.
The examples use these environment variables:
Amounts are integers
Ledger stores amounts as integers. The /2 precision in USD/2 means that 10000 represents USD 100.00. Do not send a floating-point amount.
Create a multi-posting transaction#
This transaction receives USD 100.00 into a clearing account, sends USD 97.00 to a merchant, and records a USD 3.00 fee. The clearing account ends at zero.
The response contains the generated transaction ID, the three postings, and the post-commit volumes captured at this transaction's sequence. Ledger always returns them; no request field switches them on or off.
Inspect the fields that matter to this operation:
Expected result:
Ledger also returns a generated transaction ID in .data.transaction.id. Post-commit volume amounts are strings because they can exceed the safe integer range of JavaScript.
Verify the balances independently#
Read each account instead of relying only on the write response:
Each assertion prints true and exits successfully. The clearing account returns to zero, while the merchant and fee accounts receive 9700 and 300 minor units respectively.
Control the transaction record#
The request accepts exactly one content source: postings, script, or scriptReference. Do not combine them.
| Field | Use it for | Boundary |
|---|---|---|
reference | A stable business identifier | Unique within one ledger. A duplicate returns a conflict. |
timestamp | The business-effective time | RFC 3339. If omitted, Ledger uses the current time. insertedAt still records when Ledger accepted the write. |
metadata | Data attached to the transaction | Use strings, booleans, or JSON integers in the interoperable safe range. null omits the key rather than storing a null value; use the metadata delete endpoints to remove a key that is already stored. Fractional numbers are rejected, and large identifiers that must remain exact should be strings. |
accountMetadata | Metadata updates for accounts involved in the transaction | Keys are account addresses; apply only changes required by the same operation. |
color | A segregation key on a posting | Optional; A-Z only, at most 32 characters. Omitted means the uncolored bucket. |
force | Bypass the normal balance floor | Optional boolean. It can create negative source balances; keep it false for normal writes. |
The HTTP and gRPC create surfaces, and ledgerctl transactions create --force, support forced writes. force: true bypasses balance checks and can create negative balances. Reserve it for a controlled correction or import, record the approval context in metadata, and verify every affected account afterward.
Handle errors#
| Result | Meaning | Action |
|---|---|---|
400 | Invalid body, more than one content source, malformed posting, or a business validation failure | Read errorCode, correct the request, and use a new idempotency key if the logical operation changes. |
401 / 403 | Missing credentials or insufficient scope | Refresh the token or grant ledger:write. Do not retry unchanged credentials. |
404 | The ledger does not exist | Check LEDGER_NAME and the target environment. |
409 | The reference is already claimed or the idempotency key conflicts | Determine whether the existing transaction is the intended result before creating another operation. |
503 | Ledger cannot currently accept the write, or concurrent state invalidated proposal inputs | Honor Retry-After when present; otherwise use bounded exponential backoff. Retry with the same key and identical body while the outcome is uncertain. |
Retry or correct the operation#
Keep the same Idempotency-Key and the same request body when a timeout or 503 leaves the outcome uncertain. Ledger returns the original outcome instead of creating a second transaction while the key remains in its retention window.
A definitive balance or account-rule rejection that Ledger recorded as the operation's outcome is also retained with its key. If you fund an account, change force, or otherwise correct the logical operation, send the corrected content with a new idempotency key. The HTTP status alone does not prove whether a rejection was retained; see Retry Writes Safely for the boundary.
A committed transaction is immutable. Do not attempt to delete or edit its postings. Use Revert Transactions to record a compensating movement, or create a separate correcting transaction when a full reversal does not match the business event.
Continue with Retry Writes Safely before adding this call to a production client.