Tenzro
Tutorial — Agents

Build an AI payment agent

Equip a delegated agent to clear MPP, x402, and AP2 challenges. The AP2 cart mandate, the on-chain delegation scope, and a runtime spending policy together act as a three-axis spend ceiling.
Level
Advanced
Time
~30 min
Prerequisites
Delegated machine identity, funded wallet
Stack
TypeScript
01

Open an AP2 session

An AP2 session binds an agent DID to a provider DID for a specific service with a max-amount ceiling.

import { TenzroClient } from "tenzro-sdk";
const client = new TenzroClient({ endpoint: "https://rpc.tenzro.network" });

const session = await client.ap2().createSession(
  "did:tenzro:machine:agent-1",
  "did:tenzro:machine:provider-1",
  "https://api.tenzro.network/inference",
  "1.00",
  "TNZO",
);
02

Authorize a payment within the session

Each authorization is scoped under the session ceiling. The node checks the agent's DelegationScope and runtime SpendingPolicy at sign time.

const auth = await client.ap2().authorizePayment(session.session_id, "0.10");
console.log("authorization:", auth.authorization_id);
03

Validate the mandate pair (optional gate)

For higher-assurance flows, sign Intent and Cart VDCs and validate the pair against the TDIP delegation gate.

const result = await client.ap2().validateMandatePair(intentVdc, cartVdc, true);
if (!result.valid) throw new Error(result.error);
04

Execute and confirm

Execution settles the previously-authorized amount and emits a receipt.

const receipt = await client.ap2().executePayment(session.session_id, auth.authorization_id);
console.log("settled:", receipt);
05

Or pay an HTTP 402 challenge directly

For one-shot resources the agent can also call the MPP or x402 paths by URL — both are gated by the same DelegationScope + SpendingPolicy.

const r1 = await client.payment.payMpp("https://api.tenzro.network/inference", "did:tenzro:machine:agent-1");
const r2 = await client.payment.payX402("https://api.example.com/data", "did:tenzro:machine:agent-1");
Related
← All tutorials