Basic — 0.001 USDC per request
import { withPayment } from "@ace_won/x402";
import { NextRequest, NextResponse } from "next/server";
export const GET = withPayment({ amount: "0.001" }, async (req: NextRequest) => {
return NextResponse.json({ data: "basic content" });
});
Pay directly to your wallet
export const GET = withPayment({
amount: "0.01",
payTo: "0xYourWalletAddress",
description: "Premium analytics data",
}, async (req: NextRequest) => {
const data = await getPremiumData();
return NextResponse.json({ data });
});
POST endpoint — AI inference
export const POST = withPayment({
amount: "0.005",
description: "AI inference — pay per request",
}, async (req: NextRequest) => {
const { prompt } = await req.json();
const result = await runInference(prompt);
return NextResponse.json({ result });
});
Multiple price tiers
// app/api/basic/route.ts — 0.001 USDC
export const GET = withPayment(
{ amount: "0.001", description: "Basic data" },
basicHandler
);
// app/api/premium/route.ts — 0.01 USDC
export const GET = withPayment({
amount: "0.01",
payTo: "0xYourWallet",
description: "Premium tier with full dataset",
}, premiumHandler);
Access payer address in your handler
export const GET = withPayment({ amount: "0.001" }, async (req: NextRequest) => {
const payerAddress = req.headers.get("x-payment-payer");
return NextResponse.json({ data: "your content", paidBy: payerAddress });
});
The full source for
/api/arc-stats is in the GitHub repo. Use it as a working reference.
