Skip to main content
This guide walks you through integrating with x402 to enable payments for your API or service. By the end, your API will be able to charge buyers and AI agents for access.
This quickstart begins with testnet configuration for safe testing. When you’re ready for production, see Running on Mainnet for the simple changes needed to accept real payments on Base (EVM) and Solana networks.
Need help? Join the x402 Discord for the latest updates.

Prerequisites

Before you begin, ensure you have:
  • A crypto wallet to receive funds (any EVM-compatible wallet, e.g., CDP Wallet)
  • (Optional) A Coinbase Developer Platform (CDP) account and API Keys
    • Required for mainnet use until other facilitators go live
  • Node.js and npm, or Go installed
  • An existing API or server
Python SDK Status: The Python SDK is currently under development for x402 v2. For immediate v2 support, use TypeScript or Go.
We have pre-configured examples available in our repo for both Node.js and Go. We also have an advanced example that shows how to use the x402 SDKs to build a more complex payment flow.

1. Install Dependencies

Install the x402 Express middleware and EVM mechanism packages:
npm install @x402/express @x402/evm @x402/core

2. Add Payment Middleware

Integrate the payment middleware into your application. You will need to provide:
  • The Facilitator URL or facilitator client. For testing, use https://x402.org/facilitator which works on Base Sepolia and Solana Devnet.
  • The routes you want to protect
  • Your receiving wallet address
The examples below show testnet configuration. When you’re ready to accept real payments, refer to Running on Mainnet for the simple changes needed.
Full example in the repo here.
import express from "express";
import { paymentMiddleware, x402ResourceServer } from "@x402/express";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { HTTPFacilitatorClient } from "@x402/core/server";

const app = express();

// Your receiving wallet address
const payTo = "0xYourAddress";

// Create facilitator client (testnet)
const facilitatorClient = new HTTPFacilitatorClient({
  url: "https://x402.org/facilitator"
});

// Create resource server and register EVM scheme
const server = new x402ResourceServer(facilitatorClient)
  .register("eip155:84532", new ExactEvmScheme());

app.use(
  paymentMiddleware(
    {
      "GET /weather": {
        accepts: [
          {
            scheme: "exact",
            price: "$0.001", // USDC amount in dollars
            network: "eip155:84532", // Base Sepolia (CAIP-2 format)
            payTo,
          },
        ],
        description: "Get current weather data for any location",
        mimeType: "application/json",
      },
    },
    server,
  ),
);

// Implement your route
app.get("/weather", (req, res) => {
  res.send({
    report: {
      weather: "sunny",
      temperature: 70,
    },
  });
});

app.listen(4021, () => {
  console.log(`Server listening at http://localhost:4021`);
});
Ready to accept real payments? See Running on Mainnet for production setup.
Route Configuration Interface:
interface RouteConfig {
  accepts: Array<{
    scheme: string;           // Payment scheme (e.g., "exact")
    price: string;            // Price in dollars (e.g., "$0.01")
    network: string;          // Network in CAIP-2 format (e.g., "eip155:84532")
    payTo: string;            // Your wallet address
  }>;
  description?: string;       // Description of the resource
  mimeType?: string;          // MIME type of the response
  extensions?: object;        // Optional extensions (e.g., Bazaar)
}
When a request is made to these routes without payment, your server will respond with the HTTP 402 Payment Required code and payment instructions.

3. Test Your Integration

