@aipayagnet/express
v0.1.0
Published
AiPay payment middleware for Express — monetize any API with x402 USDC payments
Maintainers
Readme
@aipay/express
AiPay payment middleware for Express — monetize any API with x402 USDC payments.
Install
npm install @aipay/expressQuick Start
import { paymentMiddleware } from "@aipay/express";
import express from "express";
const app = express();
// Protect any route with a paywall
app.use(
"/api/premium",
paymentMiddleware({
endpointId: "ep_xxx", // Your AiPay endpoint ID
facilitatorUrl: "https://facilitator.aipay.work", // Optional: override facilitator
})
);
app.get("/api/premium/search", (req, res) => {
// req.payment contains verified payment info
res.json({ results: "premium data" });
});
app.listen(3000);How It Works
- When a request hits a paid endpoint without a valid
X-PAYMENTheader, the middleware returns402 Payment Requiredwith x402 payment requirements. - The client pays (USDC on Polygon/Base/Ethereum) and retries with the
X-PAYMENTheader. - The middleware verifies the payment via the facilitator and settles it after a successful response.
API
paymentMiddleware(config)
Returns an Express middleware function.
| Parameter | Type | Description |
|---|---|---|
| config.endpointId | string | Your AiPay endpoint ID (e.g., ep_xxx) |
| config.facilitatorUrl | string? | Override facilitator URL (defaults to https://facilitator.aipay.work) |
License
MIT © AiPay
