DEV Community

Cover image for Monetize Express.js APIs for AI Agents in 3 Lines of Code with x402
ihen404
ihen404

Posted on

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

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.

1. Installation

bash
npm install x402-express

2. Implementation

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'));

3. Verification & Registry

When an unauthenticated agent hits your endpoint, x402-express interceptively returns an HTTP 402 Payment Required payload containing payment parameters, verifying settlements autonomously.

Top comments (0)