@merit-systems/x402-middleware
v0.0.1
Published
Merit payment middleware for Express, Hono, and Next.js
Readme
x402-middleware
X402 Merit payment middleware factory for Express, Hono, and Next.js applications. Automatically resolves repository addresses from GitHub and deploys repo contracts if needed.
Installation
npm install x402-middleware
# or
pnpm add x402-middlewareYou'll also need the x402 middleware for your framework:
- Express:
npm install x402-express - Hono:
npm install x402-hono - Next.js:
npm install x402-next
Usage
With Hono
import { meritPaymentMiddleware } from 'x402-middleware';
import { paymentMiddleware } from 'x402-hono';
import { Hono } from 'hono';
import type { Config } from 'x402-middleware/types';
import type { Address } from 'viem';
const app = new Hono();
const config: Config = {
repoIdentifier: "github:owner/repo",
repoFactoryAddress: "0x..." as Address,
rpcUrl: "https://..." as RpcUrl,
privateKey: "0x..." as PrivateKey,
};
app.use(meritPaymentMiddleware(
config,
paymentMiddleware,
{
"/": {
price: "$0.01",
network: "base",
config: {
description: "Pay for this repo",
}
}
}
));With Express
import { meritPaymentMiddleware } from 'x402-middleware';
import { paymentMiddleware } from 'x402-express';
import express from 'express';
const app = express();
app.use(meritPaymentMiddleware(
config,
paymentMiddleware,
routes
));API
meritPaymentMiddleware(config, paymentMiddlewareFactory, routes, facilitator?, paywall?)
Creates a repo-aware payment middleware that automatically:
- Resolves the repository ID from GitHub
- Gets or deploys the repo contract
- Wraps the payment middleware with the repo address
Parameters:
config: Configuration objectrepoIdentifier: Repository identifier in formatgithub:owner/repoorgithub:owner/repo:instanceIdrepoFactoryAddress: Smart contract address for the repo factoryrpcUrl: RPC endpoint URLprivateKey: Private key for deploying contracts
paymentMiddlewareFactory: Framework-specific payment middleware function (paymentMiddlewarefrom x402-express, x402-hono, or x402-next)routes: Route configuration object with pricingfacilitator?: Optional facilitator configurationpaywall?: Optional paywall configuration
Returns: Async middleware function compatible with your framework
How it Works
- On first request, resolves your GitHub repo to get its repo ID
- Checks if a repo contract is deployed for that ID
- If not deployed, automatically deploys it
- Passes the repo address to the payment middleware
- All payments go to the repo's smart contract address
