Tenzro
Tutorial — Payments

Create an AP2 mandate

AP2 sessions and mandate pairs are the structured way to tell an agent what it can buy and up to how much. Tenzro validates each mandate against the CheckoutMandate cap, the on-chain DelegationScope, and the runtime SpendingPolicy before any settlement clears.
Level
Intermediate
Time
~15 min
Prerequisites
Delegated machine DID, controller human DID
Stack
TypeScript
01

Open an AP2 session

A session binds the 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.xyz" });

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

Validate the checkout/payment mandate pair

For higher-assurance flows, have the principal sign a CheckoutMandate VDC and the agent sign a PaymentMandate VDC, then check the pair against every ceiling — the CheckoutMandate cap, the DelegationScope, the runtime SpendingPolicy, the on-chain escrow, and the Stripe SPT usage limits.

const result = await client.ap2().validateMandatePair(checkoutVdc, paymentVdc, true);
if (!result.valid) throw new Error(result.error);
03

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.50");
console.log("authorization:", auth.authorization_id);
04

Execute and settle

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

List persisted mandates

Signed checkout/payment mandate pairs persist under the controller DID. Retrieve them to audit what an agent was authorized to buy — each record carries the mandate ids, the controller/agent/merchant DIDs, the max and total amounts, asset, chain, expiry, the delegation-enforced flag, and the stored checkout/payment VDCs.

const { mandates } = await client.ap2().listMandates("did:tenzro:human:controller-1");
for (const m of mandates) {
  console.log(m.mandate_id, m.max_amount, m.total_amount, m.asset);
}
Related
← All tutorials