x402 Payments
Treza integrates the x402 protocol to enable instant, autonomous stablecoin micropayments for API access. AI agents and developers can pay for Treza services programmatically using USDC on Base — no API keys, subscriptions, or manual billing required.
Two endpoints are payable:
Pay-per-video: send a prompt and a payment, get a finished clip back. No Treza account required.
Credit top-up: add to the prepaid balance that pipeline runs draw from.
What is x402?
x402 is an open payment protocol built by Coinbase that uses the HTTP 402 Payment Required status code. When a client requests a paid resource:
The server responds with
402and payment instructions (price, network, recipient)The client signs a USDC payment
The client retries the request with a
Payment-SignatureheaderThe server verifies the payment, returns the data, and settles on-chain
Payments are instant, permissionless, and settled in USDC on Base (L2).
How Treza Uses x402
Paid Endpoints
/api/x402/video
POST
from $1.64
Generate a video from a prompt and return it. Priced per second of output
/api/billing/credits/x402
POST
$5.00
Top up a prepaid credit balance
Pay-Per-Video
POST /api/x402/video generates a video from a text prompt and returns it. Pay once per clip. No Treza account, API key, or signup is required. The payment is the only credential.
Price
Price is quoted per request and scales with clip length. Read it off the 402 challenge rather than hard-coding it.
5 seconds
$1.64
10 seconds
$3.27
15 seconds
$4.90
16:9 and 9:16 cost the same.
Request
prompt
Yes
—
What the video should show. Max 2000 characters
seconds
No
5
One of 5, 10, 15. Drives the price
aspectRatio
No
16:9
16:9 or 9:16
A request asking for a size that is not on sale is refused with a 400 before the payment settles, so a malformed call costs nothing.
Flow
Renders take a few minutes, so a paid call returns 202 immediately with a run id and a statusUrl containing a signed token. Poll that URL for the result.
Any x402 client signs and retries for you. On success:
Poll statusUrl until status is no longer running. The token in the URL is the only credential it needs.
Billing
Your payment credits an account keyed to your wallet address, and the render is charged against that balance. Anything left over stays as credit for your next call. The same wallet always maps to the same account.
A failed render is not charged, so the payment remains available for a retry.
Full example
A worked example is at trezalabs.com/x402.
Funding an Account Without a Human
This is the one most agents want. Pipeline runs draw a prepaid credit balance, and an agent holding a wallet can refill that balance itself, with nobody signing in to a billing page.
Ask what the balance is and how to add to it, using get_credit_balance over MCP or GET /api/account/balance with an API key:
An x402 block means agent-native top-up is available on this deployment. topUpUrl is the human path; the rest of this page is the other one.
Paying the endpoint
PAYMENT-SIGNATURE carries the payment and is the only required header.
Authorization is optional and selects which account is credited. Send a Treza credential (API key, OAuth token, or session) to credit that account; send none and the credits go to an account keyed to the paying wallet address.
A successful call returns the grant and the transaction that paid for it:
Call it again to add another $5. The credits are spendable immediately.
Paying twice for the same thing is not possible
The ledger entry is keyed to the settled transaction hash, so a repeated request credits once. A replayed payment does not even reach that check: the authorization nonce is already spent on-chain, and the facilitator rejects it.
When a payment is refused
The response says why, rather than repeating the challenge:
invalid_payload: ... execution reverted
The wallet cannot cover the payment
invalid_payload: authorization nonce already submitted
This payment was already spent
Invalid or expired credentials
The bearer is wrong, or the key lacks a pipelines: scope
x402 payments are not configured on this deployment
Agent-native top-up is off here; use topUpUrl
Authorization is checked before the payment settles, so a request refused for a bad bearer costs nothing.
Payment Flow
Client-Side: Paying for Treza Services
Using the Treza SDK (Enclave as Wallet)
A Treza Enclave can act as the payment wallet itself, keeping the private key inside the hardware-isolated TEE. This is optional; any viem-compatible account works just as well.
Install Dependencies
Auto-Paying Fetch
The simplest approach — createEnclaveFetch returns a fetch function that automatically handles 402 responses:
x402 Client (Advanced)
For more control, use the x402 client directly:
Enclave Account (Low-Level)
Create a viem-compatible account backed by the enclave for use with any x402 library:
Using Any x402 Client (External Wallet)
You don't need the Treza SDK to pay. Any x402-compatible wallet works:
Using curl (Manual)
To inspect the 402 response without paying:
Amounts are in token atomic units, so 1640000 is 1.64 USDC.
Server-Side: Adding x402 to Your Own Endpoints
If you're building on the Treza platform and want to monetize your own API endpoints, use the withX402Payment wrapper.
Basic Usage
With Bazaar Discovery
Make your endpoint discoverable by AI agents on the x402 Bazaar:
Declare method and bodyType for any route that takes a request body. Without them the listing describes the route as query-parameterized, and an agent will send its arguments in the URL of a request that only reads JSON.
Environment Variables
X402_ENABLED
No
false
Set to true to enable x402 payment gating
TREZA_X402_PAY_TO
Yes (if enabled)
none
Wallet address that receives USDC payments
X402_NETWORK
No
eip155:8453
Network identifier (Base mainnet)
X402_FACILITATOR_URL
No
https://api.cdp.coinbase.com/platform/v2/x402
Payment verification facilitator
X402_CDP_API_KEY
Yes (CDP mainnet)
none
CDP API key id; the CDP facilitator authenticates every call
X402_CDP_API_SECRET
Yes (CDP mainnet)
none
CDP API key secret
X402_TOPUP_USD
No
5
USD credited per successful top-up call
Mainnet settlement runs through Coinbase's facilitator, which authenticates every verify and settle call. Without X402_CDP_API_KEY and X402_CDP_API_SECRET the endpoint fails closed with a 503 and stops advertising itself, rather than answering payments it cannot settle.
Testnet Configuration
Mainnet Configuration
Discovering Payable Services
AI agents can discover Treza's x402-payable endpoints through the Bazaar:
Security Model
TEE Signing
Payment signatures are created inside the hardware-isolated enclave — private keys never leave the Nitro Enclave boundary
Attestation Verification
Optional pre-signing attestation check ensures the enclave is untampered before authorizing payments
Facilitator Verification
Payment signatures are verified by the Coinbase facilitator before the server returns data
On-Chain Settlement
Payments settle as real USDC transfers on Base, providing an immutable audit trail
Replay Protection
Each payment signature is bound to a specific request and cannot be reused
Architecture
FAQ
Do I need USDC to use Treza? Only if x402 is enabled on the endpoint you're calling. During development and on testnet, x402 is disabled by default.
Which network are payments on? Base (Coinbase L2). Testnet uses Base Sepolia, mainnet uses Base mainnet. Both settle in USDC.
Can I use a regular wallet instead of an enclave? Yes. Any viem-compatible account or x402 client works. The enclave-as-wallet pattern is optional — it just adds the security of TEE-based key management.
How much do API calls cost? Video generation is priced per second of output, from $1.64 for a five-second clip; the 402 challenge quotes the exact figure for what you asked for. Credit top-up is $5.00 per call.
Do I need a Treza account? No, for either x402 endpoint. Video generation needs only the payment, and credit top-up without a bearer credits an account keyed to your wallet address. An account is required for the pipeline API.
What if the render fails? A failed render is not charged. The payment stays as credit on your wallet's account for a retry.
Is there a minimum balance? No minimum. You just need enough USDC in your wallet to cover the call price.
Related
Last updated