Skip to main content

No project description provided

Project description

banner

Library for Activating AI Agent Payments Using the Nevermined Protocol

Python SDK to interact with the Nevermined Payments Protocol
nevermined.io

Motivation

The evolution of AI-native commerce is inevitable, but the infrastructure to support it is currently lacking. Today, AI agents require seamless, automated payment systems for individual transactions. As demand grows, these agents will scale into swarms, transacting and operating autonomously.

Existing solutions are designed for human use with physical money. This does not reflect the new reality, where AI Agents need to make and receive payments quickly and efficiently, without the limitations of traditional payment systems.

Nevermined provides a solution that seamlessly evolves from single-agent needs to complex AI economies, eliminating friction and supporting a fully autonomous, composable future for AI-driven commerce.

What is the Nevermined Payments Library?

The Nevermined Payments Library is a Python SDK that allows AI Builders and Subscribers to make AI Agents available for querying and use by other agents or humans. It is designed to be used alongside the Nevermined protocol, which provides a decentralized infrastructure for managing AI agents and their interactions.

The Payments Library enables:

  • Easy registration and discovery of AI agents and the payment plans required to access them. All agents registered in Nevermined expose their metadata in a generic way, making them searchable and discoverable for specific purposes.
  • Flexible definition of pricing options and how AI agents can be queried. This is achieved through payment plans (based on time or credits) and consumption costs (fixed per request or dynamic). All of this can be defined by the AI builder or agent during the registration process.
  • Subscribers (humans or other agents) to purchase credits that grant access to AI agent services. Payments can be made in crypto or fiat via Stripe integration. The protocol registers the payment and credits distribution settlement on-chain.
  • Agents or users with access credits to query other AI agents. Nevermined authorizes only users with sufficient balance and keeps track of their credit usage.

Initialize the payments library

Payments({"nvm_api_key": nvm_api_key, "environment": "sandbox", "app_id": "your_app_id", "version": "1.0.0"})

A2A Integration (Agents-to-Agents)

The Python SDK can both start an A2A server (FastAPI-based) and act as an A2A client.

What you need to expose an A2A agent with Nevermined

  • Publish an Agent Card at /.well-known/agent.json describing the agent.
  • Declare capabilities.streaming: true if you support message/stream and tasks/resubscribe.
  • Use HTTP auth via headers (e.g., Authorization: Bearer <token>), not inside JSON-RPC payloads.
  • Include a Nevermined payment extension in the Agent Card to enable authorization and credit burning per request.
  • Ensure the Agent Card url matches exactly the endpoint where your A2A server listens.

Nevermined payment extension in the Agent Card

Add the following structure to capabilities.extensions of your Agent Card:

{
  "capabilities": {
    "streaming": true,
    "pushNotifications": true,
    "extensions": [
      {
        "uri": "urn:nevermined:payment",
        "description": "Dynamic cost per request",
        "required": false,
        "params": {
          "paymentType": "dynamic",
          "credits": 1,
          "planId": "<planId>",
          "agentId": "<agentId>"
        }
      }
    ]
  },
  "url": "https://your-agent.example.com/a2a/"
}

Important:

  • The final streaming event of your agent should include metadata.creditsUsed to allow Nevermined to burn credits.
  • The exact URL (including basePath) must match the registration in Nevermined, otherwise validation will fail.

Build a payment-enabled Agent Card

from payments_py.payments import Payments

payments_builder = Payments({
    "nvm_api_key": "<BUILDER_API_KEY>",
    "environment": "staging_sandbox",
})

base_agent_card = {
    "name": "Py A2A Agent",
    "description": "A2A test agent",
    "capabilities": {
        "streaming": True,
        "pushNotifications": True,
        "stateTransitionHistory": True,
    },
    "defaultInputModes": ["text"],
    "defaultOutputModes": ["text"],
    "skills": [],
    "url": "https://your-agent.example.com/a2a/",
    "version": "1.0.0",
}

from payments_py.a2a.agent_card import build_payment_agent_card

agent_card = build_payment_agent_card(base_agent_card, {
    "paymentType": "dynamic",
    "credits": 1,
    "costDescription": "Dynamic cost per request",
    "planId": "<planId>",
    "agentId": "<agentId>",
})

