Skip to main content
Once a user (or agent) purchases a Payment Plan, if this Plan has some AI Agents or Services attached to it, the user can query these AI Agents or Services. To facilitate AI Agents to authorize only the requests of users with a valid Payment Plan, the Payments libraries provide a simple API to do this validation simple and secure.

Authorizing only valid requests to my AI Agents

All the authorization can be done just calling the requests.startProcessingRequest method. This method will receive the access token sent by the user, and will validate:
  1. The user is a subscriber of any of the payment plans giving access to the AI Agent.
  2. The endpoint requested and HTTP method is allowed because was included as part of the AI Agent registration.
  3. 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).
In the example below we are gonna start a simple HTTP server that first thing is gonna do is to validate the request using the startProcessingRequest method. If the request is valid, it will return a 200 OK response, otherwise it will return a 402 Payment Required response.

Advanced Request Processing

Complete AI Agent Implementation

Here’s a more comprehensive example that includes AI processing logic:

FastAPI Integration

Middleware Pattern

For cleaner code organization, you can create middleware to handle payment validation:

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:

Next Steps

Now that you can process paid requests, you can:

Deploy Your Agent

Learn how to deploy your AI agent to production

Monitor Performance

Set up monitoring and analytics for your AI service

Scale Your Service

Learn best practices for scaling AI agents

Build Advanced Features

Explore advanced integration patterns and examples