|
| 1 | +# Pricing Configuration - Quick Start Guide |
| 2 | + |
| 3 | +## 5-Minute Overview |
| 4 | + |
| 5 | +The pricing configuration system provides a centralized, validated way to manage all premium endpoint pricing in StellarMind. |
| 6 | + |
| 7 | +### Key Files |
| 8 | + |
| 9 | +| File | Purpose | |
| 10 | +|------|---------| |
| 11 | +| `src/pricing.config.js` | Centralized pricing configuration | |
| 12 | +| `src/pricing.validator.js` | Validation logic | |
| 13 | +| `src/server.js` | Updated to use pricing config | |
| 14 | +| `tests/pricing.validator.test.js` | Unit tests | |
| 15 | +| `tests/pricing.integration.test.js` | Integration tests | |
| 16 | + |
| 17 | +## How It Works |
| 18 | + |
| 19 | +### 1. Configuration |
| 20 | +All pricing is defined in one place: |
| 21 | + |
| 22 | +```javascript |
| 23 | +// src/pricing.config.js |
| 24 | +export const pricingConfig = { |
| 25 | + endpoints: { |
| 26 | + 'GET /api/premium/research': { |
| 27 | + price: '$0.01', |
| 28 | + agent: 'research-bot', |
| 29 | + description: 'Research Agent', |
| 30 | + emoji: '🔬', |
| 31 | + }, |
| 32 | + // ... more endpoints |
| 33 | + }, |
| 34 | +}; |
| 35 | +``` |
| 36 | + |
| 37 | +### 2. Validation |
| 38 | +Configuration is validated at startup: |
| 39 | + |
| 40 | +```javascript |
| 41 | +// src/server.js |
| 42 | +const pricingValidation = validateAll(pricingConfig, { |
| 43 | + network: config.network, |
| 44 | + payTo: config.serverAddress, |
| 45 | +}); |
| 46 | + |
| 47 | +if (!pricingValidation.valid) { |
| 48 | + console.error('❌ FATAL: Pricing configuration validation failed'); |
| 49 | + process.exit(1); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### 3. Usage |
| 54 | +Pricing is used throughout the application: |
| 55 | + |
| 56 | +```javascript |
| 57 | +// Get price for endpoint |
| 58 | +const price = pricingConfig.getPrice('GET /api/premium/research'); |
| 59 | + |
| 60 | +// Generate x402 middleware config |
| 61 | +const x402Config = pricingConfig.getX402Config(config); |
| 62 | + |
| 63 | +// Get all pricing info for status endpoint |
| 64 | +const allPricing = pricingConfig.getAllPricingInfo(); |
| 65 | +``` |
| 66 | + |
| 67 | +## Common Tasks |
| 68 | + |
| 69 | +### Change a Price |
| 70 | + |
| 71 | +**Before:** |
| 72 | +```javascript |
| 73 | +// In src/server.js - HARDCODED |
| 74 | +'GET /api/premium/research': { |
| 75 | + accepts: { |
| 76 | + scheme: 'exact', |
| 77 | + price: '$0.01', // ← Change here |
| 78 | + // ... |
| 79 | + }, |
| 80 | +}, |
| 81 | +``` |
| 82 | + |
| 83 | +**After:** |
| 84 | +```javascript |
| 85 | +// In src/pricing.config.js |
| 86 | +'GET /api/premium/research': { |
| 87 | + price: '$0.02', // ← Change here |
| 88 | + agent: 'research-bot', |
| 89 | + description: 'Research Agent', |
| 90 | + emoji: '🔬', |
| 91 | +}, |
| 92 | +``` |
| 93 | + |
| 94 | +This single change automatically updates: |
| 95 | +- ✅ x402 middleware configuration |
| 96 | +- ✅ Status endpoint output |
| 97 | +- ✅ Broadcast events |
| 98 | +- ✅ All pricing lookups |
| 99 | + |
| 100 | +### Add a New Premium Endpoint |
| 101 | + |
| 102 | +1. Add to `src/pricing.config.js`: |
| 103 | +```javascript |
| 104 | +'GET /api/premium/translate': { |
| 105 | + price: '$0.02', |
| 106 | + agent: 'translator-bot', |
| 107 | + description: 'Translation Agent', |
| 108 | + emoji: '🌐', |
| 109 | +}, |
| 110 | +``` |
| 111 | + |
| 112 | +2. Add endpoint handler in `src/server.js`: |
| 113 | +```javascript |
| 114 | +app.get('/api/premium/translate', async (req, res) => { |
| 115 | + try { |
| 116 | + const text = req.query.text || ''; |
| 117 | + const priceInfo = pricingConfig.getEndpointInfo('GET /api/premium/translate'); |
| 118 | + const cost = priceInfo.price.slice(1); |
| 119 | + broadcast({ type: 'agent_call', agent: `${priceInfo.emoji} Translator Agent`, agentId: 'translator-bot', input: text.substring(0, 100), cost, timestamp: new Date().toISOString() }); |
| 120 | + const result = await runTranslate(text); |
| 121 | + broadcast({ type: 'agent_response', agent: `${priceInfo.emoji} Translator Agent`, agentId: 'translator-bot', resultPreview: result.substring(0, 150), cost, timestamp: new Date().toISOString() }); |
| 122 | + res.json({ agent: 'translator-bot', result, model: MODEL_LABELS.translate, cost: `${cost} USDC`, paidVia: 'x402' }); |
| 123 | + } catch (err) { |
| 124 | + res.status(500).json({ error: 'Translator agent temporarily unavailable', details: err.message }); |
| 125 | + } |
| 126 | +}); |
| 127 | +``` |
| 128 | + |
| 129 | +The x402 middleware will automatically protect this endpoint with the configured price. |
| 130 | + |
| 131 | +### Query Pricing Information |
| 132 | + |
| 133 | +```javascript |
| 134 | +import { pricingConfig } from './src/pricing.config.js'; |
| 135 | + |
| 136 | +// Get price for specific endpoint |
| 137 | +pricingConfig.getPrice('GET /api/premium/research'); |
| 138 | +// '$0.01' |
| 139 | + |
| 140 | +// Get all premium endpoints |
| 141 | +pricingConfig.getPremiumEndpoints(); |
| 142 | +// ['GET /api/premium/research', 'GET /api/premium/summarize', ...] |
| 143 | + |
| 144 | +// Get endpoint info |
| 145 | +pricingConfig.getEndpointInfo('GET /api/premium/research'); |
| 146 | +// { price: '$0.01', agent: 'research-bot', description: '...', emoji: '🔬' } |
| 147 | + |
| 148 | +// Get all pricing info |
| 149 | +pricingConfig.getAllPricingInfo(); |
| 150 | +// [{ endpoint: '...', price: '...', agent: '...', ... }, ...] |
| 151 | + |
| 152 | +// Lookup by agent |
| 153 | +pricingConfig.byAgent['research-bot']; |
| 154 | +// { endpoint: 'GET /api/premium/research', price: '$0.01', ... } |
| 155 | + |
| 156 | +// Lookup by price |
| 157 | +pricingConfig.byPrice['$0.01']; |
| 158 | +// [{ endpoint: 'GET /api/premium/research', agent: 'research-bot' }, ...] |
| 159 | +``` |
| 160 | + |
| 161 | +## Running Tests |
| 162 | + |
| 163 | +### Unit Tests |
| 164 | +```bash |
| 165 | +node tests/pricing.validator.test.js |
| 166 | +``` |
| 167 | + |
| 168 | +**Output:** |
| 169 | +``` |
| 170 | +✅ Price validation tests passed |
| 171 | +✅ Endpoint validation tests passed |
| 172 | +✅ Endpoint info validation tests passed |
| 173 | +✅ Pricing config validation tests passed |
| 174 | +✅ x402 compatibility validation tests passed |
| 175 | +✅ Comprehensive validation tests passed |
| 176 | +✅ Error formatting tests passed |
| 177 | +✅ throwIfInvalid tests passed |
| 178 | +
|
| 179 | +================================================== |
| 180 | +✅ All pricing validator tests passed! |
| 181 | +================================================== |
| 182 | +``` |
| 183 | + |
| 184 | +### Integration Tests |
| 185 | +```bash |
| 186 | +node tests/pricing.integration.test.js |
| 187 | +``` |
| 188 | + |
| 189 | +**Output:** |
| 190 | +``` |
| 191 | +✅ Pricing config structure tests passed |
| 192 | +✅ Pricing consistency tests passed |
| 193 | +✅ Pricing maps tests passed |
| 194 | +✅ Getter methods tests passed |
| 195 | +✅ x402 configuration generation tests passed |
| 196 | +✅ Single source of truth tests passed |
| 197 | +✅ Pricing config validation tests passed |
| 198 | +✅ Price distribution tests passed |
| 199 | +✅ Endpoint naming convention tests passed |
| 200 | +
|
| 201 | +================================================== |
| 202 | +✅ All pricing integration tests passed! |
| 203 | +================================================== |
| 204 | +
|
| 205 | +Pricing Summary: |
| 206 | + Total Endpoints: 4 |
| 207 | + Unique Agents: 4 |
| 208 | + Price Points: 3 |
| 209 | + Total Revenue per Call: $0.10 |
| 210 | +``` |
| 211 | + |
| 212 | +## Price Format Rules |
| 213 | + |
| 214 | +Prices must follow the format: `$X.XX` |
| 215 | + |
| 216 | +✅ Valid prices: |
| 217 | +- `$0.01` - Minimum price |
| 218 | +- `$0.05` - Five cents |
| 219 | +- `$1.00` - One dollar |
| 220 | +- `$99.99` - Maximum reasonable price |
| 221 | + |
| 222 | +❌ Invalid prices: |
| 223 | +- `0.01` - Missing `$` |
| 224 | +- `$0.1` - Only 1 decimal place |
| 225 | +- `$0.001` - 3 decimal places |
| 226 | +- `$-0.01` - Negative price |
| 227 | +- `$0.00` - Zero price |
| 228 | + |
| 229 | +## Endpoint Format Rules |
| 230 | + |
| 231 | +Endpoints must follow the format: `METHOD /api/premium/name` |
| 232 | + |
| 233 | +✅ Valid endpoints: |
| 234 | +- `GET /api/premium/research` |
| 235 | +- `POST /api/premium/analyze` |
| 236 | +- `PUT /api/premium/code` |
| 237 | +- `DELETE /api/premium/summarize` |
| 238 | +- `PATCH /api/premium/translate` |
| 239 | + |
| 240 | +❌ Invalid endpoints: |
| 241 | +- `GET /api/research` - Missing `/premium/` |
| 242 | +- `GET /premium/research` - Missing `/api/` |
| 243 | +- `GET /api/premium/` - Missing name |
| 244 | +- `INVALID /api/premium/research` - Invalid HTTP method |
| 245 | + |
| 246 | +## Agent Name Rules |
| 247 | + |
| 248 | +Agent names must be alphanumeric with hyphens. |
| 249 | + |
| 250 | +✅ Valid agent names: |
| 251 | +- `research-bot` |
| 252 | +- `summary-bot` |
| 253 | +- `analyst-bot` |
| 254 | +- `code-bot` |
| 255 | +- `translator-bot` |
| 256 | + |
| 257 | +❌ Invalid agent names: |
| 258 | +- `research bot` - Contains space |
| 259 | +- `research_bot` - Contains underscore |
| 260 | +- `research.bot` - Contains period |
| 261 | +- `ResearchBot` - Contains uppercase (use lowercase) |
| 262 | + |
| 263 | +## Error Handling |
| 264 | + |
| 265 | +### Validation Fails at Startup |
| 266 | + |
| 267 | +If pricing configuration is invalid, the server will fail to start with a clear error message: |
| 268 | + |
| 269 | +``` |
| 270 | +❌ FATAL: Pricing configuration validation failed |
| 271 | +❌ Pricing configuration validation failed: |
| 272 | +
|
| 273 | +Errors: |
| 274 | + 1. Invalid price for 'GET /api/premium/research': Price must be in format '$X.XX', got '0.01' |
| 275 | + 2. Duplicate agent: 'research-bot' used in multiple endpoints |
| 276 | +``` |
| 277 | + |
| 278 | +### Common Errors |
| 279 | + |
| 280 | +| Error | Solution | |
| 281 | +|-------|----------| |
| 282 | +| `Price must be in format '$X.XX'` | Use `$X.XX` format (e.g., `$0.01`) | |
| 283 | +| `Price cannot be negative` | Use positive price (e.g., `$0.01`) | |
| 284 | +| `Price cannot be zero` | Use minimum price of `$0.01` | |
| 285 | +| `Endpoint must match pattern` | Use `METHOD /api/premium/name` format | |
| 286 | +| `Missing 'agent'` | Add `agent` field with agent name | |
| 287 | +| `Duplicate agent` | Use unique agent names | |
| 288 | +| `Agent name must be alphanumeric` | Use alphanumeric with hyphens (e.g., `research-bot`) | |
| 289 | + |
| 290 | +For more error examples, see `PRICING_ERROR_EXAMPLES.md`. |
| 291 | + |
| 292 | +## Status Endpoint |
| 293 | + |
| 294 | +The `/api/status` endpoint now includes detailed pricing information: |
| 295 | + |
| 296 | +```bash |
| 297 | +curl http://localhost:3001/api/status | jq '.x402.pricing' |
| 298 | +``` |
| 299 | + |
| 300 | +**Output:** |
| 301 | +```json |
| 302 | +[ |
| 303 | + { |
| 304 | + "endpoint": "GET /api/premium/research", |
| 305 | + "price": "$0.01", |
| 306 | + "agent": "research-bot", |
| 307 | + "description": "Research Agent - Web research and information gathering", |
| 308 | + "emoji": "🔬" |
| 309 | + }, |
| 310 | + { |
| 311 | + "endpoint": "GET /api/premium/summarize", |
| 312 | + "price": "$0.01", |
| 313 | + "agent": "summary-bot", |
| 314 | + "description": "Summary Agent - Text summarization and condensing", |
| 315 | + "emoji": "📝" |
| 316 | + }, |
| 317 | + { |
| 318 | + "endpoint": "GET /api/premium/analyze", |
| 319 | + "price": "$0.05", |
| 320 | + "agent": "analyst-bot", |
| 321 | + "description": "Analysis Agent - Deep analysis and insights", |
| 322 | + "emoji": "📊" |
| 323 | + }, |
| 324 | + { |
| 325 | + "endpoint": "GET /api/premium/code", |
| 326 | + "price": "$0.03", |
| 327 | + "agent": "code-bot", |
| 328 | + "description": "Code Agent - Code generation and debugging", |
| 329 | + "emoji": "💻" |
| 330 | + } |
| 331 | +] |
| 332 | +``` |
| 333 | + |
| 334 | +## Best Practices |
| 335 | + |
| 336 | +1. **Always validate before deploying** - Run tests to ensure pricing is valid |
| 337 | +2. **Use consistent price points** - Stick to common prices like $0.01, $0.05, $0.10 |
| 338 | +3. **Document price changes** - Keep a changelog of pricing modifications |
| 339 | +4. **Review pricing regularly** - Ensure prices reflect actual costs |
| 340 | +5. **Test in staging first** - Deploy pricing changes to staging before production |
| 341 | +6. **Monitor pricing consistency** - Check logs for validation errors |
| 342 | +7. **Use version control** - Track pricing changes in git |
| 343 | +8. **Get code review** - Have pricing changes reviewed by another team member |
| 344 | + |
| 345 | +## Troubleshooting |
| 346 | + |
| 347 | +### Server Won't Start |
| 348 | +Check the error message for pricing validation failures: |
| 349 | +```bash |
| 350 | +npm start |
| 351 | +# Look for: ❌ FATAL: Pricing configuration validation failed |
| 352 | +``` |
| 353 | + |
| 354 | +### Pricing Not Updating |
| 355 | +1. Verify you edited `src/pricing.config.js` (not `src/server.js`) |
| 356 | +2. Restart the server |
| 357 | +3. Check `/api/status` endpoint to verify pricing |
| 358 | + |
| 359 | +### Tests Failing |
| 360 | +Run tests to identify issues: |
| 361 | +```bash |
| 362 | +node tests/pricing.validator.test.js |
| 363 | +node tests/pricing.integration.test.js |
| 364 | +``` |
| 365 | + |
| 366 | +## Documentation |
| 367 | + |
| 368 | +- **Full documentation:** `PRICING_REFACTOR.md` |
| 369 | +- **Error examples:** `PRICING_ERROR_EXAMPLES.md` |
| 370 | +- **Refactoring summary:** `REFACTORING_SUMMARY.md` |
| 371 | +- **Unit tests:** `tests/pricing.validator.test.js` |
| 372 | +- **Integration tests:** `tests/pricing.integration.test.js` |
| 373 | + |
| 374 | +## Support |
| 375 | + |
| 376 | +For questions or issues: |
| 377 | +1. Check `PRICING_ERROR_EXAMPLES.md` for common errors |
| 378 | +2. Review `PRICING_REFACTOR.md` for detailed documentation |
| 379 | +3. Run tests to verify configuration |
| 380 | +4. Check server logs for validation errors |
| 381 | + |
| 382 | +## Next Steps |
| 383 | + |
| 384 | +1. ✅ Review pricing configuration in `src/pricing.config.js` |
| 385 | +2. ✅ Run tests to verify everything works |
| 386 | +3. ✅ Deploy to staging environment |
| 387 | +4. ✅ Test pricing in staging |
| 388 | +5. ✅ Deploy to production |
| 389 | +6. ✅ Monitor pricing consistency in logs |
| 390 | +7. ✅ Update API documentation |
| 391 | + |
| 392 | +--- |
| 393 | + |
| 394 | +**Last Updated:** May 26, 2026 |
| 395 | +**Version:** 1.0.0 |
0 commit comments