@faremeter/facilitator
@faremeter/facilitator bundles an opinionated Hono router that speaks Coinbase’s x402 protocol. It turns one or more FacilitatorHandlers (see @faremeter/types/facilitator) into three public endpoints your services can expose:
Source: GitHub › packages/facilitator
POST /accepts– aggregate handler intents into a canonical set of payment requirements.POST /settle– ask handlers to settle a client payment payload.GET /supported– advertise the payment kinds each handler understands.
Quick start
API surface
createFacilitatorRoutes({ handlers, timeout? })
handlers: array ofFacilitatorHandlers. Each handler implementsgetSupported,getRequirements, andhandleSettle(returningnullwhen the request is not for them).timeout: optional per-phase limits (in milliseconds). When provided,getRequirementsandgetSupportedcalls are wrapped withallSettledWithTimeout.
App. You can mount it as-is or cherry-pick handlers (e.g. routes.fetch("/accepts", ...)) if you need advanced composition.
allSettledWithTimeout(promises, timeoutMs)
Wraps an array of promises with an individual timeout guard and returns Promise.allSettled results. Useful for fan-out operations where some backends may be slow or unavailable.
timeout(timeoutMs, message?) and sleep(delayMs, value?)
Small utilities used internally but exported for convenience when composing facilitator handlers.
Request lifecycle
- Client hits protected resource and receives 402. Your API middleware (see
@faremeter/middleware) forwards its desired payment requirements to/accepts. - Facilitator collects offers. Each handler returns compatible requirements (with network-specific extras filled in). The facilitator collapses the array and responds to the middleware.
- Client pays and retries. After the client submits a payment payload, the middleware forwards it to
/settle. - Handler validates and settles. If a handler accepts, it returns
x402SettleResponsewithsuccess: true. Otherwise the facilitator returns a 402 again, letting the client retry with another offer.
Usage patterns
- Multi-network support: Register one handler per network (e.g., Solana + EVM). The facilitator walks the array until one claims the settlement request.
- Timeout tuning: The defaults (500 ms) work for low-latency handlers. Increase them for on-chain reads (e.g., verifying Solana blockhashes) to avoid unnecessary timeouts.
- Logging: The router uses
@logtape/logtapenamespaces["faremeter","facilitator"]. Configure log sinks there if you need structured tracing.
Related references
@faremeter/types: type definitions for facilitator handlers and x402 payloads.@faremeter/payment-solana/@faremeter/payment-evm: drop-in handler factories for Solana and EVM assets.@faremeter/middleware: HTTP middleware that pairs with this facilitator to protect application routes.