Use this file to discover all available pages before exploring further.
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.
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:
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).
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.
TypeScript
Python
import http from 'http'const agentHost = 'https://example.com' // The AI Agent is running in this hostconst server = http.createServer(async (req, res) => { const authHeader = req.headers['authorization'] as string const requestedUrl = `${agentHost}${req.url}` const httpVerb = req.method console.log('Received request:', { endpoint: requestedUrl, httpVerb, authHeader }) try { const isValidReq = await payments.requests.startProcessingRequest( agentId, authHeader, requestedUrl, httpVerb!, ) console.log('isValidReq', isValidReq) if (isValidReq.balance.isSubscriber) { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ message: 'Hello from the Agent!' })) return } } catch (error) { console.log('Unauthorized access attempt:', authHeader) console.log('Error details:', error) } res.writeHead(402, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ error: 'Payment Required' })) return})server.listen(8889, () => { console.log('AI Agent server running on port 8889')})
from http.server import HTTPServer, BaseHTTPRequestHandlerimport jsonagent_host = "https://example.com" # The AI Agent is running in this hostclass AgentRequestHandler(BaseHTTPRequestHandler): def do_POST(self): self._handle_request() def do_GET(self): self._handle_request() def _handle_request(self): auth_header = self.headers.get("Authorization") requested_url = f"{agent_host}{self.path}" http_verb = self.command print("Received request:", {"endpoint": requested_url, "httpVerb": http_verb, "authHeader": auth_header}) try: is_valid_req = payments.requests.start_processing_request( agent_id, auth_header, requested_url, http_verb ) print("isValidReq", is_valid_req) if is_valid_req["balance"]["isSubscriber"]: self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps({"message": "Hello from the Agent!"}).encode()) return except Exception as error: print("Unauthorized access attempt:", auth_header) print("Error details:", error) self.send_response(402) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps({"error": "Payment Required"}).encode()) return# To start the server:server = HTTPServer(("localhost", 8889), AgentRequestHandler)print("AI Agent server running on port 8889")server.serve_forever()
Here’s a more comprehensive example that includes AI processing logic:
TypeScript
Python
import express from 'express'import { Payments } from '@nevermined-io/payments'const app = express()app.use(express.json())const payments = Payments.getInstance({ nvmApiKey: process.env.NVM_API_KEY!, environment: 'testing'})const agentId = 'your-agent-id'// AI processing function (replace with your actual AI logic)async function processAIRequest(query: string, parameters?: any) { // This is where you'd integrate with your AI model // For example: OpenAI, local models, or other AI services return { response: `AI processed query: ${query}`, timestamp: new Date().toISOString(), parameters }}app.post('/api/v1/agents/:agentId/tasks', async (req, res) => { const authHeader = req.headers['authorization'] const requestedUrl = `${req.protocol}://${req.get('host')}${req.path}` const httpVerb = req.method try { // Validate the request and user subscription const validationResult = await payments.requests.startProcessingRequest( agentId, authHeader, requestedUrl, httpVerb ) if (!validationResult.balance.isSubscriber) { return res.status(402).json({ error: 'Payment Required', message: 'You need an active subscription to access this agent', plans: validationResult.plans || [] }) } // Extract the query from the request const { query, parameters } = req.body if (!query) { return res.status(400).json({ error: 'Missing query parameter' }) } // Process the AI request const aiResponse = await processAIRequest(query, parameters) // Optionally, you can redeem additional credits based on processing complexity // await payments.requests.redeemCredits(planId, additionalCredits, proof) res.json({ success: true, result: aiResponse, creditsRemaining: validationResult.balance.balance }) } catch (error) { console.error('Request processing error:', error) if (error.message.includes('insufficient credits')) { res.status(402).json({ error: 'Insufficient Credits', message: 'You need more credits to access this service' }) } else { res.status(500).json({ error: 'Internal Server Error', message: 'An error occurred while processing your request' }) } }})// Health check endpoint (usually free/open)app.get('/api/v1/health', (req, res) => { res.json({ status: 'healthy', timestamp: new Date().toISOString() })})const PORT = process.env.PORT || 8889app.listen(PORT, () => { console.log(`AI Agent server running on port ${PORT}`)})
from flask import Flask, request, jsonifyfrom payments_py import Paymentsimport osimport jsonfrom datetime import datetimeapp = Flask(__name__)# Initialize payments clientpayments = Payments( api_key=os.environ.get('NVM_API_KEY'), environment='testing')agent_id = 'your-agent-id'def process_ai_request(query, parameters=None): """ AI processing function (replace with your actual AI logic) For example: integrate with OpenAI, local models, or other AI services """ return { 'response': f'AI processed query: {query}', 'timestamp': datetime.now().isoformat(), 'parameters': parameters }@app.route('/api/v1/agents/<agent_id>/tasks', methods=['POST'])def handle_agent_request(agent_id): auth_header = request.headers.get('Authorization') requested_url = request.url http_verb = request.method try: # Validate the request and user subscription validation_result = payments.requests.start_processing_request( agent_id, auth_header, requested_url, http_verb ) if not validation_result.get('balance', {}).get('isSubscriber'): return jsonify({ 'error': 'Payment Required', 'message': 'You need an active subscription to access this agent', 'plans': validation_result.get('plans', []) }), 402 # Extract the query from the request data = request.get_json() query = data.get('query') parameters = data.get('parameters') if not query: return jsonify({ 'error': 'Missing query parameter' }), 400 # Process the AI request ai_response = process_ai_request(query, parameters) return jsonify({ 'success': True, 'result': ai_response, 'creditsRemaining': validation_result.get('balance', {}).get('balance') }) except Exception as error: print(f'Request processing error: {error}') if 'insufficient credits' in str(error): return jsonify({ 'error': 'Insufficient Credits', 'message': 'You need more credits to access this service' }), 402 else: return jsonify({ 'error': 'Internal Server Error', 'message': 'An error occurred while processing your request' }), 500@app.route('/api/v1/health', methods=['GET'])def health_check(): """Health check endpoint (usually free/open)""" return jsonify({ 'status': 'healthy', 'timestamp': datetime.now().isoformat() })if __name__ == '__main__': port = int(os.environ.get('PORT', 8889)) print(f'AI Agent server running on port {port}') app.run(host='0.0.0.0', port=port, debug=True)