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=YourBase64EncodedSecretKey

Initialize 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:

  1. Store sensitive config in environment variables

  2. Initialize X402 once at startup

  3. Set appropriate payment amounts

  4. Always set maximum payment limits

  5. Handle all error types

  6. Always close clients

  7. Log payment transactions

  8. Monitor payment metrics

  9. Test with devnet first

  10. Document payment requirements

  11. Separate payment and business logic

  12. Use middleware for payment verification

Next Steps

Last updated