1. Project Setup
First, let’s set up a new Node.js project.src directory for your source code.
2. Environment Configuration
Create a.env file in your project root to store your credentials and configuration. You’ll need an API key from the Nevermined App.
.env
3. Creating the Agent Server
Now, let’s create a basic Express.js server. This server will have a public/health endpoint and a protected /query endpoint for our AI service.
src/index.ts
4. Implementing Authentication Middleware
This middleware will protect our/query endpoint by validating every incoming request using the Nevermined SDK.
src/middleware.ts
5. Registering the Agent on Nevermined
Your agent needs to be registered with Nevermined to be discoverable and monetizable. Let’s create a separate script for this.src/register.ts
ts-node src/register.ts from your terminal.
6. Redeeming Credits
The final step in the backend is to redeem credits after successfully processing a request. Let’s update our/query endpoint.
src/index.ts (updated)
7. Running and Testing
Add scripts to yourpackage.json to make running your agent easier.
package.json
npm run dev to start your agent server.
Congratulations! You’ve built a simple, monetizable AI agent with Nevermined. You can now use the Quickstart guide for subscribers to test purchasing and accessing your new agent.