TypeScript SDK

x402.verifyPayment

Verifies X402 payments for protected resources. This function only verifies the payment, you should use settlePayment to settle the payment.

Example

// Usage in a Next.js API route
import { verifyPayment, facilitator } from "thirdweb/x402";
import { createThirdwebClient } from "thirdweb";
import { arbitrumSepolia } from "thirdweb/chains";
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 paymentArgs = {
resourceUrl: "https://api.example.com/premium-content",
method: "GET",
paymentData,
payTo: "0x1234567890123456789012345678901234567890",
network: arbitrumSepolia, // or any other chain
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,
},
};
// verify the payment
const result = await verifyPayment(paymentArgs);
if (result.status === 200) {
// Payment verified, but not settled yet
// you can do the work that requires payment first
const result = await doSomething();
// then settle the payment
const settleResult = await settlePayment(paymentArgs);
// then return the result
return Response.json(result);
} else {
// verification failed, return payment required
return Response.json(result.responseBody, {
status: result.status,
headers: result.responseHeaders,
});
}
}
function verifyPayment(
args: PaymentArgs,
): Promise<VerifyPaymentResult>;

Parameters

Configuration object containing payment verification parameters

Type

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

Returns

let returnType: Prettify<
| {
decodedPayment: RequestedPaymentPayload;
selectedPaymentRequirements: RequestedPaymentRequirements;
status: 200;
}
| PaymentRequiredResult
>;

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