Server Hooks
x402ResourceServer (Transport-agnostic)
Register hooks on the core resource server for verification and settlement lifecycle events. Use cases include logging payments, recording analytics, implementing custom access control or recovering from transient failures.- onBeforeVerify — Runs before payment verification. Return
{ abort: true, reason }to reject. - onAfterVerify — Runs after successful verification.
- onVerifyFailure — Runs on verification failure. Return
{ recovered: true, result }to override. - onBeforeSettle — Runs before settlement. Return
{ abort: true, reason }to reject. - onAfterSettle — Runs after successful settlement.
- onSettleFailure — Runs on settlement failure. Return
{ recovered: true, result }to override.
- TypeScript
- Python
- Go
x402HTTPResourceServer (HTTP)
Register hooks for HTTP-specific request handling before payment processing. Use cases include bypassing payment for API key holders, granting access to subscribers or blocking specific clients.- onProtectedRequest — Runs on every request to a protected route.
- Return
{ grantAccess: true }to bypass payment. - Return
{ abort: true, reason }to return 403. - Return
voidto continue to payment flow.
- Return
- TypeScript
- Python
- Go
Client Hooks
x402Client (Transport-agnostic)
Register hooks on the core client for payment payload creation lifecycle events. Common use cases include enforcing spending limits, requiring approval for large payments or logging outgoing transactions.- onBeforePaymentCreation — Runs before creating a payment payload. Return
{ abort: true, reason }to cancel. - onAfterPaymentCreation — Runs after successful payload creation.
- onPaymentCreationFailure — Runs on failure. Return
{ recovered: true, payload }to provide fallback.
- TypeScript
- Python
- Go
x402HTTPClient (HTTP)
Register hooks for HTTP-specific payment required handling. Use cases include trying API key authentication before paying or prompting users for payment confirmation.- onPaymentRequired — Runs when a 402 response is received.
- Return
{ headers }to retry with alternate headers before paying. - Return
voidto proceed directly to payment.
- Return
- TypeScript
- Python
- Go
Facilitator Hooks
x402Facilitator
Register hooks on the facilitator for verification and settlement lifecycle events. Use cases include populating a bazaar discovery catalog, compliance checks or collecting metrics across all processed payments.- onBeforeVerify / onAfterVerify / onVerifyFailure — Same pattern as server hooks.
- onBeforeSettle / onAfterSettle / onSettleFailure — Same pattern as server hooks.
- TypeScript
- Python
- Go
Hook Chaining
All SDKs support method chaining for registering multiple hooks:- TypeScript
- Python
- Go
Next, explore:
- Client / Server — roles and responsibilities
- Facilitator — verification and settlement service