Skip to main content
x402 payment middleware for Next.js App Router.

Installation

Features

  • Per-route payment configuration
  • Route pattern matching (exact, wildcard, parameterized)
  • Resource server for payment scheme management
  • Built-in verification integration
  • Full TypeScript support

Quick Start

API

paymentProxy(routes, resourceServer)

Creates a payment proxy handler for Next.js middleware. Parameters:
  • routes: Record mapping route patterns to payment config
  • resourceServer: x402ResourceServer instance
Returns: Next.js middleware function

x402ResourceServer

Registers and manages payment schemes. Methods:
  • register(chainId, scheme): Register a payment scheme
  • getRequirements(chainId): Get requirements for a chain
  • getAllRequirements(): Get all registered requirements

Types

Route Patterns

Supports three types of route patterns:
  • Exact: /api/users - matches only /api/users
  • Wildcard: /api/* - matches /api/users, /api/posts/123
  • Parameterized: /api/users/:id - matches /api/users/123

Examples

See the Next.js Middleware guide for complete examples.

Advanced Mix-and-Match Config (Current API)

If you are using paymentMiddleware directly, you can route by chain/token/facilitator and attach extensions:
For mixed amounts per route/combination, use explicit requirements arrays:
Unsupported extension keys are fail-open filtered out per facilitator capability response.