DEV Community

Pay-per-request APIs from Python: skip the API key, pay with USDC instead

API keys are friction. Signup, billing portal, monthly minimums, credential rotation. For agents and scripts that call APIs occasionally, there's a better model: pay per call, no account needed.

The x402 protocol does this at the HTTP level. Your script gets a 402 Payment Required response, pays automatically, and retries. Here's how it works from Python.

Install

pip install "x402[httpx,evm]"
Enter fullscreen mode Exit fullscreen mode

Call a pay-per-request API

import asyncio
import httpx
from x402 import x402Client
from x402.http.clients.httpx import x402AsyncTransport
from x402.mechanisms.evm.exact import ExactEvmScheme
from x402.mechanisms.evm import EthAccountSigner
from eth_account import Account

# Wire up your wallet once
account = Account.from_key("0xYOUR_PRIVATE_KEY")
signer = EthAccountSigner(account)
wallet = x402Client()
wallet.register("eip155:*", ExactEvmScheme(signer=signer))

async def search(query: str):
    transport = x402AsyncTransport(wallet)
    async with httpx.AsyncClient(transport=transport) as client:
        r = await client.get(
            f"https://socialintel.dev/v1/search?query={query}&country=US"
        )
        return r.json()

results = asyncio.run(search("fitness influencers"))
print(results)
Enter fullscreen mode Exit fullscreen mode

The x402AsyncTransport intercepts the 402, creates a signed EIP-3009 USDC authorization, retries the request with the payment header. No signup. No account. $0.10 USDC per call, deducted directly from your wallet.

Test without spending USDC first

Most x402 APIs offer a free demo mode. Social Intel does:

curl "https://socialintel.dev/v1/search?query=fitness&country=US&demo=true"
Enter fullscreen mode Exit fullscreen mode

Returns 3 results, no payment required. Use this to verify the API returns what you need before putting real money on it.

Get USDC on Base

  • Coinbase → buy USDC → withdraw to Base network (direct Base withdrawal supported)
  • Or bridge from any EVM chain via bridge.base.org
  • Minimum balance: $1 covers 10 requests

What the 402 response looks like

When you call without a wallet, you get:

{
  "x402Version": 2,
  "error": "Payment required",
  "accepts": [{
    "scheme": "exact",
    "network": "eip155:8453",
    "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
    "amount": "100000",
    "payTo": "0xB1Acd9E0269023546074400A434e703B646AaBBa"
  }]
}
Enter fullscreen mode Exit fullscreen mode

amount is in USDC's 6-decimal units, so 100000 = $0.10. The x402 client reads this and pays before you ever see it.

Zero-wallet alternative

Don't have USDC set up? AgentCash wraps the whole thing:

npx agentcash@latest fetch "https://socialintel.dev/v1/search?query=fitness&country=US"
Enter fullscreen mode Exit fullscreen mode

It manages a wallet for you, funded via their dashboard. Useful for quick tests.

Why this matters for agents

AI agents make thousands of API calls. API keys mean:

  • Credentials that can be stolen or rotated
  • Monthly billing even during quiet periods
  • Rate limits tied to account tier, not actual usage

x402 lets an agent carry a wallet and pay only for what it uses. Each request is a transaction on-chain — auditable, reversible if the API misbehaves, and composable with other on-chain logic.

The Social Intel API is one example. Search Instagram influencers by niche, follower count, country, gender — $0.10 per query, no account:

GET https://socialintel.dev/v1/search?query=travel&country=US&min_followers=10000
Enter fullscreen mode Exit fullscreen mode

Try the free demo: https://socialintel.dev/v1/search?query=fitness&demo=true

Top comments (0)