# Monetize Express.js APIs for AI Agents in 3 Lines of Code with x402

> Source: <https://dev.to/ihen404/monetize-expressjs-apis-for-ai-agents-in-3-lines-of-code-with-x402-4i0l>
> Published: 2026-08-29 19:15:50+00:00

AI agents are increasingly making programmatic requests, but traditional billing (credit cards, OAuth, complex API keys) creates friction for automated microtransactions.

With **x402-express**, you can monetize any Express.js endpoint using HTTP 402 payment requirements on Base in 3 lines of code.

bash

npm install x402-express

Add the middleware to any route you want to protect:

javascript

const express = require('express');

const { x402Middleware } = require('x402-express');

const app = express();

app.get('/api/agent-data', x402Middleware({ price: '0.01' }), (req, res) => {

res.json({ message: "Payment verified via x402!" });

});

app.listen(3000, () => console.log('Server running on port 3000'));

`io.github.ihen404/x402-express`

When an unauthenticated agent hits your endpoint, `x402-express`

interceptively returns an `HTTP 402 Payment Required`

payload containing payment parameters, verifying settlements autonomously.
