🔗

Enable x402 on Your Agent

A step-by-step guide to adding x402 micropayment support to any AI agent. Once enabled, your agent can pay for 4Bots services — and any x402 endpoint — autonomously.

x402 Protocol USDC on Base Python + JS ~200 lines of code

What is x402?

x402 is the HTTP-native micropayment protocol. It turns any HTTP endpoint into a pay-per-use API. No accounts. No API keys. No subscriptions. Just a wallet and an HTTP client.

How x402 works:

Agent sends request
GET /v1/data
Server returns 402
Price: $0.002 USDC
Agent pays USDC
On Base via x402
Server returns 200
Content + receipt

Why agents should use x402.

Traditional API pricing doesn't work for agents. You don't want to pre-pay $20/month for an API your agent might use 3 times. x402 lets you pay exactly for what you use.

Traditional API x402 Micropayments
Pricing model Monthly subscription Pay per request
Minimum cost $5–$20/month $0.001/request
Signup required Yes — email, credit card No — just a wallet
Agent-friendly No — humans must sign up Yes — fully autonomous
Overage handling Rate limits or overage fees Pay as you go, no surprises

5 steps to x402 on your agent.

Here's how to add x402 support to any agent that can make HTTP requests. The pattern is the same in any language.

1

Create a wallet

Your agent needs an EVM wallet (Ethereum-compatible). This can be a simple keypair — no KYC, no exchange account needed.

# Python
from eth_account import Account
agent_wallet = Account.create()
print(agent_wallet.address)    # 0x...
print(agent_wallet.key.hex()) # 0x...

Fund it with a small amount of USDC on Base. You can buy USDC on Coinbase and withdraw to Base network, or use a faucet on Base Sepolia for testing.

2

Make the initial request

Send a normal HTTP request to any 4Bots endpoint. Don't include any payment — just call the API.

response = requests.get("https://status.4bots.ai/v1/status")
# Status: 402 Payment Required (if over free tier)
# OR Status: 200 (if within free tier)
3

Parse the 402 response

If you get a 402, the response body contains the payment requirements: price, wallet address, and network details.

{
  "x402": {
    "price": "$0.002",
    "asset": "USDC",
    "network": "base",
    "payTo": "0x03aEC72419Fc4f082f5D39D88DB80162511E7ee1",
    "scheme": "exact"
  }
}
4

Sign and send the payment

Sign an EIP-3009 transferWithAuthorization message and POST it to the endpoint.

# Using the x402 Python library
from x402_http import Client
from eth_account import Account

wallet = Account.from_key("0x...")
client = Client(wallet, network="base")

# Library handles payment automatically
response = client.get(
    "https://whois.4bots.ai/v1/whois/example.com"
)

print(response.json())

The library automatically:

  • Detects 402 responses and parses payment requirements
  • Builds EIP-3009 authorization message
  • Signs with your wallet
  • Retries the request with payment header
5

Use the response

Once paid, the server returns 200 with the requested data. The response includes a settlement receipt you can use for accounting.

{
  "domain": "example.com",
  "whois": { ... },
  "dns": { ... },
  "ssl": { ... },
  "_settlement": {
    "tx_hash": "0x501a12dc...",
    "amount": "0.002",
    "asset": "USDC"
  }
}

Copy-paste ready.

Full working examples in Python and JavaScript. Both use the official x402 libraries.

🐍 Python

Using the x402-http pip package.

pip install x402-http eth-account

from x402_http import Client
from eth_account import Account

# Create or load wallet
wallet = Account.from_key("0x...")

# Make paid request (library handles x402 flow automatically)
client = Client(wallet, network="base")
resp = client.get(
    "https://whois.4bots.ai/v1/whois/google.com"
)
print(resp.json())

📦 JavaScript

Using the x402-http npm package.

npm install x402-http viem

import { Client } from "x402-http";
import { privateKeyToAccount } from "viem/accounts";

const wallet = privateKeyToAccount("0x...");
const client = new Client({ wallet, network: "base" });

const resp = await client.get(
  "https://fx.4bots.ai/v1/rates/USD"
);
console.log(await resp.json());

Test for free on Base Sepolia.

All 4Bots endpoints support Base Sepolia testnet. Get free testnet USDC from the Coinbase CDP faucet and test your integration without spending real money.

# 1. Get testnet USDC from:
#    https://docs.cdp.coinbase.com/faucets

# 2. Point your wallet to Base Sepolia
#    Network: eip155:84532
#    USDC: 0x036CbD53842c5426634e7929541eC2318f3dCF7e

# 3. Test any endpoint
curl "https://whois.4bots.ai/v1/whois/example.com"
# Returns 402 with testnet payment requirements

# 4. Pay with testnet USDC → get your data

Start building.

Pick a product, send a request, and enable x402 on your agent.

STATUS — Free to start → Alternative: Use MCP

Don't want to handle payments?

If you're already using an MCP client (Hermes, Claude Code, Cline, etc.), you can integrate with 4Bots through MCP instead of implementing x402 directly. The MCP layer handles the complexity.

🔌 MCP Integration

Add any 4Bots product to your MCP client. The client discovers tools, invokes them, and receives structured results. Some products have free tiers that don't require payment at all.

# With Hermes
hermes mcp add 4bots-status --url https://status.4bots.ai/mcp

# With Claude Code
npx -y @modelcontextprotocol/cli add-server 4bots-whois --url https://whois.4bots.ai/mcp

# Manual: Point any MCP client at
https://{product}.4bots.ai/.well-known/mcp.json

Benefits: No wallet setup, no payment handling. Works with existing MCP infrastructure. Free tiers available on most products.