import express from "express";
import { createExpressMiddleware, X402Server } from "x402x-server";
import { Facilitator } from "x402x-facilitator";
import { createPublicClient, http } from "viem";
import { bscTestnet } from "viem/chains";
const app = express();
const client = createPublicClient({ chain: bscTestnet, transport: http() });
const facilitator = new Facilitator({ recipientAddress: "0xSeller", waitUntil: "confirmed" });
const server = new X402Server({ client, facilitator });
const payment = createExpressMiddleware({
server,
getToken: () => "0xUSDC",
getAmount: () => "1000000", // 1 USDC (6 decimals)
// 可选:getConfig/onError/on402/onPaymentSuccess
});
app.post("/premium", payment, (req, res) => {
const { payer, txHash } = req.x402!;
res.json({ success: true, payer, txHash, data: "Premium content" });
});
app.listen(3000);