import 'dotenv/config';
import express from 'express';
import { HTTPFacilitatorClient } from '@x402/core/server';
import { paymentMiddleware, x402ResourceServer } from '@x402/express';
import { ExactEvmScheme } from '@x402/evm/exact/server';
const required = (name: string) => {
const value = process.env[name];
if (!value) throw new Error(`Missing environment variable: ${name}`);
return value;
};
const apiKey = required('FACILITATOR_API_KEY');
const payTo = required('PAY_TO');
const network = required('NETWORK') as `${string}:${string}`;
const facilitator = new HTTPFacilitatorClient({
url: process.env.FACILITATOR_URL ?? 'https://api.x402go.online',
createAuthHeaders: async () => {
const headers = { 'x-api-key': apiKey };
return { verify: headers, settle: headers, supported: headers };
},
});
const resourceServer = new x402ResourceServer(facilitator).register(network, new ExactEvmScheme());
// Amounts are in the token's smallest unit (10000 = 0.01 for a 6-decimal token).
const price = (amount: string) => ({
amount,
asset: required('ASSET'),
extra: {
name: required('ASSET_NAME'),
version: required('ASSET_VERSION'),
...(process.env.METHOD === 'permit2' ? { assetTransferMethod: 'permit2' } : {}),
},
});
const app = express();
app.use(
paymentMiddleware(
{
'GET /premium': {
accepts: [{ scheme: 'exact', network, payTo, price: price('10000') }],
description: 'Premium content',
mimeType: 'application/json',
},
'GET /reports': {
accepts: [{ scheme: 'exact', network, payTo, price: price('50000') }],
description: 'Detailed reports',
mimeType: 'application/json',
},
},
resourceServer,
),
);
app.get('/premium', (_req, res) => {
res.json({ content: 'This resource requires payment.' });
});
app.get('/reports', (_req, res) => {
res.json({ report: 'Your paid report.' });
});
// Routes not listed in the middleware config stay free.
app.get('/health', (_req, res) => {
res.json({ ok: true });
});
app.listen(3000);