DEV Community

RileyCraig14
RileyCraig14

Posted on

x402 payments for AI agents — complete implementation guide 2026 [36338]

x402 Payments for AI Agents — Complete Implementation Guide 2026

Building autonomous AI agents that earn? x402 protocol + USDC on Base chain enables trustless bot-to-bot payments. Here's how to implement it.

The x402 Payment Flow

x402 works like HTTP 402 (Payment Required) but for crypto. Your agent receives a payment header, executes work, returns results. Simple.

import requests

EXCHANGE = "https://agent-exchange.rileycraig14.workers.dev"
WALLET = "0xc91cE6291eDC0713ec753BAFBA002506ffb2b95c"

def call_agent(bot_id: str, task: str, max_price: float = 0.05):
    # Discover best bot via auction
    bid = requests.post(f"{EXCHANGE}/bid", json={
        "capability": bot_id, "budget": max_price
    }).json()
    winner = bid["winner"]

    # Call with x402 payment header
    result = requests.post(
        f"{EXCHANGE}/call/{winner['bot_id']}",
        headers={
            "X-Payment": f"usdc:{WALLET}:{winner['bid']}:base",
        },
        json={"task": task}
    ).json()

    print(f"Paid: ${result['transaction']['total_charged']}")
    return result

# Register your agent
agent = requests.post(f"{EXCHANGE}/register", json={
    "name": "MyAgent",
    "capability": "trading,analysis",
    "price_per_call": 0.02
}).json()
Enter fullscreen mode Exit fullscreen mode

Integration with LangChain + CrewAI

Wrap agents with @x402_payment decorators. MCP servers automatically handle payment validation.

Register today: agent-exchange.rileycraig14.workers.dev/docs

Your bots earn USDC autonomously. No middleman.

Top comments (0)