Menu

Explorer & Settings

Tempo Explorer Submit Project
⬢ Node.js guide

x402 in Node.js

Add x402 payments to a Node API with the official packages — gate an Express (or Hono/Next.js) route behind USDC on the server, and pay endpoints automatically on the client.

How do I use x402 in Node.js?

Install x402-express (server) and x402-axios or x402-fetch (client) from npm. On the server, wrap Express with paymentMiddleware, passing your wallet address and a map of routes to prices; unpaid requests get HTTP 402. On the client, wrap axios or fetch with the matching x402 helper and a funded account — it detects the 402, pays in USDC, and retries automatically.

Which package should you use?

Situation Use Why
Express serverx402-expressDrop-in middleware — builds the 402 body and calls the facilitator for you.
Hono serverx402-honoSame middleware pattern, built for Hono's context object.
Next.js appx402-nextRoute handler/middleware helpers — see the dedicated Next.js guide.
Client already using axiosx402-axiosWraps your existing axios instance, no other client code changes.
Client using native fetchx402-fetchNo extra HTTP client dependency — wraps the global fetch function.

Install

npm install x402-express x402-axios

Server (Express)

The route returns HTTP 402 until a USDC payment is made, then runs normally.

// Express server that charges for a route
import express from "express";
import { paymentMiddleware } from "x402-express";

const app = express();

// Gate routes behind a USDC payment
app.use(paymentMiddleware(
  "0xYourReceivingWalletAddress",
  {
    "GET /weather": {
      price: "$0.01",
      network: "base-sepolia",   // use "base" in production
    },
  },
));

app.get("/weather", (req, res) => {
  // Runs only after a valid payment
  res.json({ city: "Tokyo", forecast: "sunny" });
});

app.listen(3000);

Client (axios)

Wrap the client once and it pays on its own — the same pattern an AI agent uses.

// A Node client that pays automatically
import axios from "axios";
import { withPaymentInterceptor } from "x402-axios";
import { privateKeyToAccount } from "viem/accounts";

const account = privateKeyToAccount("0xYOUR_PRIVATE_KEY");

const api = withPaymentInterceptor(axios.create(), account);

const r = await api.get("http://localhost:3000/weather");
console.log(r.data); // { city: "Tokyo", ... }

Client (native fetch)

No axios dependency? x402-fetch wraps the built-in fetch instead.

// Prefer fetch over axios? x402-fetch wraps the native fetch API instead.
import { wrapFetchWithPayment } from "x402-fetch";
import { privateKeyToAccount } from "viem/accounts";

const account = privateKeyToAccount("0xYOUR_PRIVATE_KEY");
const fetchWithPayment = wrapFetchWithPayment(fetch, account);

const res = await fetchWithPayment("http://localhost:3000/weather");
const data = await res.json(); // { city: "Tokyo", ... }

Server (Hono)

Not on Express? x402-hono applies the identical pattern to Hono routes.

// Same pattern on Hono instead of Express
import { Hono } from "hono";
import { paymentMiddleware } from "x402-hono";

const app = new Hono();

app.use(paymentMiddleware(
  "0xYourReceivingWalletAddress",
  { "GET /weather": { price: "$0.01", network: "base-sepolia" } },
));

app.get("/weather", (c) => c.json({ city: "Tokyo", forecast: "sunny" }));

export default app;

Troubleshooting

Stuck in a 402 retry loop

Why: The interceptor/hooks never actually send a payment — usually a missing or wrong private key, or the client’s network doesn’t match the server’s (one says base, the other base-sepolia).

Fix: Log the raw 402 body once and confirm the network field matches your account’s chain, and that the account holds funds on that network.

"insufficient funds" on a testnet

Why: Your Base Sepolia test wallet has no faucet USDC (or no ETH for gas, depending on the facilitator’s settlement path).

Fix: Fund the address from a Base Sepolia USDC faucet before retrying — this never touches real money.

paymentMiddleware() route key never matches

