Skip to main content

What is x402?

x402 is an open standard introduced by Coinbase. It uses the HTTP 402 Payment Required status code to allow machines (such as AI Agents) to automatically complete on-chain micropayments when initiating API requests. Payments are made using stablecoins (like USDC), with instant, transparent settlement and no need for trusted intermediaries.

Integration Steps (Python Example)

Install Dependencies

pip install eth_account x402

Prepare Your Private Key

Please ensure you have an Ethereum wallet with sufficient USDC (it is recommended to use a testnet or mainnet cold wallet). Never expose your real private key in public code!
from eth_account import Account

# ⚠️ For production, please read from environment variables or a key management service
PRIVATE_KEY = "your_0x_private_key_here"  # Example: 0x123...abc
account = Account.from_key(PRIVATE_KEY)

Initialize x402 Session

from x402.clients.requests import x402_requests

# Create an HTTP session that supports x402
session = x402_requests(account)

Call Chainbase API

For all Chainbase WebAPI endpoints that support x402, simply add x-api-key: x402 to the request header. The rest of the usage is consistent with the standard API.
import requests
from pprint import pprint

# Example: Query Ethereum Mainnet USDT holder addresses (payment required)
api_url = "https://api.chainbase.com/v1/token/holders?chain_id=1&contract_address=0xdac17f958d2ee523a2206206994597c13d831ec7"

headers = {
    "x-api-key": "x402",  # Key! Enable x402 payment
    "Accept": "application/json"
}

response = session.get(api_url, headers=headers)

# Example of a successful response
if response.status_code == 200:
    pprint(response.json())
else:
    print(f"Payment failed or request error: {response.status_code} - {response.text}")

Actual Application Scenarios

  • AI Agent automatically monitors token address changes: Query once per hour, pay per request, no monthly fee required.
  • One-time data fetching: Analyze a specific Token project, call only once, and pay just a few cents.
  • Experimental development: No need to register an enterprise account, pay directly with a wallet to test interfaces.

Next Steps

Chainbase Web3API already supports x402 protocol access. For more Web3API interfaces, please check: https://docs.chainbase.com/api-reference/overview