The middleware auto-detects the payment scheme from plan metadata. Plans with fiat pricing (isCrypto: false) automatically use nvm:card-delegation (Stripe), while crypto plans use nvm:erc4337.You can explicitly override the scheme in the route configuration:
The middleware automatically detects the payment scheme from plan metadata.
Plans with fiat pricing (isCrypto: false) use nvm:card-delegation (Stripe).
No code changes are needed on the agent side. You can explicitly override with
the scheme parameter.
For fiat plans, clients can use resolveScheme() to auto-detect the payment scheme before generating tokens. See the x402 developer guide for details on scheme resolution and X402TokenOptions.
Here’s how clients interact with your payment-protected API:
import { Payments } from '@nevermined-io/payments'import { X402_HEADERS } from '@nevermined-io/payments/express'const payments = Payments.getInstance({ nvmApiKey: process.env.NVM_API_KEY!, environment: 'sandbox'})async function callProtectedAPI() { const SERVER_URL = 'http://localhost:3000' // Step 1: Request without token → 402 const response1 = await fetch(`${SERVER_URL}/ask`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: 'What is 2+2?' }) }) if (response1.status === 402) { // Step 2: Decode payment requirements const paymentRequired = JSON.parse( Buffer.from( response1.headers.get(X402_HEADERS.PAYMENT_REQUIRED)!, 'base64' ).toString() ) const { planId, extra } = paymentRequired.accepts[0] const agentId = extra?.agentId // Step 3: Generate x402 token. Create the delegation first (provider + // currency required), then request the token by its delegationId. const delegation = await payments.delegation.createDelegation({ provider: 'erc4337', // 'stripe' | 'braintree' | 'visa' for fiat plans spendingLimitCents: 1000, durationSecs: 86_400, currency: 'usdc', // 'usd' for fiat plans }) const { accessToken } = await payments.x402.getX402AccessToken(planId, agentId, { scheme: 'nvm:erc4337', // 'nvm:card-delegation' for fiat plans delegationConfig: { delegationId: delegation.delegationId }, }) // Step 4: Request with token → 200 const response2 = await fetch(`${SERVER_URL}/ask`, { method: 'POST', headers: { 'Content-Type': 'application/json', [X402_HEADERS.PAYMENT_SIGNATURE]: accessToken }, body: JSON.stringify({ query: 'What is 2+2?' }) }) const data = await response2.json() console.log('Response:', data.response) // Step 5: Decode settlement receipt const settlement = JSON.parse( Buffer.from( response2.headers.get(X402_HEADERS.PAYMENT_RESPONSE)!, 'base64' ).toString() ) console.log('Credits used:', settlement.creditsRedeemed) }}