Skip to main content
The plans module handles payment plan creation, ordering, and balance management.

Methods

createPlan

Create a new payment plan.
const { planId } = await payments.plans.createPlan({
  agentId,
  name,
  description,
  priceConfig,
  creditsConfig,
  accessLimit
})
Example:
import { getERC20PriceConfig, getFixedCreditsConfig } from '@nevermined-io/payments'

const { planId } = await payments.plans.createPlan({
  agentId: 'did:nv:agent-123',
  name: 'Pro Plan',
  description: '100 queries for $10',
  priceConfig: getERC20PriceConfig(10_000_000n, usdcAddress, builderAddress),
  creditsConfig: getFixedCreditsConfig(100n, 1n),
  accessLimit: 'credits'
})

orderPlan

Purchase a plan with cryptocurrency.
const result = await payments.plans.orderPlan(planId)
Returns:
interface OrderResult {
  transactionHash: string
  agreementId: string
  success: boolean
}

orderFiatPlan

Purchase a plan with fiat currency (Stripe).
const { sessionId, url } = await payments.plans.orderFiatPlan(planId)

// Redirect user to Stripe
window.location.href = url

getPlanBalance

Check remaining balance for a plan.
const balance = await payments.plans.getPlanBalance(planId)
Returns:
interface PlanBalance {
  credits: number
  expiresAt?: Date
  isActive: boolean
}

registerCreditsTrialPlan

Create a free trial plan with credits.
const { planId } = await payments.plans.registerCreditsTrialPlan({
  agentId,
  name: 'Free Trial',
  description: '10 free queries',
  credits: 10n,
  creditsPerRequest: 1n
})

registerTimeTrialPlan

Create a free trial plan with time limit.
const { planId } = await payments.plans.registerTimeTrialPlan({
  agentId,
  name: '7-Day Trial',
  description: 'Full access for 7 days',
  duration: 7 * 24 * 60 * 60 // seconds
})

Price Configuration Helpers

getERC20PriceConfig

Configure ERC-20 token pricing.
import { getERC20PriceConfig } from '@nevermined-io/payments'

const config = getERC20PriceConfig(
  10_000_000n,                    // amount (with decimals)
  '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // token address
  '0xYour...Address'              // receiver address
)

getNativePriceConfig

Configure native token pricing (ETH, etc.).
import { getNativePriceConfig } from '@nevermined-io/payments'

const config = getNativePriceConfig(
  1_000_000_000_000_000n,  // 0.001 ETH
  '0xYour...Address'
)

Credits Configuration Helpers

getFixedCreditsConfig

Fixed credits per purchase.
import { getFixedCreditsConfig } from '@nevermined-io/payments'

const config = getFixedCreditsConfig(
  100n,  // total credits
  1n     // credits per request
)

getDynamicCreditsConfig

Variable credits per request.
import { getDynamicCreditsConfig } from '@nevermined-io/payments'

const config = getDynamicCreditsConfig(
  1n,   // minimum per request
  10n   // maximum per request
)

getTimeBasedConfig

Time-based access configuration.
import { getTimeBasedConfig } from '@nevermined-io/payments'

const config = getTimeBasedConfig(
  30 * 24 * 60 * 60  // 30 days in seconds
)

Types

interface CreatePlanParams {
  agentId: string
  name: string
  description?: string
  priceConfig: PriceConfig
  creditsConfig?: CreditsConfig
  timeConfig?: TimeConfig
  accessLimit: 'credits' | 'time'
}

interface PriceConfig {
  tokenAddress: string
  amount: bigint
  receiver: string
}

interface CreditsConfig {
  totalCredits: bigint
  creditsPerRequest: bigint
}

Next Steps