Skip to main content
Accept x402 payments in your Next.js App Router application using the @armory-sh/middleware-next package.

Installation

Basic Setup

Create a middleware.ts file in your Next.js app root:

Route Patterns

Next.js middleware uses the same matcher config as standard Next.js middleware:

Per-Route Configuration

Configure different payment requirements for different routes:

Wildcard Routes

Use wildcards to match multiple routes with a single configuration:

Resource Server

The x402ResourceServer manages payment schemes and requirements:

Response Format

When payment is verified, the middleware returns a JSON response:
When payment is required, it returns a 402 response with payment requirements:

Complete Example

API Routes

Your API routes can check for payment verification:

Error Handling

The middleware automatically handles common errors:
  • 404: Route not found (no matching payment configuration)
  • 402: Payment required (no payment header provided)
  • 400: Invalid payment payload
  • 500: Configuration error

Types