cashapi-middleware
v1.1.0
Published
Bitcoin Cash 402 Payment Required Middleware
Maintainers
Readme
cashapi-middleware
Production-ready Express middleware for x402 (Payment Required) on Bitcoin Cash (BCH).
Gated your AI models and APIs with instant 0-conf BCH payments.
Features
- x402 v2 Compliant: Implements the standard
WWW-AuthenticateandAuthorizationheaders. - Real-Time Validation: Uses
mainnet-jsfor robust on-chain 0-conf verification. - Dynamic Pricing: Support for reputation-based pricing and discounts.
- Built-in Discovery: Automatically serves
/.well-known/402.jsonmanifest. - Replay Protection: Prevents double-submission of TXIDs.
Installation
npm install cashapi-middlewareQuick Start (Express)
import express from 'express';
import { cashapiMiddleware } from 'cashapi-middleware';
const app = express();
// Protect your AI route
app.post('/api/ai-chat', cashapiMiddleware({
address: 'bitcoincash:qpm2qavt7wjq73p8u0tmsv55p57m9au48pgshqv6a2',
priceSats: 1000,
network: 'mainnet', // Use 'chipnet' for testing
discovery: {
enabled: true,
name: "My AI Service"
}
}), (req, res) => {
// This code only runs IF payment is verified on-chain
const txId = req.cashapi.txId;
res.json({ result: "AI content unlocked!", payment: txId });
});
app.listen(3000);How It Works (x402 Protocol)
- Request: Client requests
/api/ai-chat. - Challenge: Middleware responds with
402 Payment Requiredand aWWW-Authenticatechallenge containing the address and price. - Payment: Client (using the CashApi SDK) broadcasts a BCH transaction.
- Retry: Client retries the request with
Authorization: x402 <token>:<txid>. - Verified: Middleware verifies the TXID on-chain. If valid, it passes control to your route handler.
License
MIT
