Store a Numscript program in a ledger when several callers must execute the same reviewed transaction logic. Callers send a name, version, and variables instead of embedding the source in every request.
For production traffic, publish an immutable semantic version and request that exact version. This makes a deployment reproducible and gives the application an explicit rollback target.
Prerequisites#
You need an existing normal ledger and a token with ledger:write to publish and execute programs — the save needs ledger:LedgerWrite, the execution needs ledger:TransactionWrite — plus ledger:read for the verification requests. The virtual scopes below assume the default scope mapping.
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 destination with an address that matches its configured patterns.
Publish an immutable version#
Define a reusable transfer program:
Put the same source in a shell variable for the request below:
Save it under the run-specific name in $NUMSCRIPT_NAME as version 1.0.0. The Idempotency-Key makes the save safe to retry after a lost response:
Ledger parses the program before saving it. A successful save returns 201 Created with the stored program info. Verify the stored source before pointing callers at the new version.
Verify the stored source through an exact-version lookup:
The command exits successfully only when the response reports the requested name, exact version, and submitted source.
Execute the saved program#
Reference the exact version in a transaction request:
Expected result: Ledger returns 201 Created, and .data.transaction.postings contains the movement produced by the exact 1.0.0 version. This example uses world as an explicit funding boundary. For an internal transfer, pass a funded account instead.
Verify both the program choice and the accounting result:
The destination balance increases by 2500 minor units.
Publish a compatible change#
Semantic versions are immutable. Publish changed behavior under a new version instead of overwriting 1.0.0. Version 1.1.0 adds a metadata tag:
Put that source in a shell variable and save it as 1.1.0:
Confirm that both versions remain available:
Expected output, with the run-specific name substituted for <name>:
Move callers to 1.1.0 only after testing representative balances, metadata, and failure cases. Roll back the application by changing its scriptReference.version to 1.0.0; no library mutation is required.
Understand version resolution#
A selector means different things on the read path and the write path. Reads are permissive; execution and publication are not.
Accepted on GET /v3/{ledgerName}/numscripts/{name}?version=:
| Requested version | Resolves to |
|---|---|
1.0.0 | That exact immutable version. |
1.0 | The greatest stored 1.0.x version. |
1 | The greatest stored 1.x.y version. |
latest | The greatest stored semantic version. |
| Omitted | The greatest stored semantic version. latest and an omitted value are the same selector. |
Accepted in scriptReference.version on a transaction:
| Requested version | Behavior | Production guidance |
|---|---|---|
1.0.0 | Executes that exact immutable version. | Recommended for a stable deployment. |
latest | Re-resolves to the greatest stored version on every execution. | Use for development or an explicitly mutable workflow. |
1, 1.0, or omitted | Rejected with 400 and an invalid-version reason. Partial and omitted selectors are read-only. | Not available. Pin an exact version. |
PUT .../numscripts/{name} accepts an exact major.minor.patch only. latest, a partial selector, and an omitted version are all rejected with 400 and an invalid-version reason.
Ledger accepts only numeric selectors with one to three components (1, 1.2, or 1.2.3), the literal latest, or an omitted value. Version ranges, prerelease labels, and build metadata are not supported.
Saving a version advances the program's pointer only when the new version is the greatest stored: the pointer is max(current, new), so adding an older version out of order does not move it. There is no separate mutable latest slot — latest is a selector that re-resolves through that pointer on every request.
latest re-resolves on every execution, so an operation without a retained idempotency outcome can run different source after another caller publishes a greater version. A retained request that submitted latest replays its first outcome: Ledger audits the selector as submitted and never rewrites it to a concrete version, so an unchanged body cannot become an idempotency conflict because the resolution moved. Exact semantic versions avoid the ambiguity altogether.
Check whether a program name is still used#
The usage counter answers one question: is anything still calling this program name? It aggregates invocations across every version stored under that name:
After the example transaction, the result eventually reports a positive count and the greatest effective transaction timestamp seen for this name:
The HTTP route requires the ledger:read virtual scope; the direct gRPC method checks the granular query-read permission. It does not accept a checkpoint or minimum log sequence. The counter normally follows committed writes after a short background interval, but catch-up after startup or restore can take longer; increase USAGE_TIMEOUT_SECONDS or retry the read later in those cases. It counts only committed transactions that used scriptReference: failed or skipped attempts, inline scripts, and idempotent replays do not add another invocation. An unknown program name on an existing ledger returns count: 0; an unknown ledger returns 404.
Treat usage as an operational adoption signal, not a billing counter. The counter is eventually consistent, so a 0 response can mean the program has never run or that the counter has not caught up yet. lastUsed is an effective transaction timestamp, not the time the counter observed the invocation. Entries without a saved-program identity, including records replayed from another ledger, do not add to this count. The count is per program name, not per semantic version.
List the programs in a ledger#
List the current entry for each program in the ledger:
The Ledger 3.0 numscript surface has no delete operation: the routes are GET for the listing, an individual program, its usage, and its versions, plus PUT to save. A published semantic version is immutable and permanent, so retiring a program means moving its callers off it rather than removing it from the ledger. Combine the usage signal with a search of deployed callers and recent transaction evidence before you treat a program name as unused; a low or zero count alone is not enough.
Handle errors and retries#
| Result | Meaning | Action |
|---|---|---|
400 with a parse reason | The Numscript source is invalid | Fix and test the source before publishing a version. |
400 with an invalid-version reason | A save version is not an exact major.minor.patch, or an executable scriptReference.version is neither an exact version nor latest | Publish with major.minor.patch, and execute with an exact version. |
404 | The program or requested version does not exist | Check the ledger, name, and version constraint. |
409 | The semantic version already exists | Do not overwrite it. Compare the stored content, then select the existing version or publish a new one. |
503 | The write path is temporarily unavailable | Read the exact target version before retrying, because the save may have committed. |
PUT .../numscripts/{name} honors the Idempotency-Key header, so send one and retry the identical body under the same key when a network failure makes a save uncertain. If you did not send a key, fetch the exact semantic version and compare its content instead. If it exists and matches, treat the publication as successful. If it differs, preserve the stored version and publish the intended change under a new version.
Continue with Retry Writes Safely and apply it to every transaction that executes a saved program.