Best Practices
Guidelines and recommendations for using Shade402 effectively and securely.
Configuration
Environment Variables
Store all sensitive configuration in environment variables:
// .env
PAYMENT_WALLET_ADDRESS=YourWalletAddress
TOKEN_MINT=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
SOLANA_NETWORK=solana-mainnet
SOLANA_RPC_URL=https://api.mainnet-beta.solana.com
WALLET_SECRET_KEY=YourBase64EncodedSecretKeyInitialize Once
Initialize X402 configuration once at application startup:
// config/x402.ts
import { initX402 } from '@shade402/express';
let initialized = false;
export function initializeX402() {
if (initialized) return;
initX402({
paymentAddress: process.env.PAYMENT_WALLET_ADDRESS!,
tokenMint: process.env.TOKEN_MINT!,
network: process.env.SOLANA_NETWORK!,
rpcUrl: process.env.SOLANA_RPC_URL,
autoVerify: true,
});
initialized = true;
}Default Values
Set reasonable defaults but allow overrides:
Payment Amounts
Set Appropriate Amounts
Choose payment amounts based on:
Resource value
Cost of providing the resource
Market rates
User experience
Maximum Payment Limits
Always set maximum payment limits for clients:
Payment Tiers
Implement different payment tiers:
Error Handling
Comprehensive Error Handling
Handle all error types appropriately:
Server-side Error Middleware
Always add error middleware:
Client Management
Always Close Clients
Always close clients to cleanup connections:
Reuse Clients When Possible
For multiple requests, reuse the same client:
Wallet Management
Load wallets securely:
Logging and Monitoring
Log Payment Transactions
Log all payment transactions for audit:
Monitor Payment Metrics
Track payment metrics:
Total payments received
Average payment amount
Payment success rate
Failed payment attempts
Payment expiration rate
Error Monitoring
Monitor errors and set up alerts:
Performance
Connection Pooling
Reuse Solana connections:
Async Operations
Use async/await properly:
Testing
Test with Devnet
Always test with devnet before production:
Test Payment Flows
Test all payment scenarios:
Successful payment
Payment expired
Insufficient funds
Invalid payment
Network errors
Transaction failures
Integration Tests
Write integration tests for payment flows:
Documentation
Code Documentation
Document payment-protected endpoints:
API Documentation
Document payment requirements in API docs:
Payment amounts
Payment expiration
Supported tokens
Network requirements
Error responses
Code Organization
Separate Concerns
Separate payment logic from business logic:
Use Middleware
Use middleware for payment verification:
Summary
Key best practices:
Store sensitive config in environment variables
Initialize X402 once at startup
Set appropriate payment amounts
Always set maximum payment limits
Handle all error types
Always close clients
Log payment transactions
Monitor payment metrics
Test with devnet first
Document payment requirements
Separate payment and business logic
Use middleware for payment verification
Next Steps
Review Security practices
Check out Troubleshooting guide
See Examples for reference implementations
Last updated
