from payments_py import Payments, PaymentOptions
from payments_py.common.types import AgentMetadata, AgentAPIAttributes, PlanMetadata
from payments_py.plans import (
get_erc20_price_config,
get_fixed_credits_config,
get_free_price_config
)
# Initialize
payments = Payments.get_instance(
PaymentOptions(nvm_api_key="nvm:your-key", environment="sandbox")
)
ERC20_TOKEN = "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d"
builder_address = payments.account_address
# 1. Create a payment plan first
plan_result = payments.plans.register_credits_plan(
plan_metadata=PlanMetadata(name="AI Agent Plan"),
price_config=get_erc20_price_config(20, ERC20_TOKEN, builder_address),
credits_config=get_fixed_credits_config(100)
)
plan_id = plan_result['planId']
# 2. Register an agent with the plan
agent_result = payments.agents.register_agent(
agent_metadata=AgentMetadata(
name="My First AI Agent",
description="A demo AI agent",
tags=["demo", "ai"]
),
agent_api=AgentAPIAttributes(
endpoints=[{"POST": "https://api.example.com/agents/:agentId/tasks"}],
agent_definition_url="https://api.example.com/openapi.json"
),
payment_plans=[plan_id]
)
agent_id = agent_result['agentId']
print(f"Created agent: {agent_id}")
# 3. Or create both together
combo_result = payments.agents.register_agent_and_plan(
agent_metadata=AgentMetadata(name="Combo Agent"),
agent_api=AgentAPIAttributes(
endpoints=[{"POST": "https://api.example.com/combo"}],
agent_definition_url="https://api.example.com/openapi.json"
),
plan_metadata=PlanMetadata(name="Combo Plan"),
price_config=get_free_price_config(),
credits_config=get_fixed_credits_config(10)
)
print(f"Combo Agent: {combo_result['agentId']}, Plan: {combo_result['planId']}")
# 4. Retrieve agent details
agent = payments.agents.get_agent(agent_id)
print(f"Agent details: {agent}")
# 5. Get associated plans
plans = payments.agents.get_agent_plans(agent_id)
print(f"Agent plans: {plans}")
# 6. Update agent metadata
payments.agents.update_agent_metadata(
agent_id=agent_id,
agent_metadata=AgentMetadata(name="Updated Agent Name"),
agent_api=AgentAPIAttributes(
endpoints=[{"POST": "https://api.example.com/v2/agents/:agentId/tasks"}],
agent_definition_url="https://api.example.com/v2/openapi.json"
)
)