Skip to main content

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 uses curl 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:

Daml Model

To demonstrate the JSON Ledger API, you need an example Daml model. Run the following command in the console:
A folder named 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:
Before proceeding, compile the Daml model by running the following command inside the folder:
A file named .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. 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.
Open a new Bash terminal, but keep the Canton sandbox running.

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:
You should receive a long YAML document starting with:
This 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:
This should return a response similar to:
Take note of the party ID, which will be used in the following steps. In this example, it is: Alice::122084768362d0ce21f1ffec870e55e365a292cdf8f54c5c38ad7775b9bdd462e141 — but this will differ in your case.

Create a contract

Create a file called create.json with the following content, replacing \<partyId\> with the actual party ID shown earlier:
Now submit the request:
A successful response should look like:
This confirms that a contract was created on the ledger.

Query the ledger

To query the ledger for active contracts, first create a file named `acs.json`:
Replace \<offset\> with the completionOffset you received earlier (e.g., 20). Run the query:
You should receive a response containing contract information. To improve readability, pipe the output to `jq`:
Look for the 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:
Http response different than 200 (e.g., 400, 404, etc.) indicates an error. The response body will contain details about the error. If it does not help, read logs available in the canton sandbox terminal or in the file \<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:

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 API json-api-access-tokens.

Error codes

For more information about error codes returned by the JSON Ledger API, see json-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 as npm 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.x or later.
  • Dpm — Install following these instructions
  • 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:
Start Canton:
Once the Canton console is ready, open a new terminal and navigate to the TypeScript example folder:

Running the Example

Install the project dependencies:
You may see some warnings, which can be ignored for now. The JSON Ledger API provides an OpenAPI specification. You can use this to generate TypeScript client classes (stubs). To generate the TypeScript client:
Check the generated code in the generated/api folder. It should include the TypeScript classes for the API.
This file contains definitions for services and models, such as /v2/commands/submit-and-wait, CreateCommand, and many others. Next, generate TypeScript types from the Daml model:
This creates bindings in the ./generated folder. Each Daml module has its own subfolder, for example: generated/model-tests-1.0.0/lib/Iou. Now compile the TypeScript code:
Once the build succeeds, run the example:
You should see output similar to:

Code Highlights

The JSON Ledger API client is configured in `src/client.ts`:
The openapi-fetch library is used to create the API client.

Allocating a Party

In src/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

In src/commands.ts, a contract is created using:

Querying Contracts

In src/index.ts, contracts are queried like this:

Extending the Example

In this step, you extend the Iou template with a new field and update the TypeScript code accordingly.
  1. Open the Io.daml file in canton/examples/09-json-ledger-api/model.
  2. Modify the Iou template to include a new comment field:
> The comment field is optional, making this a backwards-compatible change.
  1. Stop the Canton server (Ctrl+C) and restart it:
  1. Rebuild the TypeScript bindings:
  1. Update the createContract function in src/commands.ts to include the new field:
  1. Rebuild the TypeScript code:
  1. Run the example:
You won’t see the new comment field in the output yet. To display it, modify the showAcs function call in src/index.ts to include the new field:
The, execute the npm run scenario once again. A TypeScript version is also available that demonstrates how to create a JSON Ledger API client for use in a web browser.