Skip to content

Examples

HardlyDifficult edited this page Apr 28, 2026 · 1 revision

Examples

Runnable TypeScript examples under examples/ in the canton-node-sdk repository.

Prerequisites

  • cn-quickstart running with OAuth2 enabled
    • Clone: https://github.com/digital-asset/cn-quickstart
    • Setup: cd quickstart && make setup (choose "with OAuth2")
    • Start: cd quickstart && make start
  • SDK: from repo root, npm install

Examples

canton-quickstart.ts — unified client

Demonstrates the Canton class which provides a unified entry point for all API clients:

npx tsx examples/canton-quickstart.ts
  • Initializes all clients with shared configuration
  • Shows how to use ledger, validator, and scan APIs
  • Demonstrates dynamic party ID updates

localnet-with-oauth2.ts — OAuth2 authentication

npx tsx examples/localnet-with-oauth2.ts
  • Automatic token acquisition and refresh
  • Bearer token injection in requests
  • Keycloak/OAuth2 integration

create-party.ts — create and fund a party

npx tsx examples/create-party.ts [party-name] [amount]
npx tsx examples/create-party.ts alice 100
  • Creates user via Validator API
  • Funds party via transfer offer
  • Creates transfer preapproval contract

transfer-amulets.ts — transfer via offer

npx tsx examples/transfer-amulets.ts <receiver-party-id> [amount]
npx tsx examples/transfer-amulets.ts alice::12345... 10
  • Creates a transfer offer
  • Accepts the offer as receiver
  • Two-step transfer flow

external-signing.ts — user-controlled keys

npx tsx examples/external-signing.ts
  • Generates Ed25519 keypair
  • Creates external party on ledger
  • External signing flow

scan-traffic-status.ts — network monitoring

npx tsx examples/scan-traffic-status.ts

Quick reference

Using the Canton class

import { Canton } from '@fairmint/canton-node-sdk';

const canton = new Canton({ network: 'localnet' });

const version = await canton.ledger.getVersion();
const balance = await canton.validator.getWalletBalance();
const health = await canton.scan.getHealthStatus();

Creating a party

import { Canton, createParty } from '@fairmint/canton-node-sdk';

const canton = new Canton({ network: 'localnet' });

const { partyId, preapprovalContractId } = await createParty({
  ledgerClient: canton.ledger,
  validatorClient: canton.validator,
  partyName: 'alice',
  amount: '100',
});

Transferring Amulets

import { Canton, createTransferOffer, acceptTransferOffer } from '@fairmint/canton-node-sdk';

const canton = new Canton({ network: 'localnet' });

const offerId = await createTransferOffer({
  ledgerClient: canton.ledger,
  receiverPartyId: 'bob::123...',
  amount: '50',
  description: 'Payment',
});

await acceptTransferOffer({
  ledgerClient: canton.ledger,
  transferOfferContractId: offerId,
  acceptingPartyId: 'bob::123...',
});

External signing

import { Keypair } from '@stellar/stellar-base';
import { Canton, createExternalParty } from '@fairmint/canton-node-sdk';

const canton = new Canton({ network: 'localnet' });
const keypair = Keypair.random();

const { partyId } = await createExternalParty({
  ledgerClient: canton.ledger,
  keypair,
  partyName: 'external-alice',
  synchronizerId: 'global-synchronizer::...',
});

More resources