Skip to content

createX402Handler

createX402Handler(params): X402RequestHandler

Defined in: src/x402/server.ts:212

Builds an X402RequestHandler that wraps params.handler with the x402 payment flow.

Parameters

params

CreateX402HandlerParams

Returns

X402RequestHandler

Example

import { createServer } from "node:http";
import { parseUnits } from "viem";
import {
buildPaymentRequirements,
createSelfFacilitator,
createX402Handler,
JPYC_DECIMALS,
JPYC_V2_ADDRESS,
polygonAmoy,
} from "kawasekit";
const handler = createX402Handler({
facilitator: createSelfFacilitator({ walletClient, publicClient }),
requirementsFor: (req) =>
new URL(req.url).pathname.startsWith("/weather")
? [buildPaymentRequirements({
chainId: polygonAmoy.id,
asset: JPYC_V2_ADDRESS,
payTo: "0x...",
amount: parseUnits("0.001", JPYC_DECIMALS),
})]
: null,
handler: async (req) => new Response(JSON.stringify({ weather: "sunny" }), {
headers: { "content-type": "application/json" },
}),
});
// Mount on any framework that exposes WHATWG Request → Response.