Solana Payment Channels
Off-chain micropayments with on-chain settlement. Reduce API payment costs by 99.8% while maintaining instant verification and automatic x402 fallback.
10,000 requests: $0.002 vs $10
Instant off-chain validation
Only open and close (vs thousands)
How Payment Channels Work
Payment channels enable unlimited micropayments between two parties with only two on-chain transactions, dramatically reducing costs and latency.
Open Channel
Client deposits funds into on-chain escrow account
1 on-chain transaction (~$0.0005)
Sign Proofs
Client signs off-chain payment proofs for each request
Ed25519 signatures, instant
Verify Instantly
Server validates signatures without blockchain
< 10ms verification time
Claim Payments
Server claims accumulated funds when desired
Batch multiple payments
Close & Refund
Client closes channel and receives unused balance
1 on-chain transaction
Cost Efficiency
Comparison for 10,000 API requests over 1 hour
Slow and expensive at scale
99.8% cost reduction, 400x faster
Framework Integration
Middleware examples for Express, NestJS, and Fastify
Express Middleware
import { channelAuthMiddleware } from '@solana-payment-channel/server';
// Fixed price endpoint
app.get('/api/markets',
channelAuthMiddleware(paymentService, {
amount: 1_000_000n // 1 USDC
}),
(req, res) => res.json({ markets, payment: req.payment })
);
// Dynamic pricing
app.post('/api/process',
channelAuthMiddleware(paymentService, {
amount: async (req) => BigInt(req.body.items.length * 100_000)
}),
handler
);NestJS Guards & Decorators
import { RequirePayment, Payment } from '@solana-payment-channel/server';
@Controller('markets')
export class MarketsController {
@Get('stream')
@RequirePayment(100n) // 0.0001 USDC per request
getStream(@Payment() payment: PaymentResult) {
return {
markets: this.getMarkets(),
paymentMethod: payment.method, // 'channel' or 'x402'
remainingBalance: payment.channelBalance
};
}
// Dynamic pricing
@Post('process')
@RequirePayment((context) => {
const items = context.switchToHttp().getRequest().body.items;
return BigInt(items.length * 100_000);
})
processData() { /* ... */ }
}Fastify Plugin
import { channelPaymentPlugin } from '@solana-payment-channel/server';
// Register plugin
await fastify.register(channelPaymentPlugin, {
rpcUrl: process.env.SOLANA_RPC_URL,
network: 'devnet',
programId: new PublicKey(PROGRAM_ID)
});
// Protected route
fastify.get('/api/premium', {
preHandler: fastify.requirePayment({ amount: 1_000_000n })
}, async (request, reply) => {
return { data: 'premium', payment: request.payment };
});Key Features
Automatic x402 Fallback
Seamlessly falls back to standard x402 when channels aren't optimal
Replay Protection
Nonce-based system prevents payment replay attacks
Ed25519 Signatures
Fast cryptographic verification without blockchain calls
Dispute Resolution
On-chain dispute system protects both parties
Batch Claiming
Server can claim multiple payments in single transaction
Balance Management
Automatic balance tracking and refund handling
NPM Packages
Modular packages for client and server integration
Express, NestJS, and Fastify middleware for payment channel protection
View on NPMClient SDK for making paid requests and managing channel lifecycle
View on NPMIdeal Use Cases
Perfect For
- High-frequency streaming (market data, live scores)
- Real-time updates and interactive applications
- Gaming and multiplayer interactions
- AI/ML inference with per-request pricing
Less Ideal For
- Low-frequency requests (use standard x402)
- Expensive operations (channel setup overhead)
- Sporadic API access (not worth opening channel)
Ready to Reduce Your API Costs by 99.8%?
Start integrating Solana payment channels into your APIs today