nory-x402-middleware
v1.0.0
Published
Add x402 payment paywalls to any API in one line of code. Instant USDC micropayments on Solana.
Maintainers
Readme
nory-x402-middleware
Add x402 payment paywalls to any API in one line of code. Instant USDC micropayments on Solana.
Installation
npm install nory-x402-middlewareQuick Start
Express
import express from 'express';
import { paywall, init } from 'nory-x402-middleware/express';
const app = express();
// Initialize with your wallet address
init({
walletAddress: 'YOUR_SOLANA_WALLET_ADDRESS'
});
// Add a paywall to any route - 1 cent per request
app.get('/api/premium', paywall({ price: '0.01' }), (req, res) => {
// Only runs if payment is verified
console.log('Paid by:', req.payment?.payer);
res.json({ data: 'premium content' });
});
app.listen(3000);Next.js (App Router)
// app/api/premium/route.ts
import { createPaywallHandler, init } from 'nory-x402-middleware/next';
init({ walletAddress: 'YOUR_SOLANA_WALLET_ADDRESS' });
export const GET = createPaywallHandler(
{ price: '0.01' },
async (request, payment) => {
return Response.json({
data: 'premium content',
paidBy: payment.payer
});
}
);Next.js (Pages Router)
// pages/api/premium.ts
import { withPaywall, init } from 'nory-x402-middleware/next';
init({ walletAddress: 'YOUR_SOLANA_WALLET_ADDRESS' });
export default withPaywall({ price: '0.01' }, async (req, res) => {
res.json({
data: 'premium content',
paidBy: req.payment?.payer
});
});Configuration
Environment Variables
NORY_WALLET_ADDRESS=your_solana_wallet_address
NORY_API_KEY=your_api_key # Optional, for analytics
NORY_NETWORK=solana:mainnet # DefaultProgrammatic Configuration
import { init } from 'nory-x402-middleware';
init({
walletAddress: 'YOUR_WALLET',
apiKey: 'YOUR_API_KEY', // Optional
network: 'solana:mainnet', // Default
baseUrl: 'https://noryx402.com', // Default
});Paywall Options
interface PaywallOptions {
/** Price in USD (e.g., '0.01' for 1 cent) */
price: string;
/** Currency (defaults to 'USDC') */
currency?: string;
/** Custom description for the payment */
description?: string;
/** Override wallet address for this endpoint */
walletAddress?: string;
/** Override network for this endpoint */
network?: string;
}How It Works
- When a request comes without payment, the middleware returns HTTP 402 with payment requirements
- The client (AI agent, app, etc.) creates a USDC payment transaction
- The client retries with the payment proof in
X-Paymentheader - The middleware verifies the payment and allows access
- Payment settles on Solana in ~400ms
Response Format
402 Payment Required
{
"error": "Payment Required",
"message": "This endpoint requires a payment of 0.01 USDC",
"price": "0.01",
"currency": "USDC",
"paymentInfo": {
"x402Version": 2,
"accepts": [{
"network": "solana:mainnet",
"amount": "10000",
"currency": "USDC",
"address": "YOUR_WALLET",
"resource": "/api/premium"
}],
"resource": "/api/premium"
}
}Links
- Website: https://noryx402.com
- Docs: https://noryx402.com/docs
- MCP Server: https://www.npmjs.com/package/nory-mcp-server
- x402 Protocol: https://github.com/coinbase/x402
License
MIT
