x402 facilitator
Quickstart
A paid API route on Hedera testnet in about five minutes.
1. Install
npm install hono @hono/node-server @x402/hono @x402/core @x402/hedera2. Add a paid route
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { paymentMiddleware } from "@x402/hono";
import { x402ResourceServer, HTTPFacilitatorClient } from "@x402/core/server";
import { ExactHederaScheme } from "@x402/hedera/exact/server";
const facilitator = new HTTPFacilitatorClient({ url: "https://testnet.facilitator.amnt.io" });
const server = new x402ResourceServer(facilitator).register("hedera:testnet", new ExactHederaScheme());
const app = new Hono();
app.use(
paymentMiddleware(
{
"GET /weather": {
accepts: { scheme: "exact", network: "hedera:testnet", price: "$0.001", payTo: "0.0.YOUR_ACCOUNT" },
description: "Today's weather",
},
},
server,
),
);
app.get("/weather", (c) => c.json({ sunny: true }));
serve({ fetch: app.fetch, port: 3000 });payTo is the Hedera testnet account that receives the USDC. Get one free at portal.hedera.com.
3. Check it asks for payment
curl -i http://localhost:3000/weatherYou get 402 Payment Required with a PAYMENT-REQUIRED header. Inside it, extra.feePayer is our account: that's who pays the network fee.
4. Pay for it
Any x402 client works. With @x402/fetch:
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { ExactHederaScheme } from "@x402/hedera/exact/client";
import { createClientHederaSigner, PrivateKey } from "@x402/hedera";
const signer = createClientHederaSigner(
"0.0.BUYER",
PrivateKey.fromStringECDSA(process.env.BUYER_KEY!),
{ network: "hedera:testnet" },
);
const client = new x402Client().register("hedera:testnet", new ExactHederaScheme(signer));
const pay = wrapFetchWithPayment(fetch, client);
const res = await pay("http://localhost:3000/weather");
console.log(await res.json());The buyer needs the token your route asks for. Circle's faucet doesn't cover Hedera testnet, so for a first try create a test token with the Hedera SDK and price the route in it.
5. See it settle
Your payment appears on the testnet dashboard within seconds, with a HashScan link.
Going live
Change two things: the URL to https://facilitator.amnt.io, and the network to hedera:mainnet.