Skip to main content
@faremeter/middleware adds payment walls to your HTTP server. It supports both x402 and MPP protocols. When a request hits a protected route, the middleware checks for a valid payment header. If missing, it returns a payment challenge. If present, it verifies the payment through a handler and allows the request to proceed.

How it works

  1. A request arrives at a protected endpoint.
  2. The middleware checks for a payment header (X-PAYMENT or PAYMENT-SIGNATURE).
  3. If missing: returns 402 Payment Required with the configured requirements.
  4. If present: sends the payment to the facilitator for verification and settlement.
  5. If the facilitator confirms: the request continues to your handler.
  6. If the facilitator rejects: returns an error response.

Framework support

The common module provides the core payment verification logic without framework bindings. Use it to integrate with any HTTP server.

Configuration

The middleware supports two configuration modes: in-process handlers and remote facilitator. Both are configured through CommonMiddlewareArgs. The two modes are mutually exclusive for x402 — pass either x402Handlers/pricing or facilitatorURL/accepts, not both. You can combine remote x402 handlers with in-process MPP handlers using createRemoteX402Handlers.

In-process handlers

Pass payment handlers directly to the middleware. The middleware calls them locally to resolve requirements, verify, and settle payments — no separate facilitator service needed. See the Facilitator guide for how to create handlers.
ResourcePricing is the resource server’s statement of “I want X amount of Y asset paid to Z recipient on W network.” It says nothing about x402 schemes or protocol extras — those are determined by the handlers. The recipient field maps to payTo in the underlying x402PaymentRequirements that the handler produces.

Remote facilitator

Communicate with a separate facilitator service over HTTP for x402 payments. This is the original configuration model and is still fully supported.

Mixing remote x402 with in-process MPP

Use createRemoteX402Handlers to wrap a remote facilitator as in-process handlers, then combine with MPP handlers under a shared pricing config:

Defining payment requirements

Use @faremeter/info to construct payment requirements:
You can accept multiple payment methods by passing an array. The Solana x402Exact returns an array (one entry per legacy network ID), while the EVM x402Exact returns a single object. The middleware’s accepts field supports nested arrays, so both can be passed directly:

Caching

The middleware caches facilitator /accepts responses using an LRU cache with TTL. This reduces the number of requests to the facilitator. Cache configuration is optional.

Dynamic pricing

With in-process handlers, dynamic pricing happens naturally. When the middleware calls a handler’s getRequirements method, it passes the resource URL and payment requirements as context. The handler can inspect the resource and return different prices per request. You configure base pricing via ResourcePricing and let the handler adjust. For the remote facilitator path, or when you need full control over per-request pricing from the middleware layer, use handleMiddlewareRequest.

handleMiddlewareRequest — the lower-level API

createMiddleware (used by the Express and Hono integrations) accepts static configuration at initialization. When you need to compute requirements per-request — for example, varying the price based on a query parameter, header, or request body — use handleMiddlewareRequest from @faremeter/middleware/common. The tradeoff is that you wire up the framework integration yourself: reading headers, sending JSON responses, and calling your route handler.

When to use which

Signature

handleMiddlewareRequest accepts a single options object:

The body callback

The body function runs when the client sends a valid payment. It receives a context object with a protocolVersion discriminant: x402 v1 context (protocolVersion: 1):
  • capture(), authorize(), paymentPayload, paymentRequirements
x402 v2 context (protocolVersion: 2):
  • capture(), authorize(), paymentPayload, paymentRequirements
MPP context (protocolVersion: "mpp"):
  • capture(), credential, and an optional authorize()
Use protocolVersion to narrow the context type before accessing protocol-specific fields:

Example: Hono with dynamic pricing

See the Dynamic Pricing recipe for a complete multi-chain example with client code.

Framework-specific guides

  • Express — Express middleware setup.
  • Hono — Hono middleware setup.
  • Facilitator — Running your own facilitator.