Nevermined extends x402 with the nvm:erc4337 scheme, enabling programmable settlement (credits/subscriptions/PAYG) using ERC-4337 smart accounts and session keys.
When calling a protected endpoint, the server returns a 402 Payment Required response with the payment-required header containing the payment requirements.
TypeScript
Python
import { X402_HEADERS } from '@nevermined-io/payments/express'// Call protected endpoint - get 402 responseconst response = await fetch('https://api.example.com/protected')if (response.status === 402) { // Decode the payment-required header const paymentRequired = JSON.parse( Buffer.from(response.headers.get(X402_HEADERS.PAYMENT_REQUIRED)!, 'base64').toString() ) // Extract planId and agentId from accepts array const { planId, extra } = paymentRequired.accepts[0] const agentId = extra?.agentId}
Use the Nevermined SDK to generate an x402 access token. The supported flow is create-first: create a delegation once with createDelegation (provider and currency are required), then request access tokens by passing its delegationId. Reuse the delegation until it expires or is exhausted.
TypeScript
Python
import { Payments } from '@nevermined-io/payments'const payments = Payments.getInstance({ nvmApiKey: process.env.NVM_API_KEY, environment: 'sandbox'})// 1. Create a delegation once (provider + currency required; 'usdc' for erc4337).const delegation = await payments.delegation.createDelegation({ provider: 'erc4337', spendingLimitCents: 10000, durationSecs: 604800, currency: 'usdc'})// 2. Request a token by delegationId (reuse it for subsequent requests).const { accessToken } = await payments.x402.getX402AccessToken(planId, agentId, { delegationConfig: { delegationId: delegation.delegationId }})
import osfrom payments_py import Payments, PaymentOptionsfrom payments_py.x402 import ( CreateDelegationPayload, DelegationConfig, X402TokenOptions,)payments = Payments.get_instance( PaymentOptions(nvm_api_key=os.environ['NVM_API_KEY'], environment='sandbox'))# 1. Create a delegation once (provider + currency required; 'usdc' for erc4337).delegation = payments.delegation.create_delegation( CreateDelegationPayload( provider='erc4337', spending_limit_cents=10000, duration_secs=604800, currency='usdc', ))# 2. Request a token by delegationId (reuse it for subsequent requests).token_res = payments.x402.get_x402_access_token( plan_id, agent_id, token_options=X402TokenOptions( delegation_config=DelegationConfig( delegation_id=delegation.delegation_id ) ),)access_token = token_res['accessToken']
Inline create-on-the-fly is deprecated. Passing creation fields (spendingLimitCents, durationSecs, providerPaymentMethodId, cardId, currency) directly in delegationConfig instead of a delegationId still works but emits a runtime deprecation warning. Create the delegation first, then pass only { delegationId }.