In the code below we use “agent” terminology because it maps to the underlying object model, but you can think of it as the service/resource you’re charging for.
Prerequisites
Get Your API Key
- Go to nevermined.app and sign in
- Navigate to Settings > API Keys
- Generate a new key and copy it
Register Your Agent
Create a scrupt to register your agent and a payment plan.You can register your agent and any payment plan using the following code OR using the Nevermined App. So not need to code this, just follow the steps in the app to create your agents and plans.
- TypeScript
- Python
- TypeScript
- Python
Add Payment Validation in the Agent
Add this middleware to your agent to validate if a request is valid (have a payment attached) before delivering your response/resource. The example shows an HTTP API route; the same check can be used before calling an MCP tool handler or before serving a protected asset:- TypeScript
- Python
Integrate the validation code in your agent/server
Integrate the above validation code into your agent/server code at the very beginning of your request handling logic. The payment validation should occur before any processing of the request and can be complemented by any other authorization logic you may have.Test It
1) Try without payment (should fail)- TypeScript
- Python