Express Quickstart
Set up an x402-protected resource server with Node.js Express.
Step 1: Install dependencies
npm install express dotenv x402-xrplStep 2: Set your environment variables
Create a .env file with your configuration:
XRPL_FACILITATOR_URL=http://127.0.0.1:8011 XRPL_PAY_TO=rha... # your wallet address to receive funds XRPL_PRICE_DROPS=1000 # price in drops (1 XRP = 1,000,000 drops) XRPL_NETWORK=xrpl:1 PORT=8080
Step 3: Create your server
Create a server.js file (if you're using ESM imports, set "type": "module" in your package.json or use .mjs).
import dotenv from "dotenv";
import express from "express";
import { requirePayment } from "x402-xrpl/express";
dotenv.config({ override: false });
const payToAddress = process.env.XRPL_PAY_TO;
if (!payToAddress) {
throw new Error("XRPL_PAY_TO is required");
}
const facilitatorUrl = process.env.XRPL_FACILITATOR_URL ?? "http://127.0.0.1:8011";
const network = process.env.XRPL_NETWORK ?? "xrpl:1";
const priceDrops = process.env.XRPL_PRICE_DROPS ?? "1000";
const port = Number(process.env.PORT ?? "8080");
const app = express();
// Protect /hello with XRPL x402 middleware (facilitator does verify+settle by default).
app.use(
requirePayment({
path: "/hello",
price: priceDrops,
payToAddress,
network,
facilitatorUrl,
asset: "XRP",
resource: "demo:hello-world",
description: "Hello World Demo (paid)",
}),
);
app.get("/hello", (_req, res) => {
res.json({ message: "Hello World! Thanks for the payment." });
});
// Free endpoint
app.get("/health", (_req, res) => {
res.json({ status: "ok" });
});
app.listen(port, () => {
// eslint-disable-next-line no-console
console.log(`listening on http://127.0.0.1:${port}`);
});Step 4: Run the server
node server.jsTry visiting http://localhost:8080/hello. You will receive a 402 Payment Required response, which your client can use to pay.