@aipayagnet/fetch
v0.1.0
Published
AiPay client SDK — auto-pay x402 responses with your AI agent budget
Maintainers
Readme
@aipay/fetch
AiPay client SDK — auto-pay x402 responses with your AI agent budget.
Install
npm install @aipay/fetchQuick Start
import { AiPay } from "@aipay/fetch";
const aipay = new AiPay({
apiKey: "aipay_xxx", // Your AiPay API key
agentId: "agt_xxx", // Your agent ID (from dashboard)
walletPrivateKey: "0x...", // Your wallet private key for signing payments
});
// Fetch a paid API — handles the 402 → pay → retry flow automatically
const resp = await aipay.fetch("https://paid-api.com/v1/search?q=test");
const data = await resp.json();How It Works
- You call
aipay.fetch(url)— the SDK adds your API key and agent headers. - If the server responds with
402 Payment Required, the SDK reads the x402 requirements. - The SDK signs and broadcasts a USDC payment on the required chain (Polygon/Base/Ethereum).
- The SDK retries the request with the
X-PAYMENTheader and returns the successful response.
Risk Policies
Control how much your agent can spend per call:
// strict: max $0.25 per call
await aipay.fetch(url, { policy: "strict" });
// balanced (default): no per-call limit, respects agent budget
await aipay.fetch(url, { policy: "balanced" });
// permissive: no checks
await aipay.fetch(url, { policy: "permissive" });API
new AiPay(config)
| Parameter | Type | Description |
|---|---|---|
| config.apiKey | string | Your AiPay API key (aipay_xxx) |
| config.agentId | string? | Agent ID for budget tracking (agt_xxx) |
| config.walletPrivateKey | string? | Wallet private key for signing payments (0x...) |
| config.dashboardUrl | string? | Override dashboard URL |
| config.apiUrl | string? | Override API URL |
aipay.fetch(url, options?)
Returns a standard Promise<Response>. In addition to normal fetch options, accepts:
| Parameter | Type | Description |
|---|---|---|
| options.policy | "strict" \| "balanced" \| "permissive" | Risk policy for spending |
License
MIT © AiPay
