Skip to main content
There are two ways to implement dynamic pricing depending on your setup.

With in-process handlers

When using in-process handlers (x402Handlers + pricing), handlers receive the resource URL as context and can adjust pricing per request. This is the simplest path — configure base pricing and let the handler do the work.
The handler’s getRequirements method receives the resource URL (e.g. /api/premium) and can return different amounts based on the path, query parameters, or any other aspect of the request.

With handleMiddlewareRequest

When you need to vary pricing from the middleware layer itself, 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. Unlike fixed pricing per route, dynamic pricing lets you derive payment requirements from any aspect of the incoming request: headers, query parameters, path, or body. You can also vary:
  • The networks, schemes, and assets accepted
  • The recipient wallet address
  • The facilitator URL (via createRemoteX402Handlers)
Dynamic payment requirements must be idempotent. The same request must produce the same requirements every time, because the client may retry payment against the same requirements.

Server

server.ts

Client

client.ts

Running the example

The client requests /protected?amount=1000, and the server returns payment requirements for 1000 units (0.001 USDC). Changing the amount query parameter changes the price charged.