@cheapay/x402-express
v0.4.11
Published
HTTP-native Solana payment protocol for APIs. Accept USDC/SOL micropayments with one-line middleware integration.
Readme
@cheapay/x402-express
HTTP-native Solana payment protocol for APIs. Accept USDC/SOL micropayments with one-line middleware integration.
Features
- Solana-first: Designed for Solana payments (USDC/SOL) out of the box
- Express.js middleware for easy integration
- Simple, secure, and scalable micropayment support
- TypeScript support
Installation
npm install @cheapay/x402-express
# or
yarn add @cheapay/x402-express
# or
pnpm add @cheapay/x402-expressQuick Start (Solana)
Accepting SOL (native token)
import express from 'express';
import { paymentMiddleware } from '@cheapay/x402-express';
import { Network } from '@cheapay/x402';
const app = express();
app.use(
'/api/paid',
paymentMiddleware({
chain: 'solana',
recipient: 'YourSolanaAddressHere', // Replace with your Solana address
amount: '0.01', // Amount in SOL
network: Network.MAINNET, // or Network.DEVNET, Network.TESTNET
// currency: 'SOL', // (optional, native SOL is default)
})
);
app.get('/api/paid', (req, res) => {
res.json({ message: 'Payment received! Access granted.' });
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});Accepting USDC (SPL token)
import express from 'express';
import { paymentMiddleware } from '@cheapay/x402-express';
import { Network, TokenMint } from '@cheapay/x402';
const app = express();
app.use(
'/api/paid-usdc',
paymentMiddleware({
chain: 'solana',
recipient: 'YourSolanaAddressHere', // Replace with your Solana address
amount: '0.50', // Amount in USDC
network: Network.MAINNET, // or Network.DEVNET, Network.TESTNET
mint: TokenMint.USDC.mainnet, // Use TokenMint.USDC.devnet for devnet
})
);
app.get('/api/paid-usdc', (req, res) => {
res.json({ message: 'USDC payment received! Access granted.' });
});
app.listen(3001, () => {
console.log('Server running on http://localhost:3001');
});API
paymentMiddleware(options)
chain: Blockchain to use ('solana'recommended)recipient: Solana address to receive paymentsamount: Payment amount (string or number)network: Solana network (useNetwork.MAINNET,Network.DEVNET, orNetwork.TESTNET)mint: (optional) SPL token mint address (e.g.,TokenMint.USDC.mainnet)- ...other advanced options (see TypeScript types)
License
Apache-2.0
