Getting Started with Express
Start accepting x402 payments in your Express server in 2 minutes.
Example Code
You can find the full code for this example on GitHub.
Step 1: Install Dependencies
Install the required packages for your Express server:
npm install x402 express dotenv
Or with other package managers:
# pnpm
pnpm add x402 express dotenv
# yarn
yarn add x402 express dotenv
# bun
bun add x402 express dotenv
Step 2: Set Your Environment Variables
Open your generated project's .env and set:
FACILITATOR_URL: Facilitator base URL (defaults to:https://x402.blockeden.xyz)NETWORK: Network to use for the facilitator (default:sui)ADDRESS: Wallet public address to receive payments toBLOCKEDEN_API_KEY: Your BlockEden.xyz API key from dashboard
FACILITATOR_URL=https://x402.blockeden.xyz
NETWORK=sui
ADDRESS=0x... # wallet public address you want to receive payments to
BLOCKEDEN_API_KEY=your_api_key_here
Network Options
BlockEden.xyz supports multiple networks:
sui(recommended for fastest settlement)ethereumbasepolygonavalanche
Step 3: Create Your Express Server
Create an index.ts file with the following code:
import { config } from "dotenv";
import express from "express";
import { paymentMiddleware, Network, Resource } from "x402-express";
config();
const facilitatorUrl = process.env.FACILITATOR_URL as Resource;
const payTo = process.env.ADDRESS as `0x${string}`;
const network = process.env.NETWORK as Network;
const apiKey = process.env.BLOCKEDEN_API_KEY;
if (!facilitatorUrl || !payTo || !apiKey || !network) {
console.error("Missing required environment variables");
process.exit(1);
}
const app = express();
app.use(
paymentMiddleware(
payTo,
{
"GET /weather": {
// USDC amount in dollars
price: "$0.001",
network,
},
"/premium/*": {
// Define atomic amounts in any supported token
price: {
amount: "100000",
asset: {
address: "0xabc",
decimals: 18,
name: "USDC",
},
},
network,
},
},
{
url: facilitatorUrl,
apiKey: apiKey,
},
),
);
app.get("/weather", (req, res) => {
res.send({
report: {
weather: "sunny",
temperature: 70,
},
});
});
app.get("/premium/content", (req, res) => {
res.send({
content: "This is premium content",
});
});
app.listen(4021, () => {
console.log(`Server listening at http://localhost:4021`);
});
Step 4: Run the Server
npx tsx index.ts
Or add a script to your package.json:
{
"scripts": {
"dev": "tsx watch index.ts",
"start": "node dist/index.js"
}
}
Then run:
npm run dev
Step 5: Test the Server
You can test payments against your server locally using HTTP clients like curl, Postman, or by building a client application.
Coming Soon
Client implementation guides for fetch API and axios will be available soon.
Payment Configuration Options
The paymentMiddleware accepts flexible payment configurations:
Simple Dollar Amount
"GET /weather": {
price: "$0.001",
network: "sui",
}
Atomic Token Amount
"/premium/*": {
price: {
amount: "100000",
asset: {
address: "0x...",
decimals: 6,
name: "USDC",
},
},
network: "sui",
}
Route Patterns
You can use wildcards and specific HTTP methods:
{
"GET /weather": { /* ... */ }, // Exact GET route
"/premium/*": { /* ... */ }, // Any method, wildcard path
"POST /api/analyze": { /* ... */ }, // Exact POST route
}