Documentation Index
Fetch the complete documentation index at: https://docs.canton.network/llms.txt
Use this file to discover all available pages before exploring further.
Get started with Canton and the JSON Ledger API
This tutorial demonstrates how to interact with the Canton Ledger using the JSON Ledger API from the command line. It usescurl and optionally websocat to communicate with the Ledger.
Overview
This example shows how to:- enable the JSON Ledger API in the Canton configuration
- create a contract using the JSON Ledger API and basic Bash tools (
curl) - list active contracts using
curl - basic error handling and troubleshooting
Prerequisites
Tools
Before running the project, ensure you have the following installed:- A Bash-compatible terminal (e.g., macOS Terminal, Git Bash, etc.)
- Dpm — Install following these instructions
curl -sSL https://get.digitalasset.com/install/install.sh | sh -s(check installed dpm version by entering in bash console
dpm version --active, it should be at least )canton— Canton release with installation and examples, check Canton demo for detailscurl— command-line HTTP client: https://github.com/curl/curl (installation: https://curl.se/download.html)- (Optional)
jq— command-line JSON processor: https://github.com/jqlang/jq (installation: https://jqlang.org/download/)
Daml Model
To demonstrate the JSON Ledger API, you need an example Daml model. Run the following command in the console:json-tests should be created. Check its contents and inspect the code in daml/Main.daml.
It should contain a Daml model with a template named Asset:
.daml/dist/json-tests-0.0.1.dar should be created.
Starting Canton with JSON Ledger API
First, ensure you can run the Canton sandbox with the JSON Ledger API enabled. Navigate to the folder where you unpacked the canton distribution. There should be abin subfolder containing the canton executable.
Start the Canton sandbox, providing a path to the created dar file:
To simplify this tutorial, no security-related configuration is used. You will be able to send commands as any user.
Verification - download OpenAPI
To verify that the JSON Ledger API is working, check if the documentation endpoint is available. In a new Bash terminal, run:openapi.yaml document provides an overview of the available endpoints and can be pasted into a tool like https://editor-next.swagger.io.
It can also be used to generate client stubs in languages like Java or TypeScript.
You can use the
http://localhost:757/livez endpoint to check whether the server is running. It might be used as a health check in production environments.Create a party
Run this command in the terminal:Alice::122084768362d0ce21f1ffec870e55e365a292cdf8f54c5c38ad7775b9bdd462e141 — but this will differ in your case.
Create a contract
Create a file calledcreate.json with the following content, replacing \<partyId\> with the actual party ID shown earlier:
Query the ledger
To query the ledger for active contracts, first create a file named `acs.json`:\<offset\> with the completionOffset you received earlier (e.g., 20).
Run the query:
createdEvent section, which contains contract details like:
Troubleshooting
If you encounter issues while calling the using curl - you should enable-v (verbose) mode to see the request and response details. For instance: .. code-block:
\<canton_installation\>/logs/canton.log.
If nothing is returned when you query localhost:7575/v2/state/active-contracts ensure that the offset provided is correct and corresponds to the completionOffset from the localhost:7575/v2/commands/submit-and-wait command. You can also check current offset by running: .. code-block:
curl localhost:7575/v2/state/ledger-end
Next steps
Canton examples
For a more advanced scenario involving two parties, explore the examples provided with Canton installation:\<canton_installation\>/examples/json-ledger-api\>
A TypeScript version is also available that demonstrates how to create a JSON Ledger API client for use in a web browser.
OpenAPI and AsyncAPI
Read more about the OpenAPI and AsyncAPI specifications for the Canton JSON Ledger API:references_json-api.
Authentication and security
Read how to configure and use jwt token to access JSON Ledger APIjson-api-access-tokens.
Error codes
For more information about error codes returned by the JSON Ledger API, seejson-error-codes.
Get Started with Canton, the JSON Ledger API, and TypeScript
This tutorial shows you how to interact with a Canton Ledger using the JSON Ledger API and TypeScript.Overview
You will use and modify an existing example project provided with the Canton distribution.Prerequisites
You should be familiar with the basics of TypeScript and tools such asnpm and node.js.
Tools
Before starting, ensure you have the following installed:- Node.js and npm — Download from https://nodejs.org/en/download/. Recommended version:
18.20.xor later. - Dpm — Install following these instructions
curl -sSL https://get.digitalasset.com/install/install.sh | sh -s
Verify the installation by running:
- Canton — Includes pre-built examples. See the Canton demo for details.
Example TypeScript Project
Open a terminal and navigate to the JSON Ledger API example folder:Running the Example
Install the project dependencies:generated/api folder. It should include the TypeScript classes for the API.
/v2/commands/submit-and-wait, CreateCommand, and many others.
Next, generate TypeScript types from the Daml model:
./generated folder. Each Daml module has its own subfolder, for example: generated/model-tests-1.0.0/lib/Iou.
Now compile the TypeScript code:
Code Highlights
The JSON Ledger API client is configured in `src/client.ts`:openapi-fetch library is used to create the API client.
Allocating a Party
Insrc/user.ts, a party is allocated as follows:
openapi-fetch uses the Indexed Access Types TypeScript feature to provide type safety. The example above looks like untyped JavaScript, but it is in fact type-safe.Creating a Contract
Insrc/commands.ts, a contract is created using:
Querying Contracts
Insrc/index.ts, contracts are queried like this:
Extending the Example
In this step, you extend theIou template with a new field and update the TypeScript code accordingly.
- Open the
Io.damlfile incanton/examples/09-json-ledger-api/model. - Modify the
Ioutemplate to include a newcommentfield:
comment field is optional, making this a backwards-compatible change.
- Stop the Canton server (
Ctrl+C) and restart it:
- Rebuild the TypeScript bindings:
- Update the
createContractfunction insrc/commands.tsto include the new field:
- Rebuild the TypeScript code:
- Run the example:
comment field in the output yet. To display it, modify the showAcs function call in src/index.ts to include the new field:
npm run scenario once again.