Actix Web Server Example¶
Complete example of an X402-enabled API server using Actix Web with multiple pricing tiers.
Source Code¶
Location: examples/rust/actix-server/
Overview¶
This example demonstrates: - Free and paid endpoints - Multiple pricing tiers ($0.01, $0.10, $1.00) - Payment requirement configuration - Health check endpoint
Running the Example¶
Server starts on http://localhost:8080
Endpoints¶
| Endpoint | Price | Description |
|---|---|---|
GET / |
Free | Welcome message |
GET /basic |
$0.01 | Basic tier data |
GET /premium |
$0.10 | Premium tier data |
GET /enterprise |
$1.00 | Enterprise tier data |
GET /health |
Free | Health check |
Testing¶
Free Endpoint¶
Response:
Payment Required¶
Response (402 Payment Required):
Complete Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
Configuration¶
Update the wallet addresses in the code:
Using with Client¶
Test with the auto client:
Key Features Demonstrated¶
- Multiple Pricing Tiers - Different prices for different data levels
- Payment Descriptions - Human-readable descriptions in payment requests
- Custom Expirations - Premium tier has 10-minute expiration
- Optional Extractors - Using
Option<PaymentExtractor>for flexible handling - Health Check - Free endpoint for monitoring
- Async Handlers - All handlers are async for maximum performance
Performance Considerations¶
Actix Web is known for excellent performance: - Async I/O with Tokio - Efficient request routing - Low overhead middleware - Connection pooling
Next Steps¶
- Modify pricing tiers
- Add custom business logic
- Integrate with database
- Add rate limiting
- Deploy to production
- Add middleware for logging/metrics