import "dotenv/config";
import express from "express";
import { paymentMiddleware, x402ResourceServer } from "@x402/express";
import { EscrowServerScheme } from "@x402r/evm/escrow/server";
import { refundable } from "@x402r/helpers";
import { HTTPFacilitatorClient } from "@x402/core/server";
const address = process.env.ADDRESS as `0x${string}`;
const operatorAddress = process.env.OPERATOR_ADDRESS as `0x${string}`;
if (!address || !operatorAddress) {
console.error("Missing required environment variables: ADDRESS, OPERATOR_ADDRESS");
process.exit(1);
}
const facilitatorUrl = process.env.FACILITATOR_URL;
if (!facilitatorUrl) {
console.error("FACILITATOR_URL environment variable is required");
process.exit(1);
}
const facilitatorClient = new HTTPFacilitatorClient({ url: facilitatorUrl });
const app = express();
app.use(
paymentMiddleware(
{
"GET /weather": {
accepts: [
refundable(
{
scheme: "escrow",
price: "$0.01",
network: "eip155:84532",
payTo: address,
},
operatorAddress,
),
],
description: "Weather data",
mimeType: "application/json",
},
},
new x402ResourceServer(facilitatorClient).register(
"eip155:84532",
new EscrowServerScheme() as never,
),
),
);
app.get("/weather", (req, res) => {
res.send({
report: { weather: "sunny", temperature: 70 },
});
});
app.listen(4021, () => {
console.log("Server listening at http://localhost:4021");
});