Skip to main content
A payment handler is the bridge between a wallet and a payment scheme. When the fetch wrapper receives a 402 response, it passes the payment requirements to each registered handler. Handlers that can fulfill a requirement return a PaymentExecer — an object that can execute the payment when called.

The PaymentHandler interface

A handler receives the request context and a list of payment requirements. It returns an array of PaymentExecer objects for every requirement it can fulfill. If the handler cannot fulfill any requirement, it returns an empty array.

The PaymentExecer interface

Each execer is bound to a specific requirement. When exec() is called, it signs the payment using the associated wallet and returns the payload for the X-PAYMENT header.

How handlers compose

Multiple handlers can be registered simultaneously. The fetch wrapper collects all execers from all handlers and passes them to a payer chooser function.
The default payer chooser, chooseFirstAvailable, picks the first handler that can fulfill a requirement. You can provide a custom chooser to implement logic like balance checking, fee comparison, or chain preference.

Handler selection flow

Built-in handlers

Building custom handlers

A custom handler follows the same interface. Return PaymentExecer objects for requirements you can handle, and an empty array for those you cannot.
See Plugins for community-contributed handlers.

Further reading