x402
  • Welcome to x402
  • x402 Landing Page
  • CDP's x402 Docs
  • FAQ
  • Getting Started
    • Quickstart for Buyers
    • Quickstart for Sellers
  • Core Concepts
    • HTTP 402
    • Client / Server
    • Facilitator
    • Wallet
  • Guides
    • MCP Server with x402
Powered by GitBook
On this page
  • Prerequisites
  • 1. Install Dependencies
  • 2. Add Payment Middleware
  • 3. Test Your Integration
  • 4. Error Handling
  • Next Steps
  • Summary
  1. Getting Started

Quickstart for Sellers

PreviousQuickstart for BuyersNextHTTP 402

Last updated 10 days ago

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.

Prerequisites

Before you begin, ensure you have:

  • A crypto wallet to receive funds (any EVM-compatible wallet, e.g., CDP Wallet)

  • (optional) A (CDP) account and API Keys

    • Required for mainnet use until other facilitators go live

  • and npm installed

  • An existing API or server

Note We have pre-configured , including examples for Express, Next.js, and Hono. We also have an that shows how to use the x402 SDKs to build a more complex payment flow.

1. Install Dependencies

Install the .

npm install x402-express
npm install @coinbase/x402 # for the mainnet facilitator

Install the .

npm install x402-next
npm install @coinbase/x402 # for the mainnet facilitator

Install the .

npm install x402-hono
npm install @coinbase/x402 # for the mainnet facilitator

2. Add Payment Middleware

Integrate the payment middleware into your application. You will need to provide:

  • The Facilitator URL or facilitator object. For testing, use https://x402.org/facilitator which works on Base Sepolia. For mainnet, you can use the facilitator from @coinbase/x402.

  • The routes you want to protect.

  • Your receiving wallet address.

import express from "express";
import { paymentMiddleware, Network } from "x402-express";

const app = express();

app.use(paymentMiddleware(
  "0xYourAddress", // your receiving wallet address 
  {  // Route configurations for protected endpoints
      "GET /weather": {
        // USDC amount in dollars
        price: "$0.001",
        network: "base-sepolia",
      },
    },
  {
    url: "https://x402.org/facilitator", // Facilitator URL for Base Sepolia testnet. 
  }
));

// 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`);
});
import { paymentMiddleware, Network } from 'x402-next';

// Configure the payment middleware
export const middleware = paymentMiddleware(
  "0xYourAddress", // your receiving wallet address 
  {  // Route configurations for protected endpoints
    '/protected': {
      price: '$0.01',
      network: "base-sepolia",
      config: {
        description: 'Access to protected content'
      }
    },
  }
  {
    url: "https://x402.org/facilitator", // Facilitator URL for Base Sepolia testnet. 
  }
);

// Configure which paths the middleware should run on
export const config = {
  matcher: [
    '/protected/:path*',
  ]
};
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { paymentMiddleware, Network } from "x402-hono";

const app = new Hono();

// Configure the payment middleware
app.use(paymentMiddleware(
  "0xYourAddress", // your receiving wallet address 
  {  // Route configurations for protected endpoints
    "/protected-route": {
      price: "$0.10",
      network: "base-sepolia",
      config: {
        description: "Access to premium content",
      }
    }
  },
  {
    url: "https://x402.org/facilitator", // Facilitator URL for Base Sepolia testnet. 
  }
));

// Implement your route
app.get("/protected-route", (c) => {
  return c.json({ message: "This content is behind a paywall" });
});

serve({
  fetch: app.fetch,
  port: 3000
});

This is the interface for the payment middleware config:

interface PaymentMiddlewareConfig {
  description?: string;               // Description of the payment
  mimeType?: string;                  // MIME type of the resource
  maxTimeoutSeconds?: number;         // Maximum time for payment (default: 60)
  outputSchema?: Record; // JSON schema for the response
  customPaywallHtml?: string;         // Custom HTML for the paywall
  resource?: string;                  // Resource URL (defaults to request URL)
}

When a request is made to this route 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:3000/your-endpoint).

  2. The server responds with a 402 Payment Required, including payment instructions in the body.

  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 X-PAYMENT header containing the cryptographic proof of payment (payment payload).

  5. The server verifies the payment via the facilitator and, if valid, returns your actual API response (e.g., { "data": "Your paid API response." }).

4. Error Handling

  • npm install the dependencies in each example

Next Steps

  • Get started as a buyer

Summary

This quickstart covered:

  • Installing the x402 SDK and relevant middleware

  • Adding payment middleware to your API and configuring it

  • Testing your integration

Your API is now ready to accept crypto payments through x402.

For more information on running in production on mainnet, check out

Full example in the repo .

Full example in the repo . Since this is a fullstack example, we recommend using the example to build this yourself, and treat the code snippet below as a reference.

Full example in the repo .

If you get an error stating Cannot find module 'x402-hono/express' or its corresponding type declarations., add the tsconfig.json from the to your project.

Looking for something more advanced? Check out the

For questions or support, join our .

Coinbase Developer Platform
Node.js
examples available in our repo
advanced example
x402 Express middleware package
x402 Next.js middleware package
x402 Hono middleware package
CDP's Quickstart for Sellers
here
here
here
Hono example
Advanced Example
Discord