Skip to main content
This guide provides a deep dive into integrating Nevermined. We’ll cover the core components you’ll interact with and the different patterns you can use to add a payment layer to your AI agents.

Core Integration Concepts

Before diving into integration patterns, it’s important to understand the fundamental components of the Nevermined SDK.

1. The Payments Client

The Payments Client is your primary interface for all interactions with the Nevermined protocol. It’s a singleton instance that abstracts away the complexities of blockchain interactions. Key Responsibilities:
  • Agent & Plan Management: Handles the registration and configuration of your agents and their payment plans.
  • Access Validation: Verifies subscriber permissions through bearer token validation.
  • Credit Redemption: Manages the lifecycle of credits, from issuance to consumption.
  • Query Routing: Facilitates communication between subscribers and agents.
Modular Architecture:

2. AI Agents

In Nevermined, an AI Agent is your monetizable service. It’s defined by its metadata, its API endpoints, and the payment plans associated with it. Agent Structure:

3. The Credits System

Credits are the unit of consumption in Nevermined, providing a flexible way to bill for AI services. Credit Lifecycle:
  1. Purchase: A subscriber buys a plan and receives credits.
  2. Validation: On each API call, the system checks if the subscriber has sufficient credits.
  3. Consumption: After a successful request, the specified number of credits is redeemed (burned).
  4. Tracking: The subscriber’s balance is updated in real-time.
  5. Expiration: For time-based plans, access expires after the duration ends.

4. Access Control

Nevermined uses bearer token authentication to secure your agent’s endpoints, ensuring only authorized subscribers can access them. How it Works:
1

Plan Purchase

A subscriber purchases a plan through the SDK or Web App.
2

Token Generation

Upon purchase, the subscriber receives a bearer token via getAgentAccessToken().
3

Request Authentication

The subscriber includes the token in the Authorization header of their requests.
4

Validation

The Nevermined Proxy or your agent validates the token, checks credit balance, and authorizes access.
5

Service Delivery

If validated, your agent processes the request and credits are automatically deducted.

Integration Patterns

Choose the integration pattern that best fits your application’s architecture.

Direct Integration

Integrate the SDK directly into your agent’s code for full control.

Proxy Integration

No code changes - Nevermined Proxy handles payments.

Middleware Pattern

Create a reusable authentication layer for Node.js applications.

Direct Integration

This pattern is best for new services or when you want maximum control over the request lifecycle. Example: Protecting an Express.js Endpoint

Proxy Integration

For existing services that can’t be modified, use the Nevermined Proxy. The proxy handles all payment validation automatically. How It Works:
  1. Register your existing API endpoints with Nevermined
  2. Subscribers access your service through the proxy URL
  3. The proxy validates tokens and manages credits
  4. Your service receives forwarded requests without modification
Example Registration:

Middleware Pattern

For Node.js applications, create reusable middleware to protect multiple routes. Example: Express.js Auth Middleware

Direct Integration

Best for new services or when you want full control over the payment flow.

Step 1: Install Dependencies

Step 2: Initialize Nevermined

Step 3: Create Protected Endpoint

Step 4: Add Health Check

Step 5: Register Your Agent

Middleware Pattern

Create reusable authentication middleware for Express/Node.js applications.

Create Auth Middleware

Use Middleware

Advanced Middleware Features

Proxy Integration

For existing services that can’t be modified, use the Nevermined Proxy.

How It Works

1

Register Your Service

Register your existing endpoints with Nevermined
2

Proxy Handles Payments

All payment validation happens at the proxy layer
3

No Code Changes

Your service continues to work as before

Setup Process

Client Usage

Subscribers access your proxied service:

Error Handling

Implement comprehensive error handling for all integration patterns:

Request Tracking

Track and manage the lifecycle of subscriber requests for analytics, debugging, and compliance purposes.

Request Lifecycle Management

The SDK provides methods to track requests from initialization through completion:

Integration with Request Handler

Add request tracking to your API endpoints:

Analytics and Monitoring

Use request tracking data for insights:

Best Practices for Request Tracking

  • Use async operations to avoid blocking request processing
  • Consider batching updates for high-volume scenarios
  • Implement request sampling for very high traffic
  • Store detailed metadata only when necessary
  • Don’t store sensitive user data in request metadata
  • Implement data retention policies
  • Allow users to request deletion of their tracking data
  • Comply with GDPR and other privacy regulations
  • Always finish requests even if they fail
  • Track different types of failures separately
  • Use request IDs for debugging and support
  • Implement retry logic for tracking failures

Request Tracking Middleware

Create reusable middleware for automatic request tracking:

Testing Your Integration

Unit Tests

Integration Tests

Production Checklist

Before going live:
  • Environment variables configured
  • Error handling implemented
  • Rate limiting in place
  • Logging and monitoring set up
  • Security headers configured
  • SSL/TLS enabled
  • Health check endpoint working
  • Payment flow tested end-to-end
  • Documentation updated

Next Steps

Best Practices

Security and performance recommendations

Go Live

Deploy your integrated service

Examples

See complete implementations

Troubleshooting

Common issues and solutions