Cortex

SDK Examples

Copy-paste TypeScript flows for turning onboarding output into executable commerce: discovery, direct payments, swap-before-pay, facilitator/x402 policy, receipts, disputes, analytics, and reputation.

Environment

export API_URL=https://api.cortex.wallyweb.com
export RPC_URL=https://sepolia.base.org
export COMMERCE_REGISTRY_ADDRESS=0xf0bf44b28567f0b3d2370dc7af8a63335746d8d4
export POLICY_MODULE_ADDRESS=0xb2686c5cc3ab7ce45acfe0091698d9b6a16c2d0c
export INTENT_BOOK_ADDRESS=0x16f7e7c4856bad4dcbE61400630087Dab75B229E

Runnable dry-run template

Use ops/sdk-examples/commerce-flow.ts through the demo package to test the SDK flow without sending transactions by default.

cd ops/demo
npm install
npm run sdk:commerce
npm run sdk:payment-rails

Hosted payment rail dry run

ops/sdk-examples/payment-rails.ts consumes hosted catalog and quote URLs, verifies their hashes, computes the quote hash, checks reputation, and prints a rail-specific execution plan before any transaction is sent.

export CATALOG_URL=https://api.cortex.wallyweb.com/catalogs/0x...
export QUOTE_REQUEST_URL=https://api.cortex.wallyweb.com/quote-requests/0x...
export QUOTE_RESPONSE_URL=https://api.cortex.wallyweb.com/quote-responses/0x...
npm run sdk:payment-rails

Current Base Sepolia contract semantics are rail-aware: facilitator and x402 quotes require an active facilitator, while transfer and swap quotes can use address(0) and allow the merchant or agent to record the receipt.

Client Setup

const cortex = new AgentChainClient({
  apiUrl: process.env.API_URL ?? "https://api.cortex.wallyweb.com",
  publicClient,
  walletClient,
  chain: baseSepolia,
  intentBookAddress: process.env.INTENT_BOOK_ADDRESS as `0x${string}`,
  commerceRegistryAddress: process.env.COMMERCE_REGISTRY_ADDRESS as `0x${string}`,
  policyModuleAddress: process.env.POLICY_MODULE_ADDRESS as `0x${string}`,
});

Quote and Direct Transfer

Compute the quote hash, commit it onchain, then pay through a normal ERC-20 transfer.

const quoteHash = await cortex.computeQuoteHash(quote);
await cortex.commitQuote(quote);

await walletClient.writeContract({
  address: quote.token,
  abi: ERC20ABI,
  functionName: "transfer",
  args: [merchantPayoutAddress, quote.amount],
});

Facilitator and x402 Policy

Configure signed payment budgets and record the exact x402 payload hash against policy.

await cortex.setSignedPaymentPolicy({
  merchant: merchantOwnerAddress,
  token: usdcAddress,
  facilitator: facilitatorAddress,
  maxPerPayment: 1_000_000n,
  maxPerDay: 10_000_000n,
  allowed: true,
});

const x402PayloadHash = keccak256(stringToHex(normalizedX402Payload));
await cortex.recordSignedPayment(
  merchantOwnerAddress,
  usdcAddress,
  facilitatorAddress,
  1_000_000n,
  x402PayloadHash,
);

Receipts and Disputes

const receiptTx = await cortex.recordReceipt(quoteHash, resultHash);
const receipts = await cortex.listReceipts({ agent: account.address, merchant_id: 1n });

const disputeTx = await cortex.openDispute(receiptId, reasonHash);
await cortex.resolveDispute(disputeId, 1, resolutionHash);

Full Examples

The full markdown includes direct stablecoin transfer, swap-before-pay, x402 quote binding, receipt/fulfillment, dispute, analytics, and reputation examples.

View full markdown