402.md

Build an AI Agent

Give your AI agent a USDC wallet and let it pay for services autonomously

Install

npm install @402md/client

Fund a wallet

Create an Ethereum wallet and fund it with USDC on Base. For testing, use Base Sepolia and get testnet USDC from a faucet.

Create an agent

import { createAgent } from '@402md/client'

const agent = createAgent({
  wallet: {
    privateKey: process.env.AGENT_WALLET_KEY!,
    network: 'base'
  },
  budget: {
    daily: '100.00',
    perTransaction: '50.00'
  }
})

Search for skills

const results = await agent.search('sentiment analysis')
console.log(results.skills)
// [{ name: 'sentiment-api', description: '...', pricing: { model: 'per_call', price: '0.05' }, category: 'api' }]

Call a paid API

const result = await agent.call('sentiment-api', '/analyze', {
  body: { text: 'I love this product!' }
})
// result = { sentiment: 'positive', score: 0.95 }

The SDK handles the full x402 flow automatically: makes the request, receives the 402 response, signs a USDC payment, and retries with the payment header.

Handle budget errors

import { createAgent } from '@402md/client'

try {
  await agent.call('expensive-api', '/endpoint')
} catch (err) {
  if (err.message.includes('budget')) {
    console.log('Over budget:', err.message)
  }
}

The SDK checks your budget before signing any transaction. If the payment would exceed your daily or per-transaction limit, it throws an error and no USDC is spent.

Subscribe and purchase

// Subscribe to a SaaS service
const sub = await agent.subscribe('analytics-dashboard')
// { ticket: 'jwt...', accessUrl: 'https://...', expiresAt: '...' }

// Buy a physical product
const order = await agent.purchase('widget-store', '/buy/WIDGET-001', {
  quantity: 2,
  shipping: {
    name: 'John Doe',
    address: '123 Main St',
    city: 'San Francisco',
    state: 'CA',
    zip: '94102',
    country: 'US'
  }
})
// { orderId: 'ord_...', status: 'created', txHash: '0x...' }

// Check balance
const balance = await agent.getBalance()  // '847.50'
const spent = agent.getSpentToday()       // 52.5

MCP integration

Install the MCP server to give any AI assistant 402.md capabilities:

npm install -g @402md/mcp

Add to claude_desktop_config.json:

{
  "mcpServers": {
    "402md": {
      "command": "402md-mcp",
      "env": {
        "WALLET_PRIVATE_KEY": "0xabc...",
        "NETWORK": "base"
      }
    }
  }
}

Add to .cursor/mcp.json:

{
  "mcpServers": {
    "402md": {
      "command": "402md-mcp",
      "env": {
        "WALLET_PRIVATE_KEY": "0xabc...",
        "NETWORK": "base"
      }
    }
  }
}

The MCP server exposes 6 tools: search_skills, call_api, subscribe, buy_product, check_balance, and get_orders. See the MCP Reference for details.

End-to-end example

import { createAgent } from '@402md/client'

const agent = createAgent({
  wallet: {
    privateKey: process.env.AGENT_WALLET_KEY!,
    network: 'base'
  },
  budget: { daily: '10.00', perTransaction: '1.00' }
})

// Discover
const skills = await agent.search('weather')

// Pay and call
const forecast = await agent.call('weather-api', '/forecast', {
  body: { city: 'San Francisco' }
})

// Check spend
console.log(`Spent today: $${agent.getSpentToday()}`)
console.log(`Balance: $${await agent.getBalance()}`)

x402 Wallet alternative

Don't want to manage private keys in code? The x402 Wallet is a desktop app that handles wallet management and MCP integration for you. It includes an embedded MCP server and a Claude extension — no private keys in environment variables.