Getting started with Hono
Start accepting 402 payments in your Hono server in 2 minutes.
You can find the full code for this example
here.
Step 1: Install dependencies
npm install x402-hono hono dotenv @hono/node-server
Step 2: Set your environment variables
echo "ADDRESS=0x...\nFACILITATOR_URL=https://facilitator.PalPaxAI.network\nNETWORK=solana-devnet" > .env
Your .env file should look like this:
ADDRESS=0x... # the wallet address you will receive payments on, could be evm or solana
FACILITATOR_URL=https://facilitator.PalPaxAI.network
NETWORK=solana-devnet # or base-sepolia, avalanche, etc.
Step 3: Create a new Hono app
import { config } from "dotenv";
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { paymentMiddleware, Network, Resource } from "x402-hono";
config();
const facilitatorUrl = process.env.FACILITATOR_URL as Resource;
const payTo = process.env.ADDRESS as `0x${string}`;
const network = process.env.NETWORK as Network;
if (!facilitatorUrl || !payTo || !network) {
console.error("Missing required environment variables");
process.exit(1);
}
const app = new Hono();
console.log("Server is running");
app.use(
paymentMiddleware(
payTo,
{
"/weather": {
price: "$0.001",
network,
},
},
{
url: facilitatorUrl,
},
),
);
app.get("/weather", c => {
return c.json({
report: {
weather: "sunny",
temperature: 70,
},
});
});
serve({
fetch: app.fetch,
port: 4021,
});
Step 4: Run the server
Your server is now accepting x402 payments!
Step 5: Test the server
You can test payments against your server locally by following the fetch example or the axios example.
Just set your environment variables to match your local server, install the dependencies, and run the examples.
Need help?