Skip to content

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 ​

ts
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 older X-PAYMENT) → 402 with the spec's PaymentRequired, { x402Version, resource, accepts }, in the PAYMENT-REQUIRED header and as the body. One accepts[] 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.x402 and call next(). A settle answer, success or failure, goes back in the PAYMENT-RESPONSE header.

Parameters ​

options ​

PaymentRequiredOptions

Returns ​

RequestHandler

Example ​

ts
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 ​

ts
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 older X-PAYMENT) → 402 with the spec's PaymentRequired, { x402Version, resource, accepts }, in the PAYMENT-REQUIRED header and as the body. One accepts[] 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.x402 and call next(). A settle answer, success or failure, goes back in the PAYMENT-RESPONSE header.

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 ​

ts
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
    ],
  },
}));