TypeScript SDK

x402.verifyPayment

Verifies and processes X402 payments for protected resources.

This function implements the X402 payment protocol, verifying payment proofs and settling payments through a facilitator service. It handles the complete payment flow from validation to settlement.

Example

// Usage in a Next.js API route
import { verifyPayment, facilitator } from "thirdweb/x402";
import { createThirdwebClient } from "thirdweb";
const client = createThirdwebClient({
secretKey: process.env.THIRDWEB_SECRET_KEY,
});
const thirdwebFacilitator = facilitator({
client,
serverWalletAddress: "0x1234567890123456789012345678901234567890",
});
export async function GET(request: Request) {
const paymentData = request.headers.get("x-payment");
const result = await verifyPayment({
resourceUrl: "https://api.example.com/premium-content",
method: "GET",
paymentData,
payTo: "0x1234567890123456789012345678901234567890",
network: "eip155:84532", // CAIP2 format: "eip155:<chain_id>"
price: "$0.10", // or { amount: "100000", asset: { address: "0x...", decimals: 6 } }
facilitator: thirdwebFacilitator,
routeConfig: {
description: "Access to premium API content",
mimeType: "application/json",
maxTimeoutSeconds: 300,
},
});
if (result.status === 200) {
// Payment verified and settled successfully
return Response.json(
{ data: "premium content" },
{
headers: result.responseHeaders,
},
);
} else {
// Payment required
return Response.json(result.responseBody, {
status: result.status,
headers: result.responseHeaders,
});
}
}
function verifyPayment(
): Promise<VerifyPaymentResult>;

Parameters

Configuration object containing payment verification parameters

Type

let args: {
facilitator: ReturnType<typeof facilitatorType>;
method: "GET" | "POST" | ({} & string);
network: FacilitatorNetwork;
paymentData?: string | null;
payTo: Address;
price: Money | ERC20TokenAmount;
resourceUrl: string;
routeConfig?: PaymentMiddlewareConfig;
};

Returns

let returnType:
| {
paymentReceipt: FacilitatorSettleResponse;
responseHeaders: Record<string, string>;
status: 200;
}
| {
responseBody: {
accepts: Array<RequestedPaymentRequirements>;
error: string;
payer?: string;
x402Version: number;
};
responseHeaders: Record<string, string>;
status: 402;
};

A promise that resolves to either a successful payment result (200) or payment required error (402)