@x402-iota/server-nextjs
v0.1.1
Published
Next.js middleware for x402-iota payments
Maintainers
Readme
@x402-iota/server-nextjs
Next.js middleware for x402 payment gateway on IOTA blockchain.
Overview
Add payment requirements to Next.js API routes with middleware.
Installation
npm install @x402-iota/server-nextjs @x402-iota/coreQuick Start
1. Create middleware.ts
import { NextRequest } from "next/server";
import { createX402Middleware } from "@x402-iota/server-nextjs";
export const middleware = createX402Middleware({
facilitatorUrl: "http://localhost:3001",
payToAddress: "0x1234567890123456789012345678901234567890",
network: "iota-evm-testnet",
asset: "0xFbDa5F676cB37624f28265A144A48B0d6e87d3b6",
routes: {
"/api/protected": {
amount: "1000000",
description: "Access protected API",
},
"/api/premium/*": {
amount: "5000000",
},
},
});
export const config = createX402MatcherConfig([
"/api/protected/:path*",
"/api/premium/:path*",
]);2. Create API Route
// app/api/protected/route.ts
export async function GET(request: NextRequest) {
// Middleware verified payment automatically
const verified = request.headers.get("x-payment-verified");
return Response.json({
message: "Protected data",
verified,
});
}Configuration
interface X402NextjsOptions {
facilitatorUrl: string;
payToAddress: string;
network: Network;
asset: string;
routes: Record<string, RouteConfig>;
onChainVerification?: boolean;
}Middleware Matcher
// middleware.ts
export const config = createX402MatcherConfig([
"/api/protected/:path*",
"/api/premium/:path*",
"/api/admin/*",
]);Request Headers
After verification, middleware adds:
request.headers.get("x-payment-verified"); // 'true'
request.headers.get("x-payment-header"); // Payment headerRelated Packages
- @x402-iota/core - Core types
- @x402-iota/client - Client SDK
- @x402-iota/server-express - Express middleware
License
MIT
