Skip to main content

How x402 Makes AI APIs Pay-Per-Use

March 15, 2026 · 6 min read

HTTP has had a 402 Payment Required status code since 1999. For 27 years, it was "reserved for future use." In 2025, the x402 protocol finally gave it a job: letting clients pay for API calls inline, with no subscription, no billing portal, and no human in the loop.

AiPayGen is built on x402. Here's how it works and why it matters for AI agents.

The subscription problem

Traditional API billing works like a gym membership. You pay monthly whether you use it or not. For predictable workloads, that's fine. For AI agents? It breaks down fast.

What agents need is vending-machine economics: put in a coin, get a result. That's exactly what x402 provides.

What x402 is

x402 is a protocol that embeds payment into HTTP requests. The payment is a cryptographic signature proving you've authorized a USDC transfer. It travels with the request header, so the API can verify payment and return the result in a single round trip.

Client sends request
Server returns 402 + price
Client signs payment
Client resends with X-PAYMENT header
Server verifies & responds 200

The 402 response

When you call an AiPayGen endpoint without an API key or x402 payment, you get a 402 response with pricing details:

HTTP/1.1 402 Payment Required

{
  "error": "Payment required",
  "x402": {
    "version": 2,
    "networks": [
      {
        "network": "eip155:8453",
        "asset": "USDC",
        "address": "0x366D488a48de1B2773F3a21F1A6972715056Cb30",
        "amount": "6000",
        "decimals": 6
      },
      {
        "network": "solana:mainnet",
        "asset": "USDC",
        "amount": "6000",
        "decimals": 6
      },
      {
        "network": "stellar:pubnet",
        "asset": "USDC",
        "amount": "6000",
        "decimals": 6
      }
    ]
  },
  "unlock": "Add x-api-key header or pay via x402 (USDC on Base, Solana, or Stellar)"
}

The amount: 6000 with decimals: 6 means $0.006 USDC. Three networks are supported: Base (Ethereum L2), Solana, and Stellar.

The payment header

The client signs a USDC transfer authorization and attaches it as an X-PAYMENT header. The server verifies the signature on-chain before processing the request.

# Using the x402 SDK (Python)
pip install x402

# In your code:
from x402.client import create_x402_client

client = create_x402_client(
    private_key="0x_your_wallet_private_key",
    network="base"
)

# The SDK handles 402 → sign → retry automatically
response = client.post(
    "https://api.aipaygen.com/summarize",
    json={"text": "Long article text...", "length": "short"}
)
print(response.json())

That's it. No API key, no account creation, no billing portal. The wallet signs, the server verifies, the result comes back.

How AiPayGen implements x402

Every paid endpoint in AiPayGen supports three payment methods:

  1. Free tier (10 calls/day) — no key, no payment, just call the endpoint
  2. API key — prepaid balance via Stripe, deducted per call
  3. x402 — pay per call in USDC, no account needed

The middleware checks in order: API key → x402 payment → free tier. If you have a key with balance, that's used first. If you send an x402 payment, that's verified and accepted. If neither, the free tier counter is checked.

# Simplified middleware flow (actual code in app.py)
def payment_middleware():
    # 1. Check API key
    key = request.headers.get("x-api-key")
    if key and validate_key(key):
        deduct_balance(key, cost)
        return proceed()

    # 2. Check x402 payment
    payment = request.headers.get("X-PAYMENT")
    if payment:
        if verify_x402(payment, cost):
            return proceed()
        return error("Invalid x402 payment"), 402

    # 3. Check free tier
    if free_calls_remaining(request) > 0:
        decrement_free_calls(request)
        return proceed()

    # 4. Return 402 with pricing
    return x402_pricing_response(), 402

Why this matters for AI agents

The key insight: AI agents can hold wallets. You give an agent a private key with a USDC balance, and it can autonomously pay for any x402-enabled API. No OAuth, no API key management, no billing dashboard.

This enables a new pattern:

Real-world example: An autonomous research agent receives a task. It calls AiPayGen's /research endpoint ($0.006), then /extract ($0.006), then stores results in /memory/store ($0.002). Total cost: $0.014. No subscription. No human approval. The agent's wallet handles everything.

x402 V2: Multi-chain support

AiPayGen uses x402 V2, which adds support for multiple blockchain networks in a single 402 response. The client picks whichever network has the cheapest gas or where it holds funds:

All three networks settle in USDC, so the API price is the same regardless of which chain you use.

Try it yourself

You don't need crypto to start. The free tier (10 calls/day) and API key ($1 via Stripe) both work without a wallet. But if you're building autonomous agents that need to pay for tools, x402 is the cleanest path.

# No payment needed — free tier
curl -X POST "https://api.aipaygen.com/summarize" \
  -H "Content-Type: application/json" \
  -d '{"text": "Your text here", "length": "short"}'

# API key (prepaid Stripe balance)
curl -X POST "https://api.aipaygen.com/summarize" \
  -H "x-api-key: apk_your_key" \
  -H "Content-Type: application/json" \
  -d '{"text": "Your text here", "length": "short"}'

# x402 (USDC on Base) — using the x402 Python SDK
python -c "
from x402.client import create_x402_client
c = create_x402_client(private_key='0x...', network='base')
r = c.post('https://api.aipaygen.com/summarize',
    json={'text': 'Your text here', 'length': 'short'})
print(r.json())
"

Further reading

Try Free — No Signup Get API Key ($1) View Pricing

Published March 15, 2026 · All posts · RSS feed