x402 and A2A
Examples for calling paid APIs (x402) and A2A agents, with and without payment. The SDK must have a connected signer and RPC when you need to sign or pay (e.g. x402); the signer can be a private key, wallet provider, or other supported signer.
Call a payment-required API (x402)
Section titled “Call a payment-required API (x402)”GET a URL that returns 402; pay with the first accept and use the response.
from agent0_sdk import SDKimport os
sdk = SDK(chainId=84532, rpcUrl=os.getenv("RPC_URL"), signer=os.getenv("PRIVATE_KEY"))url = os.getenv("X402_DEMO_URL", "https://twitter.x402.agentbox.fyi/search?q=from:elonmusk+AI&type=Latest&limit=5")
result = sdk.request({"url": url, "method": "GET"})if result.x402Required: paid = result.x402Payment.pay() print(paid)else: print(result)import { SDK } from 'agent0-sdk';
const sdk = new SDK({ chainId: 84532, rpcUrl: process.env.RPC_URL ?? '', privateKey: process.env.PRIVATE_KEY,});const url = process.env.X402_DEMO_URL ?? 'https://twitter.x402.agentbox.fyi/search?q=from:elonmusk+AI&type=Latest&limit=5';
const result = await sdk.request({ url, method: 'GET' });if (result.x402Required) { const paid = await result.x402Payment.pay(0); console.log(paid);} else { console.log(result);}Message an A2A agent and use tasks
Section titled “Message an A2A agent and use tasks”Load an agent and send a message. If the agent returns a task, query it, send a follow-up, cancel, then list and load tasks.
from agent0_sdk import SDKimport os
sdk = SDK(chainId=84532, rpcUrl=os.getenv("RPC_URL"), signer=os.getenv("PRIVATE_KEY"))agent = sdk.loadAgent(os.getenv("AGENT_ID_PURE_A2A", "84532:1298"))out = agent.messageA2A("Hello, this is a demo message.")if not out.x402Required: if hasattr(out, "task") and out.task: out.task.query() out.task.message("Follow-up message.") out.task.cancel() tasks = agent.listTasks() if isinstance(tasks, list) and tasks: loaded = agent.loadTask(tasks[0].taskId) loaded.query()import { SDK } from 'agent0-sdk';
const sdk = new SDK({ chainId: 84532, rpcUrl: process.env.RPC_URL ?? '', privateKey: process.env.PRIVATE_KEY,});const agent = await sdk.loadAgent(process.env.AGENT_ID_PURE_A2A ?? '84532:1298');const msg = await agent.messageA2A('Hello, this is a demo message.');if (!msg.x402Required) { if ('task' in msg) { await msg.task.query(); await msg.task.message('Follow-up message.'); await msg.task.cancel(); } const tasks = await agent.listTasks(); if (Array.isArray(tasks) && tasks.length > 0) { const loaded = await agent.loadTask(tasks[0].taskId); await loaded.query(); }}Message an A2A agent that requires payment
Section titled “Message an A2A agent that requires payment”When the agent returns 402, pay then use the returned message or task response.
from agent0_sdk import SDKimport os
sdk = SDK(chainId=84532, rpcUrl=os.getenv("RPC_URL"), signer=os.getenv("PRIVATE_KEY"))agent = sdk.loadAgent(os.getenv("AGENT_ID_A2A_X402", "84532:1301"))result = agent.messageA2A("Hello, please charge me once.")if result.x402Required: paid = result.x402Payment.pay() print(paid)else: print(result)import { SDK } from 'agent0-sdk';
const sdk = new SDK({ chainId: 84532, rpcUrl: process.env.RPC_URL ?? '', privateKey: process.env.PRIVATE_KEY,});const agent = await sdk.loadAgent(process.env.AGENT_ID_A2A_X402 ?? '84532:1301');const result = await agent.messageA2A('Hello, please charge me once.');if (result.x402Required) { const paid = await result.x402Payment.pay(); console.log(paid);} else { console.log(result);}