Authorizing only valid requests to my AI Agents
All the authorization can be done just calling therequests.startProcessingRequest method. This method will receive the access token sent by the user, and will validate:
- The user is a subscriber of any of the payment plans giving access to the AI Agent.
- The endpoint requested and HTTP method is allowed because was included as part of the AI Agent registration.
- The user has enough credits to pay for the request (if the AI Agent is using a credit-based Payment Plan) or the payment plan didn’t expire (if it’s a time-based subscription).
startProcessingRequest method. If the request is valid, it will return a 200 OK response, otherwise it will return a 402 Payment Required response.
- TypeScript
- Python
Advanced Request Processing
Complete AI Agent Implementation
Here’s a more comprehensive example that includes AI processing logic:- TypeScript
- Python
Integration with Popular Frameworks
FastAPI Integration
- Python (FastAPI)
Middleware Pattern
For cleaner code organization, you can create middleware to handle payment validation:- TypeScript (Express Middleware)
Best Practices for Request Processing
Validation First
Always validate payments before processing expensive AI operations to avoid wasting resources.
Error Handling
Provide clear error messages and appropriate HTTP status codes for different failure scenarios.
Resource Management
Implement timeouts and resource limits to prevent abuse and ensure fair usage.
Monitoring
Log all requests, validation results, and processing times for analytics and debugging.
Response Patterns
Success Response
Payment Required Response
Error Response
Testing Your Implementation
Create test scripts to verify your payment validation:- TypeScript