@faremeter/middleware adds x402 payment walls to your HTTP server. When a request hits a protected route, the middleware checks for a valid X-PAYMENT header. If missing, it returns a 402 response with payment requirements. If present, it verifies the payment through a facilitator and allows the request to proceed.
How it works
- A request arrives at a protected endpoint.
- The middleware checks for an
X-PAYMENTheader. - If missing: returns
402 Payment Requiredwith the configured requirements. - If present: sends the payment to the facilitator for verification and settlement.
- If the facilitator confirms: the request continues to your handler.
- If the facilitator rejects: returns an error response.
Framework support
| Framework | Import path |
|---|---|
| Express | @faremeter/middleware/express |
| Hono | @faremeter/middleware/hono |
| Custom | @faremeter/middleware/common |
common module provides the core payment verification logic without framework bindings. Use it to integrate with any HTTP server.
Configuration
All middleware variants accept the same configuration:| Option | Type | Description |
|---|---|---|
facilitatorURL | string | URL of the facilitator server. |
accepts | x402PaymentRequirements[] | Payment methods this endpoint accepts. |
cacheConfig | AgedLRUCacheOpts | Optional cache configuration for facilitator responses. |
Defining payment requirements
Use@faremeter/info to construct payment requirements:
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.
Framework-specific guides
- Express — Express middleware setup.
- Hono — Hono middleware setup.
- Facilitator — Running your own facilitator.