x402 Payment Integration: Autonomous Micropayments for AI Agents
An AI agent with a USDC wallet can buy generated media from Treza over x402: a video, a voiceover, a music track, an image, a captioned clip cut from a YouTube link, or a finished narrated short. The payment is the only credential. Here is what each endpoint sells, how a purchase works, and the code to make one.

AI agents can write code, browse the web and run whole workflows on their own. Paying for things is where most of them still stop. Using a new API usually means a human creates an account, generates a key, enters a card and pastes the key into the agent's environment, once for every service the agent needs.
The x402 protocol, created by Coinbase, removes that step. It puts a real payment behind the HTTP 402 Payment Required status code, which has been reserved since 1997 without a standard way to use it. The agent asks for something, the server names a price, the agent pays in USDC, and the server delivers.
Treza sells generated media this way. An agent with a funded wallet can buy a video, a voiceover, a music track, an image, a captioned clip cut from a YouTube video, or a finished narrated short, and get the file back in the same exchange. No account, no API key, no signup.
How an x402 purchase works
- The agent sends its request. With no payment attached, the server answers
402with the price, the networks it accepts, and where to pay. - The agent's wallet signs a USDC payment for exactly that price.
- The agent sends the same request again, with the signed payment in a
Payment-Signatureheader. - The server has a facilitator verify the payment and settle it on-chain, then does the work and responds.
Any x402 client library handles steps 2 and 3, so in code a paid call looks like an ordinary fetch.
What an agent can buy from Treza
Each product has its own endpoint and its own listing on the x402 Bazaar, the index agents search to find services they can pay for.
| Endpoint | What the payment buys | Price in USDC |
|---|---|---|
POST /api/x402/video | A video clip from a prompt, or animated from a first frame, on eight models including Hailuo 3, Veo 3.1, Kling 3.0 and Seedance 2.5 | $0.42 to $4.90 |
POST /api/x402/speech | An ElevenLabs voiceover in one of 13 voices, up to 3,000 characters | $0.42 per 1,000 characters, from $0.02 |
POST /api/x402/music | An original Google Lyria 3 track: a 30-second piece or a full song | $0.06 or $0.12 |
POST /api/x402/image | An image on Nano Banana, GPT Image 2, Seedream, FLUX.2 or Recraft | $0.02 to $0.36 |
POST /api/x402/clip | The most shareable 30 to 60 seconds of a YouTube video, reframed on the speaker and captioned | $0.04 to $0.87, by the source's length |
POST /api/x402/short | A finished 30-second vertical short from a topic: script, scenes, narrator, music and captions | $0.87 on stills, $3.22 animated |
Every price is what the work costs us at the provider, times the same markup our human customers pay. The 402 always quotes the exact figure for the request you sent, so read the price from it rather than hard-coding one.
A plain GET on any of these endpoints returns its full menu for free: every model, option and price, and the fields the request accepts.
curl https://www.trezalabs.com/api/x402/videoWhat a purchase looks like
Ask without paying and you get the quote:
curl -i -X POST https://www.trezalabs.com/api/x402/speech \
-H 'Content-Type: application/json' \
-d '{"text": "Welcome back. Today we are looking at three small habits that make a big difference.", "voice": "george"}'
# HTTP/2 402
# Payment-Required: <base64: the price, once for Base and once for Solana>Pay it with an x402 client and the same request returns the file. This is the setup our example repo uses, paying from a Base wallet:
import { wrapFetchWithPaymentFromConfig } from '@x402/fetch';
import { ExactEvmScheme } from '@x402/evm';
import { privateKeyToAccount } from 'viem/accounts';
const paidFetch = wrapFetchWithPaymentFromConfig(fetch, {
schemes: [
{
network: 'eip155:8453',
client: new ExactEvmScheme(privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`)),
},
],
// The client refuses any single payment over $1 unless you raise the cap.
spendControls: { maxAmountPerPayment: '$5' },
});
const res = await paidFetch('https://www.trezalabs.com/api/x402/speech', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: 'Welcome back. Today we are looking at three small habits that make a big difference.',
voice: 'george',
}),
});
const run = await res.json();
console.log(run.status, run.audio, run.paidUsd);The paid call answers 200 straight away and holds the connection open while the work runs, then ends with one JSON object. Speech, music and images finish in seconds, so the file is in that object: audio for speech and music, image for images, video for the rest. A video comes back in the same response too when it renders within 45 seconds.
Clips and shorts take a few minutes, so their response ends with "status": "running" and a statusUrl. Polling it is free and needs no other credential. The X-Status-Url header carries the same URL from the first byte of the response, so a dropped connection never loses a paid job. Send Prefer: wait=600 to hold the connection open for longer, or "async": true to get the statusUrl at once.
Every 402 lists the price twice, once for Base and once for Solana, and the client pays with whichever wallet it has. Neither chain needs gas money in the wallet: on Base the payment is an authorization the facilitator submits, and on Solana the facilitator pays the network fee. The wallet needs USDC and nothing else.
What the payment guarantees
Paying before you see the result only makes sense if the seller cannot take money for nothing. Every Treza endpoint works the same way:
- A request we cannot fill costs nothing. An unknown field, a model or length that is not on sale, an image that cannot be fetched, or a YouTube video that cannot be clipped is refused with a
400after your payment verifies and before it settles. A field is never silently ignored and paid for. - A failed render is not charged. If the model refuses the prompt or the render fails, the payment stays as credit on an account keyed to your wallet, and the response carries a
retryUrl. POST to it, with a reworded prompt if you like, to try once more without paying again. A clip or short that fails part way charges only the steps that finished, and its retry picks up where the run stopped. - One payment buys one result. A payment presented a second time is refused with a
409, and nothing is rendered or charged. - Your wallet is your account. The first payment creates it, and the same wallet always maps to the same account. There is nothing to sign up for and nothing to log in to.
How agents find it
- The x402 Bazaar. Each endpoint is listed with a description, an input schema, an example request and a price, so an agent searching the index for text-to-speech, image generation or video finds the matching Treza listing. How to Build an AI Agent That Can Pay for Its Own APIs shows that search in code.
- The menus. A
GETon any endpoint, as above. - OpenAPI. Every x402 endpoint and its retry route is described at /.well-known/openapi.json.
- MCP. Treza's MCP server is listed in the MCP Registry, and its
get_credit_balancetool tells an agent how to top up its own balance over x402.
For agents with a Treza account
Paying per call suits a one-off job. An agent that runs pipelines regularly is better served by a Treza account with prepaid credits, and x402 covers the refill as well: POST /api/billing/credits/x402 adds $5 to the balance the account's pipeline runs draw from. Send the account's API key as the bearer and the credits land on that account, so the agent keeps itself funded without anyone opening a billing page. What Is a Video Generation MCP Server? covers how agents run pipelines, and the docs cover the top-up.
Why this matters
For agents, x402 turns any service into something they can use the moment they find it: read the price, pay it, get the result. For the people building those services, it means selling to agents without building signups, key management or billing for them.
It is also why Treza sells finished work rather than raw model calls. Any gateway can resell a model. An agent that needs a captioned clip or a narrated short needs the pipeline around the model, and over x402 it can buy that pipeline's output in one call. How the endpoints are built is written up in Selling Video Generation to Agents, for USDC and Selling Agents the Whole Job, Not the Model Call.
Get started
- x402 payments documentation: every endpoint's fields, prices and responses
- Example buyer scripts: buy any of the six from a funded Base or Solana wallet with
npm run buy,buy:speech,buy:music,buy:image,buy:cliporbuy:short - Pay-per-video walkthrough: a real purchase you can check on-chain
- x402 video generation API: the video endpoint's models and prices
- x402 protocol docs: Coinbase's protocol reference


