Pay for an API
If your service is the one calling a paid API, you pay from the other side with @chainsafe/x402-server-sdk. Given your Canton party key and a wallet-sdk ledger connection, it prepares and Ed25519-signs a Canton Coin transfer and wraps fetch so a 402 is paid and the request retried transparently — you call the URL and get the paid response back.
Before you start
- A funded payer party — its
partyIdand Ed25519 signing key (privateKey, the 32-byte seed; the public half is published on the ledger, which is where a facilitator reads it), holding enough Canton Coin to cover the price. - A connection to your participant's Ledger API — its URL plus OAuth2 client credentials (LocalNet uses a self-signed token instead).
- The Amulet Token Standard registry URL — your validator's scan-proxy for Amulet, where the payer fetches the
TransferFactory. - The facilitator the API you are paying uses — the DevNet and MainNet facilitators are listed on Networks.
- The DSO party (the Amulet asset admin) — from
extra.instruments[].instrumentId.admininGET $FACILITATOR_URL/v2/supportedon the facilitator the API you are paying uses.
Steps
Install the payer SDK
The payer SDK and the Canton wallet-sdk it signs with.
bashnpm install @chainsafe/x402-server-sdk @canton-network/wallet-sdkConnect and build the payer
Pick the network preset (
devnetConfig,mainnetConfig, orlocalnetConfig), open a wallet-sdk connection withSDK.create(), then construct aCantonX402Payerfrom your party key and the Amulet registry. The payer only pays requirements on its ownnetwork, so that must be thenetworkthatGET $FACILITATOR_URL/v2/supportedreturns; passnetworkto the preset to override its default.Wrap fetch and call the endpoint
createX402Fetch(payer)returns afetchthat pays402s for you. Call the gated URL through it and read the paid response — no 402 handling in your own code.
Putting it together
import { SDK } from "@canton-network/wallet-sdk";
import { CantonX402Payer, createX402Fetch, devnetConfig } from "@chainsafe/x402-server-sdk";
const net = devnetConfig({ ledgerClientUrl: process.env.LEDGER_CLIENT_URL! });
const registryUrl = process.env.AMULET_REGISTRY_URL!; // Amulet Token Standard registry
const DSO_PARTY = "DSO::1220be58c2…"; // from GET /v2/supported
// OAuth2 for your participant's Ledger API (localnet uses method: "self_signed").
const auth = {
method: "client_credentials",
configUrl: process.env.CANTON_OAUTH_CONFIG_URL!,
credentials: {
clientId: process.env.CANTON_OAUTH_CLIENT_ID!,
clientSecret: process.env.CANTON_OAUTH_CLIENT_SECRET!,
scope: net.auth.scope,
audience: net.auth.audience,
},
} as const;
const sdk = await SDK.create({
auth,
ledgerClientUrl: net.ledgerClientUrl,
token: { auth, registries: [registryUrl] },
});
const payer = new CantonX402Payer({
sdk,
key: {
partyId: process.env.PAYER_PARTY_ID!,
privateKey: process.env.PAYER_PRIVATE_KEY!,
},
network: net.network,
registries: [{ instrumentId: { id: "Amulet", admin: DSO_PARTY }, registryUrl }],
});
// A fetch that pays 402s automatically.
const pay = createX402Fetch(payer);
// Call the gated endpoint — the 402 is paid and the request retried for you.
const res = await pay("https://api.example.com/paid");
const data = await res.json();
console.log(data);The examples/mortgage pair in the SDK repo is a full runnable payer + merchant, with the complete credential wiring in its .env.example.
Next
Payer SDK covers auth on each network, several payers, and writing your own, and Pending payments what to do when a payment's outcome is not known yet.