|
| 1 | +--- |
| 2 | +title: 'Agent Payments (x402)' |
| 3 | +description: 'Store files autonomously and pay with USDC on Base — no human intervention required' |
| 4 | +--- |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +`@toju.network/x402` implements the [x402 protocol](https://x402.org) for autonomous storage payments. An agent instantiates `AgentClient` with a funded Base wallet, calls `store()`, and the SDK handles the full payment negotiation automatically — no wallet prompts, no manual signing steps. |
| 9 | + |
| 10 | +The flow under the hood: |
| 11 | + |
| 12 | +<Steps> |
| 13 | + <Step title="Send request"> |
| 14 | + Agent sends `POST /upload/agent` with the file and storage parameters |
| 15 | + </Step> |
| 16 | + <Step title="Receive 402"> |
| 17 | + Server responds with `402 Payment Required` and a price quote in USDC |
| 18 | + </Step> |
| 19 | + <Step title="Sign off-chain"> |
| 20 | + SDK signs an EIP-3009 authorization (no on-chain transaction yet) |
| 21 | + </Step> |
| 22 | + <Step title="Retry with payment"> |
| 23 | + SDK retries the request with the signed `X-PAYMENT` header attached |
| 24 | + </Step> |
| 25 | + <Step title="Facilitator settles"> |
| 26 | + Coinbase's public facilitator verifies and settles the USDC transfer on Base |
| 27 | + </Step> |
| 28 | + <Step title="File stored"> |
| 29 | + Server uploads to IPFS via Storacha and returns the CID |
| 30 | + </Step> |
| 31 | +</Steps> |
| 32 | + |
| 33 | +## Install |
| 34 | + |
| 35 | +```shell |
| 36 | +pnpm add @toju.network/x402 |
| 37 | +``` |
| 38 | + |
| 39 | +## Quick start |
| 40 | + |
| 41 | +```ts |
| 42 | +import { createAgentClient } from '@toju.network/x402' |
| 43 | + |
| 44 | +const client = createAgentClient({ |
| 45 | + privateKey: process.env.AGENT_PRIVATE_KEY as `0x${string}`, |
| 46 | + environment: 'mainnet', |
| 47 | +}) |
| 48 | + |
| 49 | +const file = new File([fileBuffer], 'report.pdf', { type: 'application/pdf' }) |
| 50 | +const result = await client.store(file, { durationDays: 30 }) |
| 51 | + |
| 52 | +console.log(result.cid) // bafy... |
| 53 | +console.log(result.expiresAt) // 2025-06-01T00:00:00.000Z |
| 54 | +``` |
| 55 | + |
| 56 | +Your agent's wallet needs USDC on Base to pay. At our rate of 3×10⁻¹² USD/byte/day, storing 1 MB for 30 days costs about **$0.0001**. |
| 57 | + |
| 58 | +## createAgentClient |
| 59 | + |
| 60 | +```ts |
| 61 | +import { createAgentClient } from '@toju.network/x402' |
| 62 | + |
| 63 | +const client = createAgentClient(options) |
| 64 | +``` |
| 65 | + |
| 66 | +### Options |
| 67 | + |
| 68 | +<ParamField path="privateKey" type="`0x${string}`" required> |
| 69 | + EVM private key for the agent's wallet. Must hold USDC on Base to pay for storage. |
| 70 | +</ParamField> |
| 71 | + |
| 72 | +<ParamField path="environment" type="'mainnet'" required> |
| 73 | + Target environment. Use `'mainnet'` for Base Mainnet with real USDC. |
| 74 | +</ParamField> |
| 75 | + |
| 76 | +## estimateStorageCost |
| 77 | + |
| 78 | +Check the cost before uploading. |
| 79 | + |
| 80 | +```ts |
| 81 | +const estimate = await client.estimateStorageCost( |
| 82 | + 1_000_000, // sizeInBytes |
| 83 | + 30 // durationDays |
| 84 | +) |
| 85 | + |
| 86 | +console.log(estimate.usdc) // '0.000090' (USDC, 6 decimal places) |
| 87 | +console.log(estimate.usd) // '0.00' |
| 88 | +``` |
| 89 | + |
| 90 | +### Parameters |
| 91 | + |
| 92 | +<ParamField path="sizeInBytes" type="number" required> |
| 93 | + Raw byte count of the file |
| 94 | +</ParamField> |
| 95 | + |
| 96 | +<ParamField path="durationDays" type="number" required> |
| 97 | + How long to keep the file on IPFS |
| 98 | +</ParamField> |
| 99 | + |
| 100 | +### Response |
| 101 | + |
| 102 | +<ResponseField name="usdc" type="string"> |
| 103 | + Cost in USDC, formatted to 6 decimal places (e.g. `'0.000090'`) |
| 104 | +</ResponseField> |
| 105 | + |
| 106 | +<ResponseField name="usd" type="string"> |
| 107 | + Approximate USD cost, formatted to 2 decimal places |
| 108 | +</ResponseField> |
| 109 | + |
| 110 | +## store |
| 111 | + |
| 112 | +Upload a file and pay autonomously via x402. |
| 113 | + |
| 114 | +```ts |
| 115 | +const result = await client.store(file, { durationDays: 30 }) |
| 116 | +``` |
| 117 | + |
| 118 | +### Parameters |
| 119 | + |
| 120 | +<ParamField path="file" type="File" required> |
| 121 | + The file to upload. Use the standard Web API `File` object — works in Node.js 20+ and all modern runtimes. |
| 122 | +</ParamField> |
| 123 | + |
| 124 | +<ParamField path="options.durationDays" type="number" required> |
| 125 | + Storage duration in days |
| 126 | +</ParamField> |
| 127 | + |
| 128 | +### Response |
| 129 | + |
| 130 | +<ResponseField name="cid" type="string"> |
| 131 | + IPFS content identifier for the uploaded file (e.g. `bafybei...`) |
| 132 | +</ResponseField> |
| 133 | + |
| 134 | +<ResponseField name="expiresAt" type="string"> |
| 135 | + ISO 8601 date string when the file will be removed from IPFS |
| 136 | +</ResponseField> |
| 137 | + |
| 138 | +<ResponseField name="fileName" type="string"> |
| 139 | + Original file name |
| 140 | +</ResponseField> |
| 141 | + |
| 142 | +<ResponseField name="fileSize" type="number"> |
| 143 | + File size in bytes |
| 144 | +</ResponseField> |
| 145 | + |
| 146 | +## USDC on Base |
| 147 | + |
| 148 | +USDC uses 6 decimal places on all EVM chains (Circle standard). The SDK and server handle conversion automatically. |
| 149 | + |
| 150 | +| Network | USDC Contract | |
| 151 | +|---|---| |
| 152 | +| Base Mainnet | `0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913` | |
| 153 | + |
| 154 | +<Info> |
| 155 | + Your agent wallet needs enough USDC to cover the storage cost, plus a small amount of ETH on Base for gas. Gas fees on Base are typically under $0.001 per transaction. |
| 156 | +</Info> |
| 157 | + |
| 158 | +## Framework integrations |
| 159 | + |
| 160 | +`AgentClient` works with any framework that can hold an EVM private key: |
| 161 | + |
| 162 | +```ts |
| 163 | +// LangChain tool |
| 164 | +import { DynamicTool } from 'langchain/tools' |
| 165 | + |
| 166 | +const storeTool = new DynamicTool({ |
| 167 | + name: 'store_file', |
| 168 | + description: 'Store a file on IPFS and return its CID', |
| 169 | + func: async (filePath: string) => { |
| 170 | + const buffer = await fs.readFile(filePath) |
| 171 | + const file = new File([buffer], path.basename(filePath)) |
| 172 | + const result = await client.store(file, { durationDays: 30 }) |
| 173 | + return result.cid |
| 174 | + }, |
| 175 | +}) |
| 176 | +``` |
| 177 | + |
| 178 | +```ts |
| 179 | +// CrewAI-style (TypeScript) |
| 180 | +const storeAction = async (input: { filePath: string; days: number }) => { |
| 181 | + const buffer = await fs.readFile(input.filePath) |
| 182 | + const file = new File([buffer], path.basename(input.filePath)) |
| 183 | + return client.store(file, { durationDays: input.days }) |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +## Related |
| 188 | + |
| 189 | +<CardGroup cols={2}> |
| 190 | + <Card title="Pricing" icon="coins" href="/pricing"> |
| 191 | + Storage rate and cost calculator |
| 192 | + </Card> |
| 193 | + <Card title="Upload (SOL)" icon="arrow-up" href="/sdk/deposit"> |
| 194 | + Human wallet uploads with Solana |
| 195 | + </Card> |
| 196 | + <Card title="CID Computation" icon="hashtag" href="/concepts/cid-computation"> |
| 197 | + How content identifiers work |
| 198 | + </Card> |
| 199 | + <Card title="Storage Payments" icon="credit-card" href="/concepts/storage-payments"> |
| 200 | + Payment mechanics |
| 201 | + </Card> |
| 202 | +</CardGroup> |
0 commit comments