Function: paymentRequired()
@chainsafe/x402-express
Merchant-side Express middleware for x402 payments. paymentRequired() answers 402 with the ways to pay when a request is unpaid, verifies the payment via a facilitator (optionally settling it), and lets paid requests through with req.x402 attached.
Canton's registered exact is the default scheme and needs no wiring. The middleware itself is scheme-agnostic: pass a SchemeAdapter to gate a route on something else.
Call Signature
function paymentRequired(options): RequestHandler;Defined in: packages/x402-express/src/middleware.ts:63
Express middleware that gates a route behind an x402 payment.
Two signatures: the first is the default canton-exact scheme, which keeps the concrete types so an inline spec is contextually typed against them. The second is the generic one, for a route gated on another scheme via PaymentRequiredOptions.scheme.
- No/invalid payment in
PAYMENT-SIGNATURE(or the olderX-PAYMENT) →402with the spec'sPaymentRequired,{ x402Version, resource, accepts }, in thePAYMENT-REQUIREDheader and as the body. Oneaccepts[]entry per way to pay; entries sharing a reference generator are issued one freshly minted reference between them, a 402 being one sale. - Valid payment → verify (and optionally settle) via the facilitator, then attach
req.x402and callnext(). A settle answer, success or failure, goes back in thePAYMENT-RESPONSEheader.
Parameters
options
Returns
RequestHandler
Example
app.use("/paid", paymentRequired({
facilitator,
settle: true,
requirements: {
payTo: "merchant::1220…",
resource: "https://merchant.example/report",
accepts: [
{ amount: "500000000", asset: "Amulet" }, // 0.05 CC
{ amount: "250000", asset: "USDCx" }, // priced separately
],
},
}));Call Signature
function paymentRequired<TSpec, TRequirements, TPayload>(options): RequestHandler;Defined in: packages/x402-express/src/middleware.ts:68
Express middleware that gates a route behind an x402 payment.
Two signatures: the first is the default canton-exact scheme, which keeps the concrete types so an inline spec is contextually typed against them. The second is the generic one, for a route gated on another scheme via PaymentRequiredOptions.scheme.
- No/invalid payment in
PAYMENT-SIGNATURE(or the olderX-PAYMENT) →402with the spec'sPaymentRequired,{ x402Version, resource, accepts }, in thePAYMENT-REQUIREDheader and as the body. Oneaccepts[]entry per way to pay; entries sharing a reference generator are issued one freshly minted reference between them, a 402 being one sale. - Valid payment → verify (and optionally settle) via the facilitator, then attach
req.x402and callnext(). A settle answer, success or failure, goes back in thePAYMENT-RESPONSEheader.
Type Parameters
TSpec
TSpec extends BaseRequirementsSpec
TRequirements
TRequirements extends X402PaymentRequirements<unknown>
TPayload
TPayload extends X402PaymentPayload<unknown, Record<string, unknown>>
Parameters
options
PaymentRequiredOptions<TSpec, TRequirements, TPayload> & object
Returns
RequestHandler
Example
app.use("/paid", paymentRequired({
facilitator,
settle: true,
requirements: {
payTo: "merchant::1220…",
resource: "https://merchant.example/report",
accepts: [
{ amount: "500000000", asset: "Amulet" }, // 0.05 CC
{ amount: "250000", asset: "USDCx" }, // priced separately
],
},
}));