Why: The route key in the middleware config ("GET /weather") must match the HTTP method and path exactly, including any mount prefix if the router is nested.

Fix: Double-check the method + path string, and log req.method + req.path in the handler to confirm what Express actually sees.

Amount charged looks 1,000,000x too big/small

Why: USDC uses 6 decimals. price strings like "$0.01" are converted for you by paymentMiddleware, but a hand-built accepts body needs an integer maxAmountRequired in base units, not dollars.

Fix: Use the free base-unit converter (linked below) to get the integer right.

Works with axios, breaks with plain fetch

Why: x402-axios and x402-fetch are separate packages with separate APIs — swapping HTTP clients without swapping the matching x402 wrapper leaves requests unpaid.

Fix: Use withPaymentInterceptor from x402-axios for axios, or wrapFetchWithPayment from x402-fetch for native fetch — not the other package’s helper.

Get amounts right with the free base-unit converter.

Real Node/TypeScript x402 projects

From the 671 JavaScript/TypeScript projects we track that reference x402 — stars live from GitHub.

BlockRunAI/ClawRouter

The agent-native LLM router for autonomous agents. 55+ models (8 free), <1ms local routing, USDC payments on Base & Solana via x402.

⭐ 6.7k
coinbase/x402

A payments protocol for the internet. Built on HTTP.

⭐ 6.4k
Bitterbot-AI/bitterbot-desktop

A local-first AI agent with persistent memory, emotional intelligence, and a peer-to-peer skills economy.

⭐ 2.4k
internet-court/internet-court-skill

The trust layer for agent-to-agent commerce — natural-language mandates, ERC-7710 delegated permissions, x402 payments, escrow, and dispute resolution as one open, catch-all Agent Skill / Claude Code plugin.

⭐ 1.1k
BlockRunAI/Franklin

The AI agent with a wallet — spends USDC autonomously to get real work done. Apache-2.0, TypeScript.

⭐ 624
daydreamsai/daydreams

Daydreams is a set of tools for building agents for commerce

⭐ 611

Test for free on Base Sepolia, then set network: "base" for production. Using a framework? See the Next.js guide; for Python, Go and Rust see the SDK guide.

Frequently asked questions

Does x402 have a Node.js SDK?

Yes. The x402 ecosystem ships JavaScript/TypeScript packages including x402-express (server middleware for Express), x402-hono and x402-next for other frameworks, and x402-axios / x402-fetch on the client. Install them from npm and they work in any Node.js project.

How do I add x402 to an Express app?

Install x402-express, then app.use(paymentMiddleware(...)) with your receiving wallet address and a map of routes to prices and networks. Unpaid requests get HTTP 402; paid ones reach your handler. Use network "base-sepolia" to develop for free and "base" in production.

How does a Node client pay an x402 endpoint?

Wrap an axios (or fetch) instance with withPaymentInterceptor (or wrapFetchWithPayment) and a funded account. When the server returns 402, the wrapper reads the payment requirements, signs a USDC payment, and retries automatically — your code just awaits the request and gets the data.

x402-axios or x402-fetch — which client should I use?

Use x402-axios if your project already depends on axios; use x402-fetch if you prefer the native fetch API with no extra HTTP client dependency. Both wrap the same underlying x402 payment flow, so behavior is identical — pick whichever matches your existing code.

Can I use x402 with Next.js or Hono?

Yes. Use x402-next for Next.js route handlers/middleware and x402-hono for Hono apps. They expose the same payment-gating pattern as x402-express. See our Next.js guide for a full example.

Can I test the Node x402 flow without real money?

Yes. Set the network to "base-sepolia" and fund a test wallet from a free faucet. The full 402 → pay → 200 flow behaves exactly like mainnet, so switch to "base" only when going live.

Is x402-express production-ready?

The package is actively maintained by the x402 ecosystem and used in real deployments, but treat it like any payments middleware: pin a version, test the full 402 → pay → settle path on Base Sepolia, and monitor facilitator responses before relying on it for revenue.

More: all SDKs · Python · Go · Rust · examples