nexuspay-x402
v1.0.0
Published
NexusPay SDK for x402 - Add payments to any API in 3 lines
Maintainers
Readme
nexuspay-x402
NexusPay for x402 - add payments to any API in 3 lines.
Install
npm install nexuspay-x402Express Usage
import nexuspay from 'nexuspay-x402';
app.use(nexuspay({
apiKey: 'np_your_key',
price: '0.001',
payTo: '0xYourWallet'
}));Next.js Usage
import { nexuspayNext } from 'nexuspay-x402';
export async function GET(request: Request) {
const check = await nexuspayNext(request, {
apiKey: 'np_your_key',
price: '0.001',
payTo: '0xYourWallet'
});
if (check) return check;
// Your API logic here
return Response.json({ data: 'Protected content' });
}How it works
- Request comes in
- No payment? → 402 returned automatically
- Payment header found? → Verified on Base Sepolia
- Verified? → API access granted
Built on Base Sepolia. Payments in USDC.
Features
- ✅ 3-line integration
- ✅ On-chain payment verification
- ✅ ERC-20 Transfer event decoding
- ✅ Works with Express and Next.js
- ✅ AI agent compatible
- ✅ TypeScript support
Configuration
interface NexusPayConfig {
apiKey?: string; // Optional API key for app-side tracking
price: string; // Price in USDC (e.g., '0.001')
payTo: string; // Your wallet address
token?: string; // Token address (default: USDC)
network?: string; // Network (default: 'base-sepolia')
enabled?: boolean; // Set false to bypass checks in local dev
rpcUrl?: string; // Optional custom RPC URL
}Payment Header Format
Clients must include payment proof in the X-Payment header:
X-Payment: txHash=0x...;from=0x...;to=0x...;amount=0.001;token=0x...;network=base-sepoliaExample: Protected API
import express from 'express';
import nexuspay from 'nexuspay-x402';
const app = express();
// Protect this route
app.get('/api/data',
nexuspay({
apiKey: 'np_your_key',
price: '0.001',
payTo: '0xA1B320D8061357efa286Af2629DF6AC554C05d6E'
}),
(req, res) => {
res.json({ data: 'This is protected content!' });
}
);
app.listen(3000);Example: AI Agent Client
// 1. Call API
const response = await fetch('https://api.example.com/data');
// 2. Got 402? Pay with USDC
if (response.status === 402) {
const { payment } = await response.json();
// Send USDC on Base Sepolia
const txHash = await sendUSDC(payment.payTo, payment.amount);
// 3. Retry with payment proof
const response2 = await fetch('https://api.example.com/data', {
headers: {
'X-Payment': `txHash=${txHash};from=${myAddress};to=${payment.payTo};amount=${payment.amount};token=${payment.token};network=base-sepolia`
}
});
const data = await response2.json();
}Network Details
- Network: Base Sepolia (testnet)
- Chain ID: 84532
- USDC Contract:
0x036CbD53842c5426634e7929541eC2318f3dCF7e - RPC:
https://sepolia.base.org
Get Test USDC
Visit: https://www.coinbase.com/faucets/base-ethereum-sepolia-faucet
License
MIT
Backward Compatibility
nexuspay-x402 also exports paygate and paygateNext aliases so older integrations can migrate gradually.
Links
- x402 Protocol: https://github.com/coinbase/x402
- NexusPay Docs: https://github.com/dhruvxop19/nexuspay
Built with the x402 protocol by Coinbase.