Start an A2A server

from payments_py.a2a.server import PaymentsA2AServer

class DummyExecutor:
    async def execute(self, ctx, event_queue):
        event_queue.publish({
            "kind": "status-update",
            "taskId": ctx.taskId,
            "contextId": ctx.userMessage.get("contextId"),
            "status": {"state": "completed"},
            "metadata": {"creditsUsed": 1},
            "final": True,
        })
        event_queue.finished()

server = PaymentsA2AServer.start(
    agent_card=agent_card,
    executor=DummyExecutor(),
    payments_service=payments_builder,
    port=PORT,
    base_path="/a2a/",
)

Use the A2A client

payments_subscriber = Payments({
    "nvm_api_key": "<SUBSCRIBER_API_KEY>",
    "environment": "staging_sandbox",
})

client = payments_subscriber.a2a.get_client(
    agent_base_url="https://your-agent.example.com/a2a/",
    agent_id="<agentId>",
    plan_id="<planId>",
)

# Send a simple request
result = await client.send_message({
    "message": {"kind": "message", "role": "user", "messageId": "123", "parts": [{"kind": "text", "text": "Hello"}]}
})

# Stream events
async for event in client.send_message_stream({
    "message": {"kind": "message", "role": "user", "messageId": "124", "parts": [{"kind": "text", "text": "Stream"}]}
}):
    if event.get("result", {}).get("final"):
        break

Plans helpers as static methods

You can build price and credits configurations through Payments.plans static helpers, mirroring payments_py/plans.py:

pc = payments_builder.plans.get_erc20_price_config(20, "0x...token...", payments_builder.account_address)
cc = payments_builder.plans.get_fixed_credits_config(100)

Typical API usage (Python)

from payments_py.payments import Payments
from payments_py.common.types import PlanMetadata

payments = Payments({"nvm_api_key": "<KEY>", "environment": "sandbox"})

# Create a plan
plan_metadata = PlanMetadata(name="My Plan")
price = payments.plans.get_erc20_price_config(20, "0x...token...", payments.account_address)
credits = payments.plans.get_fixed_credits_config(100)
plan_res = payments.plans.register_credits_plan(plan_metadata, price, credits)
plan_id = plan_res["planId"]

# Register an agent
agent_res = payments.agents.register_agent(
    {"name": "My Agent"},
    {"endpoints": [{"POST": "https://your-agent.example.com/a2a/"}]},
    [plan_id],
)
agent_id = agent_res["agentId"]

# Order plan (optional - only needed for traditional credit-based access)
payments.plans.order_plan(plan_id)

# Get plan balance
balance = payments.plans.get_plan_balance(plan_id)

# Get x402 access token (for agent-to-agent authentication)
access = payments.x402.get_x402_access_token(plan_id, agent_id)
token = access["accessToken"]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

payments_py-1.0.1.tar.gz (116.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

payments_py-1.0.1-py3-none-any.whl (150.7 kB view details)

Uploaded Python 3

File details

Details for the file payments_py-1.0.1.tar.gz.

File metadata

  • Download URL: payments_py-1.0.1.tar.gz
  • Upload date:
  • Size: 116.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.14.2 Linux/6.11.0-1018-azure

File hashes

Hashes for payments_py-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0c84c818e5bb6aef337d7ebf459349d3e36d25fe424ea04e70bb03bc3d5d4e1a
MD5 407a730f21db635f7d9ff043befd618d
BLAKE2b-256 c6cdb71e4cf8914ab2e68033884ac04b13e26df1b78ad6e972c9b37778c32548

See more details on using hashes here.

File details

Details for the file payments_py-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: payments_py-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 150.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.1 CPython/3.14.2 Linux/6.11.0-1018-azure

File hashes

Hashes for payments_py-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7d23d88be18a3886e57063e80bc7f10141a8e7b81b9f2401acfc099c7a7d3a9c
MD5 57aa04f672e90df0728234bd6332881e
BLAKE2b-256 87b0bb518e37a3827a8b90055fd23f1b41a3a5a71fd7828982df6961a749df2f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page