Skip to main content
Glama

agent-x402

x402 micropayments for AI agents — pay per API call in USDC on Base. No account, no API key, no subscription. With a spend policy a prompt injection cannot talk its way past.

CI npm license node paid

Docs · Contributing · Security · Issues · Discussions


The problem

x402 turns a 402 Payment Required into a signed USDC transfer. That means an agent can buy inference, data and compute without an account — but it also means an agent can spend money. A model that can sign payments is one prompt injection away from draining a wallet.

agent-x402 is the missing layer: a client that pays, and a policy that decides whether it may.

Related MCP server: token4u-mcp

Install

# tarball из релиза: без токена и без npm-логина
npm i -g https://github.com/rustemmustafingovua-byte/agent-x402/releases/download/v0.1.0/agent-x402-0.1.0.tgz
npm i -g @rustemmustafingovua-byte/agent-x402 --registry=https://npm.pkg.github.com
import { X402Client, SpendPolicy, createAgentWallet } from "agent-x402"

const wallet = createAgentWallet()          // or load an existing key
const policy = new SpendPolicy({
  dailyUsd: 5,
  perRequestUsd: 0.5,
  allowPayees: ["0x2670b922ef37c7df47158725c0cc407b5382293f"],
})

const client = new X402Client({ privateKey: wallet.privateKey, policy })

const res = await client.fetch("https://api.venice.ai/api/v1/chat/completions", {
  method: "POST",
  body: { model: "claude-haiku-5-5", messages: [{ role: "user", content: "hi" }] },
})

console.log(res.paid, res.amountUsd, res.body)

What the policy enforces

Rules live in code, outside the model:

Rule

Example

daily cap

dailyUsd: 5 — at most $5 per UTC day

per-request cap

perRequestUsd: 0.5 — one call may never exceed $0.50

payment count

maxPayments: 100

allow-list

only these contract addresses may be paid

block-list

these assets are never paid (sandbox tokens, honeypots)

per-network cap

perProviderUsd: { "eip155:8453": 2 }

policy.check() runs before signing; policy.commit() records what was actually spent. The day counter resets on the UTC day boundary.

How the payment flows

sequenceDiagram
  participant A as Agent
  participant S as Service (402)
  participant W as Wallet (USDC, Base)
  A->>S: GET /v1/chat/completions
  S-->>A: 402 + payment-required (accepts, 5 USDC)
  A->>A: policy.check() — caps, allow-list
  A->>W: balanceOf(address)
  W-->>A: 12.40 USDC
  A->>A: sign EIP-3009 transferWithAuthorization
  A->>S: retry + SIGN-IN-WITH-X
  S-->>A: 200 answer
  A->>A: policy.commit() — record the spend

Under the hood

  1. GET/POST the endpoint → 402 with a payment-required header (base64 JSON)

  2. pick an accepted option (eip155:8453 preferred, Solana supported)

  3. policy.check() → refuse if it breaks the rules

  4. check the USDC balance on Base

  5. sign an EIP-3009 transferWithAuthorization (USDC, exact scheme, 15-minute window)

  6. retry with the signed payment in SIGN-IN-WITH-X / X-PAYMENT / PAYMENT-SIGNATURE

Services that accept x402 today

Service

What

Price

api.venice.ai/api/v1

Claude Haiku 5.5 / Sonnet 5.5 / Opus 5.5

from $0.10 / 1M in

pool.linkrra.com/v1

open-weight models on their own GPUs, no key

from $0.001 / call

MCP server — wallet operations as tools

The private key stays inside the process; the policy is enforced before the client signs anything.

// opencode.json
{
  "mcp": {
    "agent-x402": { "type": "local", "command": ["ax402-mcp"], "enabled": true }
  }
}

Tool

What it does

x402-wallet

address + USDC balance (never the key)

x402-quote

what would this call cost — pays nothing

x402-pay

call + pay, policy enforced before signing

x402-policy

show or update caps, allow-list, blocked contracts

CLI

ax402 new                # create an agent wallet
ax402 balance            # USDC balance on Base
ax402 pay <url> [json]   # call any x402 endpoint, paying automatically
ax402 quote <url>        # show what a call would cost, pay nothing
ax402 policy             # show the current spend report

Part of a portfolio

Project

What it does

opencode-free-stack

route to free models, fall back automatically

free-web-mcp

web search and page reading for agents, zero credits

agent-x402

pay per API call in USDC with a spend policy

mcp-doctor

check that MCP servers work, and say why they do not

mcp-proxy

one endpoint, many MCP servers, with keys and limits

tool-schema-lint

lint MCP tool schemas before agents misuse them

prompt-budget

what does your system prompt cost per request?

agent-cost-guard

a budget in front of any OpenAI-compatible endpoint

opencode-doctor

audit an OpenCode install in one command

free-tier-tracker

know your free quota before it runs out

x402-priceboard

the price list of x402 endpoints, without paying

prompt-injection-guard

find instructions hidden in files the agent reads itself

readme-doctor

why nobody uses your repository — checked in one command

Donate

MIT, forever. USDC on Base:

0x8aC6a229f254980CCd3Dd35CF5E1B7c08880B40E

Roadmap

  • x402 v2 "exact" scheme on Base (EIP-3009)

  • spend policy: caps, allow-list, block-list, per-network

  • CLI and tests

  • MCP server exposing wallet and policy operations

  • policy file (~/.config/agent-x402/policy.json)

  • Solana USDC (SPL) signing

  • spend report exporter (CSV/JSON) for accounting

  • hosted relay with a take rate

License

MIT © rustemmustafingovua-byte

Related MCP Connectors

Related MCP Servers