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, or{ skip: true, result }to use a locally produced verification result. - onAfterVerify — Runs after successful verification. Return
{ skipHandler: true, response? }to settle without invoking the resource handler. - onVerifyFailure — Runs on verification failure. Return
{ recovered: true, result }to override. - onBeforeSettle — Runs before settlement. Return
{ abort: true, reason }to reject, or{ skip: true, result }to use a locally produced settlement result. - onAfterSettle — Runs after successful settlement.
- onSettleFailure — Runs on settlement failure. Return
{ recovered: true, result }to override. - onVerifiedPaymentCanceled — Runs when a verified payment is not settled because the protected handler throws or returns an error response.
- 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. - onPaymentResponse — Runs after a paid request completes. Return
{ recovered: true }to tell the transport to retry with a fresh payload.
- 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
MCP Hooks
x402MCPClient (MCP client)
Register hooks on the MCP client for payment lifecycle events specific to tool calls. Use cases include logging payments, enforcing per-tool spending limits or auditing payment receipts.- onPaymentRequired — Runs when a 402 payment required response is received from a tool call. First hook to return a result wins. Return
{ abort: true }to cancel,{ payment }to supply a pre-built payload, orvoidto proceed with normal payment flow. - onBeforePayment — Runs after payment approval but before the payment payload is created.
- onAfterPayment — Runs after the payment payload is submitted and the tool result is received.
x402MCPServer payment wrapper (MCP server)
Register hooks in thePaymentWrapperConfig.hooks object when creating a payment wrapper. Use cases include rate limiting, logging tool executions or sending payment receipts.
- onBeforeExecution — Runs after payment verification, before the tool handler executes. Return
falseto abort execution. - onAfterExecution — Runs after the tool handler returns, before settlement.
- onAfterSettlement — Runs after successful payment settlement.
Hook Chaining
Hooks can be chained when registering multiple handlers:- TypeScript
- Python
- Go
Next, explore:
- Client / Server — roles and responsibilities
- Facilitator — verification and settlement service