To verify:
  1. Make a request to your endpoint (e.g., curl http://localhost:4021/weather).
  2. The server responds with a 402 Payment Required, including payment instructions in the PAYMENT-REQUIRED header.
  3. Complete the payment using a compatible client, wallet, or automated agent. This typically involves signing a payment payload, which is handled by the client SDK detailed in the Quickstart for Buyers.
  4. Retry the request, this time including the PAYMENT-SIGNATURE header containing the cryptographic proof of payment.
  5. The server verifies the payment via the facilitator and, if valid, returns your actual API response (e.g., { "data": "Your paid API response." }).
When using the CDP facilitator, your endpoints can be listed in the x402 Bazaar, our discovery layer that helps buyers and AI agents find services. To enable discovery and improve visibility:
Include descriptive metadata in your route configuration:
  • description: Clear explanation of what your endpoint does
  • mimeType: MIME type of your response format
  • extensions.bazaar: Enable Bazaar discovery
This metadata helps:
  • AI agents automatically understand how to use your API
  • Developers quickly find services that meet their needs
  • Improve your ranking in discovery results
Example with Bazaar extension:
{
  "GET /weather": {
    accepts: [
      {
        scheme: "exact",
        price: "$0.001",
        network: "eip155:8453",
        payTo: "0xYourAddress",
      },
    ],
    description: "Get real-time weather data including temperature, conditions, and humidity",
    mimeType: "application/json",
    extensions: {
      bazaar: {
        discoverable: true,
        category: "weather",
        tags: ["forecast", "real-time"],
      },
    },
  },
}
Learn more about the discovery layer in the x402 Bazaar documentation.

5. Error Handling

  • If you run into trouble, check out the examples in the repo for more context and full code.
  • Run npm install or go mod tidy to install dependencies

Running on Mainnet

Once you’ve tested your integration on testnet, you’re ready to accept real payments on mainnet.

Setting Up CDP Facilitator for Production

CDP’s facilitator provides enterprise-grade payment processing with compliance features:

1. Set up CDP API Keys

To use the mainnet facilitator, you’ll need a Coinbase Developer Platform account:
  1. Sign up at cdp.coinbase.com
  2. Create a new project
  3. Generate API credentials
  4. Set the following environment variables:
    CDP_API_KEY_ID=your-api-key-id
    CDP_API_KEY_SECRET=your-api-key-secret
    

2. Update Your Code

Replace the testnet configuration with mainnet settings:
import { paymentMiddleware, x402ResourceServer } from "@x402/express";
import { ExactEvmScheme } from "@x402/evm/exact/server";
import { HTTPFacilitatorClient } from "@x402/core/server";

// For mainnet, use CDP's facilitator with API keys
const facilitatorClient = new HTTPFacilitatorClient({
  url: "https://api.cdp.coinbase.com/platform/v2/x402",
  // Add auth headers if required
});

const server = new x402ResourceServer(facilitatorClient)
  .register("eip155:8453", new ExactEvmScheme()); // Base mainnet

app.use(
  paymentMiddleware(
    {
      "GET /weather": {
        accepts: [
          {
            scheme: "exact",
            price: "$0.001",
            network: "eip155:8453", // Base mainnet (CAIP-2)
            payTo: "0xYourAddress",
          },
        ],
        description: "Weather data",
        mimeType: "application/json",
      },
    },
    server,
  ),
);

3. Update Your Wallet

Make sure your receiving wallet address is a real mainnet address where you want to receive USDC payments.

4. Test with Real Payments

Before going live:
  1. Test with small amounts first
  2. Verify payments are arriving in your wallet
  3. Monitor the facilitator for any issues
Mainnet transactions involve real money. Always test thoroughly on testnet first and start with small amounts on mainnet.

Using Different Networks

CDP facilitator supports multiple networks. Simply change the network parameter using CAIP-2 format:
// Base mainnet
{
  scheme: "exact",
  price: "$0.001",
  network: "eip155:8453", // Base mainnet
  payTo: "0xYourAddress",
}

// Base Sepolia testnet
{
  scheme: "exact",
  price: "$0.001",
  network: "eip155:84532", // Base Sepolia
  payTo: "0xYourAddress",
}
Need support for additional networks like Polygon or Avalanche? You can run your own facilitator or contact CDP support to request new network additions.

Network Identifiers (CAIP-2)

x402 v2 uses CAIP-2 format for network identifiers:
NetworkCAIP-2 Identifier
Base Mainneteip155:8453
Base Sepoliaeip155:84532
Solana Mainnetsolana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp
Solana Devnetsolana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1
See Network Support for the full list.

Next Steps

For questions or support, join our Discord.

Summary

This quickstart covered:
  • Installing the x402 SDK and relevant middleware
  • Adding payment middleware to your API and configuring it
  • Testing your integration
  • Deploying to mainnet with CAIP-2 network identifiers
Your API is now ready to accept crypto payments through x402.