// Base (8453) and Celo mainnet (42220). Node 20+. npm i express @x402/express @x402/core @x402/evm
import express from "express";
import { paymentMiddleware, x402ResourceServer } from "@x402/express";
import { HTTPFacilitatorClient, type RoutesConfig } from "@x402/core/server";
import { ExactEvmScheme } from "@x402/evm/exact/server";
const payTo = "0xYourSellerPayoutAddress"; // example
// Stands in for the facilitator client you already have. Reuse that client
// as configured: building a new one from its URL drops any createAuthHeaders.
const existingFacilitator = new HTTPFacilitatorClient({ url: process.env.FACILITATOR_URL! });
// Celo's hosted facilitator. Note the api. subdomain
const celoFacilitator = new HTTPFacilitatorClient({
url: "https://api.x402.celo.org",
createAuthHeaders: async () => {
const h = { "X-API-Key": process.env.X402_API_KEY! };
return { verify: h, settle: h, supported: h };
},
});
// Celo first: for each network the server uses the first facilitator that supports it
const server = new x402ResourceServer([celoFacilitator, existingFacilitator])
.register("eip155:8453", new ExactEvmScheme())
.register("eip155:42220", new ExactEvmScheme());
// A Celo mainnet offer for $0.01 in a 6-decimal token
const onCelo = (asset: string, name: string, version: string) => ({
scheme: "exact",
network: "eip155:42220" as const,
payTo,
price: { amount: "10000", asset, extra: { name, version } },
});
const routes: RoutesConfig = {
"GET /rates": {
accepts: [
{ scheme: "exact", network: "eip155:8453", payTo, price: "$0.01" },
onCelo("0xcebA9300f2b948710d2653dD7B07f33A8B32118C", "USDC", "2"), // Celo mainnet USDC, 6 decimals
onCelo("0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e", "Tether USD", "1"), // Celo mainnet USDT, 6 decimals
onCelo("0xD2ab3C9A02DBBAB236BfEC45D1d755DF4267F771", "Tether America USD", "1"), // Celo mainnet USA₮, 6 decimals
],
description: "Latest FX rates",
mimeType: "application/json",
},
};
const app = express();
app.use(paymentMiddleware(routes, server));
app.get("/rates", (_req, res) => res.json({ usd_ngn: 1500 }));
app.listen(3000);