3.3 KiB

x402-next Example App

This is a Next.js application that demonstrates how to use the x402-next middleware to implement paywall functionality in your Next.js routes with EVM, SVM, and optional Stellar payment support.

Prerequisites

  • Node.js v20+ (install via nvm)
  • pnpm v10 (install via pnpm.io/installation)
  • A valid Ethereum address for receiving payments
  • A valid Solana address for receiving payments
  • (Optional) A valid Stellar address for receiving payments

Setup

  1. Copy .env.local to .env and add your addresses to receive payments:
cp .env.local .env
  1. Install and build all packages from the typescript examples root:
cd ../../
pnpm install
pnpm build
cd fullstack/next
  1. Install and start the Next.js example:
pnpm dev

Example Routes

The app includes protected routes that require payment to access:

Protected Page Route

The /protected route requires a payment of $0.01 to access. The route is protected using the x402-next middleware:

// middleware.ts
import { paymentMiddleware, Network, Resource } from "x402-next";

const facilitatorUrl = process.env.NEXT_PUBLIC_FACILITATOR_URL as Resource;
const payTo = process.env.RESOURCE_WALLET_ADDRESS as Address;
const network = process.env.NETWORK as Network;

export const middleware = paymentMiddleware(
  payTo,
  {
    "/protected": {
      price: "$0.01",
      network,
      config: {
        description: "Access to protected content",
      },
    },
  },
  {
    url: facilitatorUrl,
  },
);

// Configure which paths the middleware should run on
export const config = {
  matcher: ["/protected/:path*"],
};

Response Format

Payment Required (402)

{
  "error": "X-PAYMENT header is required",
  "paymentRequirements": {
    "scheme": "exact",
    "network": "base",
    "maxAmountRequired": "1000",
    "resource": "http://localhost:3000/protected",
    "description": "Access to protected content",
    "mimeType": "",
    "payTo": "0xYourAddress",
    "maxTimeoutSeconds": 60,
    "asset": "0x...",
    "outputSchema": null,
    "extra": null
  }
}

Successful Response

// Headers
{
  "X-PAYMENT-RESPONSE": "..." // Encoded response object
}

Extending the Example

To add more protected routes, update the middleware configuration:

export const middleware = paymentMiddleware(
  payTo,
  {
    "/protected": {
      price: "$0.01",
      network,
      config: {
        description: "Access to protected content",
      },
    },
    "/api/premium": {
      price: "$0.10",
      network,
      config: {
        description: "Premium API access",
      },
    },
  }
);

export const config = {
  matcher: ["/protected/:path*", "/api/premium/:path*"],
};

Environment Variables

Required

  • PORT - HTTP server port
  • EVM_PAYEE_ADDRESS - Ethereum address to receive payments
  • SVM_PAYEE_ADDRESS - Solana address to receive payments
  • FACILITATOR_URL - Facilitator endpoint URL

Optional

  • STELLAR_PAYEE_ADDRESS - Stellar address to receive payments - enables Stellar endpoints
  • EVM_NETWORK - EVM network (default: eip155:84532)
  • SVM_NETWORK - SVM network (default: solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1)
  • STELLAR_NETWORK - Stellar network (default: stellar:testnet)