Skip to content

Interface: PaymentRequiredOptions<TSpec, TRequirements, TPayload> ​

Defined in: packages/x402-express/src/options.ts:98

Configuration for paymentRequired.

Type Parameters ​

TSpec ​

TSpec extends BaseRequirementsSpec = CantonRequirementsSpec

TRequirements ​

TRequirements extends X402PaymentRequirements<unknown> = CantonPaymentRequirements

TPayload ​

TPayload extends X402PaymentPayload<unknown> = CantonPaymentPayload

Properties ​

facilitator ​

ts
facilitator: FacilitatorClient;

Defined in: packages/x402-express/src/options.ts:104

Client used to verify (and optionally settle) payments against the facilitator.


onQuoteError? ​

ts
onQuoteError?: (error, req) => void | Promise<void>;

Defined in: packages/x402-express/src/options.ts:183

Called when a way to pay could not be quoted and was left out of the 402, e.g. the facilitator no longer settles the instrument it names. The request is still answered, from the entries that did quote.

Nothing else reports this: the 402 simply has fewer entries, so without a hook a way to pay can disappear unnoticed. Fires once per dropped entry per request, so throttle if you log it. A throw (or a rejected promise) is swallowed, as with PaymentRequiredOptions.onReject.

Not called when NO entry can be quoted — there is nothing to answer with then, and the request fails instead: 502 when the facilitator is what failed, whatever the number of entries, and 500 when the cause is the merchant's own config.

Parameters ​

error ​

Error

req ​

Request

Returns ​

void | Promise<void>


onReject? ​

ts
onReject?: (rejection, req) => void | Promise<void>;

Defined in: packages/x402-express/src/options.ts:164

Called after the middleware rejects a payment it was handed (402), or fails on the facilitator (502) or its own config (500) — e.g. to log it. Not called for the plain 402 challenge to a request without a payment. The middleware logs nothing itself. A throw (or a rejected promise from an async hook) is swallowed so it can't break the response or crash the process.

Parameters ​

rejection ​

PaymentRejectionInfo<TRequirements, TPayload>

req ​

Request

Returns ​

void | Promise<void>


preSettle? ​

ts
preSettle?: (payment, req) => string | Promise<string | undefined> | undefined;

Defined in: packages/x402-express/src/options.ts:147

Your own say on a verified payment, run after the facilitator has verified it and before anything settles. Return a short reason to refuse it (answered as a 402 carrying that code), or nothing to let it through.

This is where merchant state goes, because the middleware has none. It rebuilds the terms on every request, so it can tell you the payer echoed a memo, but not whether it is one you issued; it knows the price it quotes now, but not whether that order is still open. Those are questions only your records answer:

ts
preSettle: async ({ requirements, payer }) => {
  const order = await orders.findByRef(requirements.extra.memo);
  if (!order) return "policy_memo_unknown";
  if (order.status !== "awaiting-payment") return "policy_order_closed";
  if (order.buyer !== payer) return "policy_wrong_payer";
},

Running after verify means requirements has been held against the signed transfer, so a memo here is one the payer committed to rather than one it claims — safe to reserve stock or mark an order claimed on. payer is the verified transfer's sender, and is unavailable before this point: the payload names none (spec Rule 8).

With settle: false nothing settles, and this is the last gate before the request is let through.

Parameters ​

payment ​
payer ​

string

The verified transfer's sender.

payload ​

TPayload

requirements ​

TRequirements

verify ​

| { extensions?: { [key: string]: unknown; }; extra?: { [key: string]: unknown; }; isValid: true; payer?: string; } | { extensions?: { [key: string]: unknown; }; extra?: { [key: string]: unknown; }; invalidMessage?: string; invalidReason: | "invalid_exact_canton_missing_proof" | "invalid_exact_canton_malformed_payload" | "invalid_exact_canton_signature_invalid" | "invalid_exact_canton_amount_mismatch" | "invalid_exact_canton_merchant_mismatch" | "invalid_exact_canton_instrument_id_mismatch" | "invalid_exact_canton_preapproval_missing" | "invalid_exact_canton_fee_payer_mismatch" | "invalid_exact_canton_expired" | "invalid_exact_canton_execute_before_too_far" | "invalid_exact_canton_insufficient_inputs" | "invalid_exact_canton_memo_mismatch" | "invalid_exact_canton_merchant_not_registered" | "invalid_exact_canton_self_payment" | "invalid_exact_canton_execute_failed" | "unexpected_canton_ledger_error" | "settlement_pending" | "invalid_network" | "unsupported_scheme" | "insufficient_funds" | "invalid_payload" | "invalid_payment_requirements" | "invalid_scheme" | "invalid_x402_version" | "invalid_transaction_state" | "unexpected_verify_error" | "unexpected_settle_error" | "invalid_exact_canton_synchronizer_mismatch"; isValid: false; payer?: string; }

req ​

Request

Returns ​

string | Promise<string | undefined> | undefined


requirements ​

ts
requirements: 
  | RequirementsSpec<TSpec>
| RequirementsBuilder<TRequirements>;

Defined in: packages/x402-express/src/options.ts:117

The terms to advertise: a declarative spec, or a builder for the whole list.


scheme? ​

ts
scheme?: SchemeAdapter<TSpec, TRequirements, TPayload>;

Defined in: packages/x402-express/src/options.ts:113

The payment scheme this route is gated on. Defaults to Canton's registered exact (cantonExact).

It owns extra — what a spec puts in it, how an echoed one is compared, where a per-challenge reference lives, and how to read the payer. Everything else here is the protocol's, and applies whatever the scheme.


settle? ​

ts
settle?: boolean;

Defined in: packages/x402-express/src/options.ts:115

Capture the payment (call the facilitator's /v2/settle) after a valid verify. Default false.