Payments Class Reference¶
The main entry point for the Nevermined Payments Python SDK.
Payments¶
Payments
¶
Bases: BasePaymentsAPI
Main class that interacts with the Nevermined payments API.
Use Payments.get_instance for server-side usage.
The library provides methods to manage AI Agents, Plans & process AI Agent Requests.
Each of these functionalities is encapsulated in its own API class:
- plans: Manages AI Plans, including registration and ordering and retrieving plan details.
- orders: Browser-fiat Orders - merchant-initiated, off-plan charges for an arbitrary amount.
- agents: Handles AI Agents, including registration of AI Agents and access token generation.
- requests: Manages requests received by AI Agents, including validation and tracking.
- facilitator: Handles X402 permission verification and settlement for AI Agents acting as facilitators.
Initialize the Payments class.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
The initialization options
TYPE:
|
is_browser_instance
|
Whether this is a browser instance (default False)
TYPE:
|
Source code in payments_py/payments.py
get_instance
classmethod
¶
Get an instance of the Payments class for server-side usage.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
The options to initialize the payments class
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Payments
|
An instance of Payments |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If nvm_api_key is missing |
Source code in payments_py/payments.py
get_browser_instance
classmethod
¶
Get an instance of the Payments class for browser usage.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
The options to initialize the payments class
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Payments
|
An instance of Payments |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If return_url is missing |
Source code in payments_py/payments.py
Plans API¶
PlansAPI
¶
Bases: BasePaymentsAPI
The PlansAPI class provides methods to register and interact with payment plans on Nevermined.
Initialize the PlansAPI class.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
The options to initialize the payments class
TYPE:
|
Source code in payments_py/api/plans_api.py
get_instance
classmethod
¶
Get a singleton instance of the PlansAPI class.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
The options to initialize the payments class
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PlansAPI
|
The instance of the PlansAPI class |
Source code in payments_py/api/plans_api.py
register_plan
¶
register_plan(
plan_metadata: PlanMetadata,
price_config: PlanPriceConfig,
credits_config: PlanCreditsConfig,
nonce: Optional[int] = None,
access_limit: Optional[
Literal["credits", "time"]
] = None,
organization_id: Optional[str] = None,
) -> Dict[str, str]
Allows an AI Builder to create a Payment Plan on Nevermined in a flexible manner. A Nevermined Credits Plan limits access based on plan usage. With them, AI Builders control the number of requests that can be made to an agent or service. Every time a user accesses any resource associated with the Payment Plan, the usage consumes from a capped amount of credits. When the user consumes all the credits, the plan automatically expires and the user needs to top up to continue using the service.
| PARAMETER | DESCRIPTION |
|---|---|
plan_metadata
|
Plan metadata
TYPE:
|
price_config
|
Plan price configuration
TYPE:
|
credits_config
|
Plan credits configuration
TYPE:
|
nonce
|
Optional nonce for the transaction
TYPE:
|
access_limit
|
Optional access limit for the plan
TYPE:
|
organization_id
|
Optional org id (e.g.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, str]
|
The unique identifier of the plan (Plan ID) of the newly created plan |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If registration fails |
Source code in payments_py/api/plans_api.py
register_credits_plan
¶
register_credits_plan(
plan_metadata: PlanMetadata,
price_config: PlanPriceConfig,
credits_config: PlanCreditsConfig,
organization_id: Optional[str] = None,
) -> Dict[str, str]
Allows an AI Builder to create a Payment Plan on Nevermined based on Credits. A Nevermined Credits Plan limits the access by the access/usage of the Plan. With them, AI Builders control the number of requests that can be made to an agent or service. Every time a user accesses any resource associated with the Payment Plan, the usage consumes from a capped amount of credits. When the user consumes all the credits, the plan automatically expires and the user needs to top up to continue using the service.
| PARAMETER | DESCRIPTION |
|---|---|
plan_metadata
|
Plan metadata
TYPE:
|
price_config
|
Plan price configuration
TYPE:
|
credits_config
|
Plan credits configuration
TYPE:
|
organization_id
|
Optional org id to publish into for this call.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, str]
|
The unique identifier of the plan (Plan ID) of the newly created plan |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the credits configuration is invalid |
Source code in payments_py/api/plans_api.py
register_time_plan
¶
register_time_plan(
plan_metadata: PlanMetadata,
price_config: PlanPriceConfig,
credits_config: PlanCreditsConfig,
organization_id: Optional[str] = None,
) -> Dict[str, str]
Allows an AI Builder to create a Payment Plan on Nevermined limited by duration. A Nevermined Credits Plan limits the access by the access/usage of the Plan. With them, AI Builders control the number of requests that can be made to an agent or service. Every time a user accesses any resource associated with the Payment Plan, the usage consumes from a capped amount of credits. When the user consumes all the credits, the plan automatically expires and the user needs to top up to continue using the service.
| PARAMETER | DESCRIPTION |
|---|---|
plan_metadata
|
Plan metadata
TYPE:
|
price_config
|
Plan price configuration
TYPE:
|
credits_config
|
Plan credits configuration
TYPE:
|
organization_id
|
Optional org id to publish into for this call.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, str]
|
The unique identifier of the plan (Plan ID) of the newly created plan |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the credits configuration is invalid |
Source code in payments_py/api/plans_api.py
register_credits_trial_plan
¶
register_credits_trial_plan(
plan_metadata: PlanMetadata,
price_config: PlanPriceConfig,
credits_config: PlanCreditsConfig,
organization_id: Optional[str] = None,
) -> Dict[str, str]
Allows an AI Builder to create a Trial Payment Plan on Nevermined based on Credits. A Nevermined Trial Plan allow subscribers of that plan to test the Agents associated to it. A Trial plan is a plan that only can be purchased once by a user.
| PARAMETER | DESCRIPTION |
|---|---|
plan_metadata
|
Plan metadata
TYPE:
|
price_config
|
Plan price configuration
TYPE:
|
credits_config
|
Plan credits configuration
TYPE:
|
organization_id
|
Optional org id to publish into for this call.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, str]
|
The unique identifier of the plan (Plan ID) of the newly created plan |
Source code in payments_py/api/plans_api.py
register_time_trial_plan
¶
register_time_trial_plan(
plan_metadata: PlanMetadata,
price_config: PlanPriceConfig,
credits_config: PlanCreditsConfig,
organization_id: Optional[str] = None,
) -> Dict[str, str]
Allows an AI Builder to create a Trial Payment Plan on Nevermined limited by duration. A Nevermined Trial Plan allow subscribers of that plan to test the Agents associated to it. A Trial plan is a plan that only can be purchased once by a user.
| PARAMETER | DESCRIPTION |
|---|---|
plan_metadata
|
Plan metadata
TYPE:
|
price_config
|
Plan price configuration
TYPE:
|
credits_config
|
Plan credits configuration
TYPE:
|
organization_id
|
Optional org id to publish into for this call.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, str]
|
The unique identifier of the plan (Plan ID) of the newly created plan |
Source code in payments_py/api/plans_api.py
get_plan
¶
Get the metadata for a given Plan identifier.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the plan
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The plan's metadata |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the plan is not found |
Source code in payments_py/api/plans_api.py
get_user_plans
¶
get_user_plans(
org_id: Optional[str] = None,
pagination: Optional[PaginationOptions] = None,
) -> Dict[str, Any]
List the payment plans published by the current user.
Returns only the plans the authenticated account created — this is
account management ("my plans"), not a marketplace search, and never
returns other users' plans. Pass org_id to list every plan in an
organization you are a member of instead.
Analogous to plans.getPlans() in the TypeScript SDK; this method
also accepts org_id to scope the listing to an organization.
| PARAMETER | DESCRIPTION |
|---|---|
org_id
|
Optional organization id. When set, returns every plan in that organization (the backend rejects orgs you are not an active member of); when omitted, returns the plans authored by the caller.
TYPE:
|
pagination
|
Optional pagination options (page, offset, sort_by, sort_order).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
A paginated result of the shape |
Dict[str, Any]
|
|
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the request fails. |
Source code in payments_py/api/plans_api.py
get_plan_balance
¶
Get the balance of a plan for a specific account.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the plan
TYPE:
|
account_address
|
The account address to check balance for (defaults to current user)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The plan balance information with properly typed fields (balance as int) |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to get plan balance |
Source code in payments_py/api/plans_api.py
order_plan
¶
Order a plan by its ID.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The ID of the plan to order
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, bool]
|
The result of the order operation |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to order the plan |
Source code in payments_py/api/plans_api.py
mint_plan_credits
¶
Mint credits for a plan.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the plan
TYPE:
|
credits_amount
|
The amount of credits to mint
TYPE:
|
credits_receiver
|
The address that will receive the credits
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The result of the mint operation |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to mint credits |
Source code in payments_py/api/plans_api.py
mint_plan_expirable
¶
mint_plan_expirable(
plan_id: str,
credits_amount: int,
credits_receiver: str,
credits_duration: int = 0,
) -> Dict[str, Any]
Mint expirable credits for a plan.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the plan
TYPE:
|
credits_amount
|
The amount of credits to mint
TYPE:
|
credits_receiver
|
The address that will receive the credits
TYPE:
|
credits_duration
|
The duration of the credits in seconds
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The result of the mint operation |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to mint credits |
Source code in payments_py/api/plans_api.py
burn_credits
¶
Burn credits from a plan.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the plan
TYPE:
|
credits_amount
|
The amount of credits to burn
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The result of the burn operation |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to burn credits |
Source code in payments_py/api/plans_api.py
get_agents_associated_to_plan
¶
get_agents_associated_to_plan(
plan_id: str,
pagination: Optional[PaginationOptions] = None,
) -> Dict[str, Any]
Gets the list of agents that can be accessed with a plan.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the plan
TYPE:
|
pagination
|
Optional pagination options to control the number of results returned
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The list of all different agents giving access to the plan |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the plan is not found |
Source code in payments_py/api/plans_api.py
get_fiat_price_config
staticmethod
¶
Build a fiat price configuration.
get_eurc_price_config
staticmethod
¶
get_eurc_price_config(
amount: int,
receiver: str,
eurc_address: str = plan_utils.EURC_TOKEN_ADDRESS,
) -> PlanPriceConfig
Build an EURC (Euro stablecoin) price configuration.
Source code in payments_py/api/plans_api.py
get_crypto_price_config
staticmethod
¶
get_crypto_price_config(
amount: int,
receiver: str,
token_address: str = "0x0000000000000000000000000000000000000000",
) -> PlanPriceConfig
Build a crypto (native/ERC20) price configuration.
Source code in payments_py/api/plans_api.py
get_erc20_price_config
staticmethod
¶
Build an ERC20 price configuration.
get_free_price_config
staticmethod
¶
get_native_token_price_config
staticmethod
¶
Build a native token price configuration.
get_expirable_duration_config
staticmethod
¶
Build an expirable duration credits configuration.
get_non_expirable_duration_config
staticmethod
¶
Build a non-expirable duration credits configuration.
get_fixed_credits_config
staticmethod
¶
Build a fixed credits configuration.
Source code in payments_py/api/plans_api.py
get_dynamic_credits_config
staticmethod
¶
get_dynamic_credits_config(
credits_granted: int,
min_credits_per_request: int = 1,
max_credits_per_request: int = 1,
) -> PlanCreditsConfig
Build a dynamic credits configuration.
Source code in payments_py/api/plans_api.py
set_redemption_type
staticmethod
¶
set_redemption_type(
credits_config: PlanCreditsConfig,
redemption_type: PlanRedemptionType,
) -> PlanCreditsConfig
Set redemption type on a credits configuration (returns new object).
Source code in payments_py/api/plans_api.py
set_onchain_mirror
staticmethod
¶
set_onchain_mirror(
credits_config: PlanCreditsConfig,
onchain_mirror: bool = True,
) -> PlanCreditsConfig
Set the on-chain mirror flag on a credits configuration (returns new object).
Source code in payments_py/api/plans_api.py
get_pay_as_you_go_price_config
¶
get_pay_as_you_go_price_config(
amount: int,
receiver: str,
token_address: str = plan_utils.ZeroAddress,
) -> PlanPriceConfig
Build a pay-as-you-go price configuration using contract address from API.
This method fetches the PayAsYouGoTemplate contract address from the API info endpoint and uses it to create the price configuration. The address is cached for subsequent calls.
| PARAMETER | DESCRIPTION |
|---|---|
amount
|
The amount per usage in the smallest unit of the token
TYPE:
|
receiver
|
The address that will receive the payment
TYPE:
|
token_address
|
The address of the token to use for payment (defaults to native token)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PlanPriceConfig
|
A PlanPriceConfig object configured for pay-as-you-go payments |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to fetch contract address from API |
ValueError
|
If the receiver address is not a valid Ethereum address |
Source code in payments_py/api/plans_api.py
get_pay_as_you_go_credits_config
staticmethod
¶
order_fiat_plan
¶
Order a fiat plan using Stripe checkout.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the plan
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The Stripe checkout result |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to order the fiat plan |
Source code in payments_py/api/plans_api.py
redeem_credits
¶
redeem_credits(
agent_request_id: str,
plan_id: str,
redeem_from: str,
credits_amount_to_redeem: str,
) -> Dict[str, Any]
Redeem credits from a plan for a specific agent request.
| PARAMETER | DESCRIPTION |
|---|---|
agent_request_id
|
The unique identifier of the agent request
TYPE:
|
plan_id
|
The unique identifier of the plan
TYPE:
|
redeem_from
|
The address to redeem credits from
TYPE:
|
credits_amount_to_redeem
|
The amount of credits to redeem
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The result of the redeem operation |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to redeem credits |
Source code in payments_py/api/plans_api.py
Agents API¶
AgentsAPI
¶
Bases: BasePaymentsAPI
The AgentsAPI class provides methods to register and interact with AI Agents on Nevermined.
Source code in payments_py/api/base_payments.py
get_instance
classmethod
¶
Get a singleton instance of the AgentsAPI class.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
The options to initialize the payments class
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AgentsAPI
|
The instance of the AgentsAPI class |
Source code in payments_py/api/agents_api.py
register_agent
¶
register_agent(
agent_metadata: AgentMetadata,
agent_api: AgentAPIAttributes,
payment_plans: List[str],
organization_id: Optional[str] = None,
) -> Dict[str, str]
Registers a new AI Agent on Nevermined. The agent must be associated to one or multiple Payment Plans. Users that are subscribers of a payment plan can query the agent. Depending on the Payment Plan and the configuration of the agent, the usage of the agent/service will consume credits. When the plan expires (because the time is over or the credits are consumed), the user needs to renew the plan to continue using the agent.
| PARAMETER | DESCRIPTION |
|---|---|
agent_metadata
|
Agent metadata
TYPE:
|
agent_api
|
Agent API attributes
TYPE:
|
payment_plans
|
The list of payment plans giving access to the agent
TYPE:
|
organization_id
|
Optional org id (e.g.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, str]
|
The unique identifier of the newly created agent (Agent Id) |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If registration fails |
Source code in payments_py/api/agents_api.py
register_agent_and_plan
¶
register_agent_and_plan(
agent_metadata: AgentMetadata,
agent_api: AgentAPIAttributes,
plan_metadata: PlanMetadata,
price_config: PlanPriceConfig,
credits_config: PlanCreditsConfig,
access_limit: Optional[
Literal["credits", "time"]
] = None,
organization_id: Optional[str] = None,
) -> Dict[str, str]
Registers a new AI Agent and a Payment Plan associated to this new agent. Depending on the Payment Plan and the configuration of the agent, the usage of the agent/service will consume credits. When the plan expires (because the time is over or the credits are consumed), the user needs to renew the plan to continue using the agent.
| PARAMETER | DESCRIPTION |
|---|---|
agent_metadata
|
Agent metadata
TYPE:
|
agent_api
|
Agent API attributes
TYPE:
|
plan_metadata
|
Plan metadata
TYPE:
|
price_config
|
Plan price configuration
TYPE:
|
credits_config
|
Plan credits configuration
TYPE:
|
access_limit
|
Optional access limit for the plan
TYPE:
|
organization_id
|
Optional org id (e.g.
TYPE:
|
Returns: Dictionary containing agentId, planId, and txHash
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If registration fails |
Source code in payments_py/api/agents_api.py
get_agent
¶
Gets the metadata for a given Agent identifier.
| PARAMETER | DESCRIPTION |
|---|---|
agent_id
|
The unique identifier of the agent
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The agent's metadata |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the agent is not found |
Source code in payments_py/api/agents_api.py
get_agent_plans
¶
Gets the list of plans that can be ordered to get access to an agent.
| PARAMETER | DESCRIPTION |
|---|---|
agent_id
|
The unique identifier of the agent
TYPE:
|
pagination
|
Optional pagination options to control the number of results returned
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The list of all different plans giving access to the agent |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the agent is not found |
Source code in payments_py/api/agents_api.py
get_user_agents
¶
get_user_agents(
org_id: Optional[str] = None,
pagination: Optional[PaginationOptions] = None,
) -> Dict[str, Any]
List the AI agents published by the current user.
Returns only the agents the authenticated account created — this is
account management ("my agents"), not a marketplace search, and never
returns other users' agents. Pass org_id to list every agent in an
organization you are a member of instead.
| PARAMETER | DESCRIPTION |
|---|---|
org_id
|
Optional organization id. When set, returns every agent in that organization (the backend rejects orgs you are not an active member of); when omitted, returns the agents authored by the caller.
TYPE:
|
pagination
|
Optional pagination options (page, offset, sort_by, sort_order).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
A paginated result of the shape |
Dict[str, Any]
|
|
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the request fails. |
Source code in payments_py/api/agents_api.py
add_plan_to_agent
¶
Add a plan to an agent.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the plan
TYPE:
|
agent_id
|
The unique identifier of the agent
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The result of the operation |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to add plan to agent |
Source code in payments_py/api/agents_api.py
remove_plan_from_agent
¶
Remove a plan from an agent.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the plan
TYPE:
|
agent_id
|
The unique identifier of the agent
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The result of the operation |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to remove plan from agent |
Source code in payments_py/api/agents_api.py
update_agent_metadata
¶
update_agent_metadata(
agent_id: str,
agent_metadata: AgentMetadata,
agent_api: AgentAPIAttributes,
) -> Dict[str, Any]
Updates the metadata and API attributes of an existing AI Agent.
| PARAMETER | DESCRIPTION |
|---|---|
agent_id
|
The unique identifier of the agent
TYPE:
|
agent_metadata
|
The new metadata attributes for the agent
TYPE:
|
agent_api
|
The new API attributes for the agent
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The result of the update operation |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the agent is not found or if the update fails |
Source code in payments_py/api/agents_api.py
Facilitator API (x402)¶
FacilitatorAPI
¶
Bases: BasePaymentsAPI
The FacilitatorAPI class provides methods to verify and settle AI agent permissions. It enables AI agents to act as facilitators, managing credit verification and settlement for subscribers using X402 access tokens.
Source code in payments_py/api/base_payments.py
get_instance
classmethod
¶
Get a singleton instance of the FacilitatorAPI class.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
The options to initialize the payments class
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FacilitatorAPI
|
The instance of the FacilitatorAPI class |
Source code in payments_py/x402/facilitator_api.py
verify_permissions
¶
verify_permissions(
payment_required: X402PaymentRequired,
x402_access_token: str,
max_amount: Optional[str] = None,
) -> VerifyResponse
Verify if a subscriber has permission to use credits from a payment plan. This method simulates the credit usage without actually burning credits, checking if the subscriber has sufficient balance and permissions.
The planId and subscriberAddress are extracted from the x402AccessToken.
| PARAMETER | DESCRIPTION |
|---|---|
payment_required
|
x402 PaymentRequired from 402 response (required, for validation)
TYPE:
|
x402_access_token
|
The X402 access token (contains planId, subscriberAddress, agentId)
TYPE:
|
max_amount
|
The maximum number of credits to verify (as string, optional)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
VerifyResponse
|
VerifyResponse with is_valid boolean and optional error details |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If verification fails |
Source code in payments_py/x402/facilitator_api.py
settle_permissions
¶
settle_permissions(
payment_required: X402PaymentRequired,
x402_access_token: str,
max_amount: Optional[str] = None,
agent_request_id: Optional[str] = None,
) -> SettleResponse
Settle (burn) credits from a subscriber's payment plan. This method executes the actual credit consumption, burning the specified number of credits from the subscriber's balance. If the subscriber doesn't have enough credits, it will attempt to order more before settling.
The planId and subscriberAddress are extracted from the x402AccessToken.
| PARAMETER | DESCRIPTION |
|---|---|
payment_required
|
x402 PaymentRequired from 402 response (required, for validation)
TYPE:
|
x402_access_token
|
The X402 access token (contains planId, subscriberAddress, agentId)
TYPE:
|
max_amount
|
The number of credits to burn (as string, optional)
TYPE:
|
agent_request_id
|
Agent request ID for observability tracking (optional)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SettleResponse
|
SettleResponse with success boolean and transaction details |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If settlement fails |
Source code in payments_py/x402/facilitator_api.py
MPP API¶
MppAPI
¶
Bases: BasePaymentsAPI
Challenge issuance, credential redemption and the buyer-side helper.
Source code in payments_py/api/base_payments.py
get_instance
classmethod
¶
issue_challenge
¶
Mint the challenge a plan-protected endpoint returns with its 402.
Each call returns a distinct challenge even for identical inputs — the id doubles as the burn idempotency key, so two requests sharing one would settle as a single burn.
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
|
Source code in payments_py/mpp/mpp_api.py
verify_credential
¶
Run the full credential and plan checks without burning anything.
settle_credential
¶
Verify and burn through the same chokepoint an x402 settlement uses, so the credits charged are identical on both protocols. Settling the same credential twice burns once.
| RAISES | DESCRIPTION |
|---|---|
MppSettlementOutcomeUnknownError
|
when the call ended without a definite answer — the burn may or may not have committed. |
Source code in payments_py/mpp/mpp_api.py
get_mpp_access_token
¶
get_mpp_access_token(
plan_id: str,
agent_id: Optional[str] = None,
token_options: Optional[MppTokenOptions] = None,
) -> Dict[str, Any]
Mint an access token signed under the Nevermined-MPP EIP-712 domain.
Same inputs and same settlement rail as
:meth:X402TokenAPI.get_x402_access_token minus token_version;
the token verifies only on the MPP routes, which is what keeps the two
protocols isolated.
MPP carries no token version (nvm-monorepo#3266): its single-use
unit is the challenge, not the token, so one MPP token is presented
across many challenges by design. The parameter type says so, and a
caller that sets token_version on an :class:X402TokenOptions and
passes it here is refused before the request — the backend answers
BCK.MPP.0007 for any value, 2 included.
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
The mint response. Unlike the x402 mint it carries no |
Dict[str, Any]
|
|
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
( |
Source code in payments_py/mpp/mpp_api.py
fetch
¶
Perform an HTTP request, paying an MPP challenge if the endpoint returns one.
The buyer needs no new plan, delegation or credential: the delegation
that works for x402 works here unchanged. request_kwargs are handed
to :func:requests.request unchanged (headers, json, data,
params, timeout, stream, …).
A request body must be replayable if the endpoint may challenge the
request: a generator, iterator or file-like data= raises a typed
:class:PaymentsError once a 402 challenge actually requires a retry,
since it cannot be resent. A request that is never challenged sends such
a body exactly once, exactly like a plain requests call — the
paid=False / untouched-response guarantee still holds.
options.delegation_config must carry a delegation_id — this call
refuses the deprecated inline create-on-the-fly shape (no
delegation_id) that
:meth:X402TokenAPI.get_x402_access_token otherwise tolerates with a
warning: the retry loop here can mint an access token twice per call, so
that shape could silently create two delegations as a side effect of
paying.
Example
Source code in payments_py/mpp/mpp_api.py
MppFetchOptions
dataclass
¶
MppFetchOptions(
delegation_config: DelegationConfig,
agent_id: Optional[str] = None,
plan_id: Optional[str] = None,
max_credits: Optional[Union[str, int]] = None,
)
Options for :func:mpp_fetch / MppAPI.fetch.
.. warning:: Experimental. The MPP buyer surface may change in a minor release.
A request body must be replayable if the endpoint may challenge the
request: a generator, iterator or file-like data=, or an open handle
inside files=, raises a typed :class:PaymentsError once a 402 challenge
actually requires a retry, since it cannot be resent. A request that is never
challenged sends such a body exactly once, exactly like a plain requests
call.
This helper mints nvm:erc4337 access tokens only in this release — a
buyer holding an nvm:card-delegation delegation cannot use it yet.
MppFetchResult
dataclass
¶
MppFetchResult(
response: Response,
settled: bool,
paid: bool,
credentials_presented: int = 0,
credits_presented: Optional[str] = None,
receipt: Optional[MppReceipt] = None,
)
What :func:mpp_fetch / MppAPI.fetch returns.
.. warning:: Experimental. Fields may be added or change meaning in a minor release.
X402 Token API¶
X402TokenAPI
¶
Bases: BasePaymentsAPI
X402 Token API for generating access tokens.
Handles X402 access token generation for subscribers to authorize payment operations with AI agents.
Source code in payments_py/api/base_payments.py
get_instance
classmethod
¶
Get a singleton instance of the X402TokenAPI class.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
The options to initialize the API
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
X402TokenAPI
|
The instance of the X402TokenAPI class |
Source code in payments_py/x402/token.py
get_x402_access_token
¶
get_x402_access_token(
plan_id: str,
agent_id: Optional[str] = None,
token_options: Optional[X402TokenOptions] = None,
) -> Dict[str, Any]
Get an X402 access token for the given plan against a delegation.
This token allows the agent to verify and settle delegations on behalf of the subscriber.
Supported flow (create-first): create the delegation once via
payments.delegation.create_delegation(...) and pass only its
delegation_id here via token_options.delegation_config.
Passing spending limits / a payment method here instead (inline
create-on-the-fly, i.e. a delegation_config with no
delegation_id) is deprecated (#1674): this method emits a
FutureWarning and the backend logs its own deprecation warning.
The inline path will be removed in a future release.
A v3 token (single-use, bound to one seller and one endpoint) is
requested with token_options.token_version=3 plus the
resource / http_verb it should be bound to. Requesting v3 does
not guarantee v3: a backend predating nvm-monorepo#2646 strips the
field silently and returns v2. Read the tokenVersion key of the
result — it is detected from the token that came back, never from what
was asked for.
| PARAMETER | DESCRIPTION |
|---|---|
plan_id
|
The unique identifier of the payment plan
TYPE:
|
agent_id
|
The unique identifier of the AI agent (optional)
TYPE:
|
token_options
|
Options controlling scheme, delegation, the resource/verb the token is bound to, and the requested token version (optional)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
A dictionary containing:
- accessToken: The X402 access token string
- tokenVersion: |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If the request fails, or ( |
Example
# Create the delegation once (currency is required), then reuse it.
delegation = payments.delegation.create_delegation(
CreateDelegationPayload(
provider="erc4337",
spending_limit_cents=10000,
duration_secs=604800,
currency="usdc",
)
)
result = payments.x402.get_x402_access_token(
plan_id, agent_id,
token_options=X402TokenOptions(
delegation_config=DelegationConfig(
delegation_id=delegation.delegation_id
)
)
)
# Single-use, bound to one seller endpoint (v3):
result = payments.x402.get_x402_access_token(
plan_id, agent_id,
token_options=X402TokenOptions(
delegation_config=DelegationConfig(
delegation_id=delegation.delegation_id
),
resource="https://seller.example/api/v1/tasks",
http_verb="POST",
token_version=3,
),
)
if result["tokenVersion"] == 3:
... # single-use: mint a fresh token for the next paid request
Source code in payments_py/x402/token.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
Requests API¶
AgentRequestsAPI
¶
Bases: BasePaymentsAPI
The AgentRequestsAPI class provides methods to manage the requests received by AI Agents integrated with Nevermined.
Note: For request validation and credit settlement, use the x402 API instead: - payments.facilitator.verify_permissions() - to verify token and permissions - payments.facilitator.settle_permissions() - to burn credits
Source code in payments_py/api/base_payments.py
get_instance
classmethod
¶
Get a singleton instance of the AgentRequestsAPI class.
| PARAMETER | DESCRIPTION |
|---|---|
options
|
The options to initialize the payments class
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AgentRequestsAPI
|
The instance of the AgentRequestsAPI class |
Source code in payments_py/api/requests_api.py
track_agent_sub_task
¶
Tracks an agent sub task.
This method is used by agent owners to track agent sub tasks for agent tasks. It records information about credit redemption, categorization tags, and processing descriptions.
| PARAMETER | DESCRIPTION |
|---|---|
track_agent_sub_task
|
The agent sub task data to track
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
A promise that resolves to the result of the operation |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to track the agent sub task |
Source code in payments_py/api/requests_api.py
start_simulation_request
¶
start_simulation_request(
price_per_credit: float = 0.01,
batch: bool = False,
agent_name: str = None,
plan_name: str = None,
) -> StartAgentRequest
This method simulates an agent request.
| PARAMETER | DESCRIPTION |
|---|---|
price_per_credit
|
The price per credit in USD
TYPE:
|
batch
|
Whether the request is a batch request
TYPE:
|
agent_name
|
The name of the agent
TYPE:
|
plan_name
|
The name of the plan
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
StartAgentRequest
|
The information about the simulation of the request |
Source code in payments_py/api/requests_api.py
finish_simulation_request
¶
finish_simulation_request(
agent_request_id: str,
margin_percent: float = 0.2,
batch: bool = False,
) -> Dict[str, Any]
Simulates the redemption of credits for an agent request.
| PARAMETER | DESCRIPTION |
|---|---|
agent_request_id
|
The unique identifier of the agent request.
TYPE:
|
margin_percent
|
The margin percentage to apply. Defaults to 0.2.
TYPE:
|
batch
|
Whether the request is a batch request. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
A dictionary containing the result of the simulation, including the credits to redeem and the success status. |
| RAISES | DESCRIPTION |
|---|---|
PaymentsError
|
If unable to finish the simulation request. |