Express.js Server Example with X402 Payment Support¶
This example demonstrates how to create an Express.js API server with X402 payment requirements for specific endpoints.
Features¶
- Free endpoints - Public access without payment
- Premium endpoints - Require payment to access
- Automatic payment verification - Built-in Solana blockchain verification
- Flexible pricing - Different amounts for different endpoints
- TypeScript - Full type safety
Setup¶
1. Install Dependencies¶
2. Configure Environment¶
Create a .env file based on .env.example:
Edit .env with your Solana wallet details:
3. Build and Run¶
The server will start on http://localhost:3000.
API Endpoints¶
Free Endpoints¶
- GET / - API information (no payment)
- GET /free-data - Free data access (no payment)
Paid Endpoints¶
- GET /premium-data - Premium data (0.10 USDC)
- GET /expensive-data - AI inference (1.00 USDC)
- GET /tiered-data/:tier - Tiered content (0.05 USDC)
- POST /process-data - Data processing (0.25 USDC)
Usage Examples¶
Free Endpoint¶
Response:
Paid Endpoint (Without Payment)¶
Response (402 Payment Required): ```json { "max_amount_required": "0.10", "asset_type": "SPL", "asset_address": "DEMO_USDC_MINT", "payment_address": "DEMO_WALLET_ADDRESS", "network": "solana-devnet", "expires_at": "2025-01-01T10:05:00Z", "nonce": "...", "payment_id": "...",