Skip to main content
description: “Accept USDC payments via x402 protocol”

x402 Payments

Accept USDC cryptocurrency payments using the x402 protocol on Base network.

Overview

x402 is a payment protocol that enables HTTP requests to require payment. Agents can pay for API access programmatically.

Setup

Environment Variables

# x402 Configuration
X402_PAY_TO=0xd8fd0e1dce89beaab924ac68098ddb17613db56f
X402_FACILITATOR_URL=https://x402.org/facilitator

Supported Networks

NetworkChain IDStatus
Base Mainnet8453
Base Sepolia84532✅ (Testnet)

Creating Paid Endpoints

Step 1: Import x402

import { getX402Server, x402Config } from "@/lib/x402";

Step 2: Define Payment Requirements

const paymentRequirements = {
  accepts: {
    scheme: "exact",
    price: "$0.001",
    network: "eip155:8453",
    payTo: x402Config.payTo,
  },
  description: "Premium API endpoint",
  mimeType: "application/json",
};

Step 3: Check for Payment

export async function GET(req: NextRequest) {
  const server = getX402Server();
  
  const authHeader = req.headers.get("x-payments");
  if (!authHeader) {
    return new NextResponse(
      JSON.stringify({ 
        error: "Payment required",
        payment: paymentRequirements 
      }), 
      { status: 402 }
    );
  }

  // Verify and process payment
  // Return data
  return Response.json({ data: "Hello, paid user!" });
}

Agent Payment Flow

Agents can make paid API calls:
// Agent makes a paid request
const response = await fetch('https://api.example.com/paid-endpoint', {
  headers: {
    'x-payments': paymentHeader // Automatically handled by x402 SDK
  }
});

if (response.status === 402) {
  // Payment required - x402 SDK handles payment automatically
  const data = await response.json();
  // Retry with payment
}

Pricing Examples

ActionPrice
Basic API call$0.001
AI generation$0.01
File upload$0.05
Video generation$1.00

Wallet Setup

Users need a wallet with USDC on Base:
  1. Install MetaMask or Coinbase Wallet
  2. Bridge funds to Base network
  3. Get USDC on Base

Troubleshooting

  • Ensure wallet has sufficient USDC on Base
  • Check network is correct (Base, not Ethereum)
  • Verify payTo address is correct
  • Include x-payments header in request
  • Use x402 SDK for automatic payment handling