Skip to main content

Initializing the Library

This guide shows how to initialize the Nevermined Payments Library in your application. The library supports both server-side (Node.js) and browser environments with different initialization methods.

Get Your NVM API Key

Before initializing the library, you need a Nevermined API key:
  1. Visit nevermined.app
  2. Sign in or create a free account
  3. Navigate to your profile settings
  4. Generate an API key
Important: There are two types of API keys:
  • Builder Key: For AI builders who register agents and payment plans
  • Subscriber Key: For users who purchase access and query agents
Most applications will use a builder key for server-side operations.

Server-Side Initialization

For Node.js applications, use the getInstance method:
The environment is derived automatically from your API key — see Environment derivation below.

Configuration Options

The getInstance method accepts a PaymentOptions object:

Environment derivation

NVM API keys are prefixed with the environment they belong to (<prefix>:<jwt>), so the SDK resolves the target environment from the key — you no longer need to pass environment. The environment option is deprecated but still accepted:
  • When the key prefix maps to a known environment, the key always wins and the environment option is ignored (a one-time deprecation warning is logged).
  • When the key has no recognized prefix (for example a local/custom build), the SDK falls back to the environment option, and finally to custom. This keeps local and custom-backend workflows working unchanged.

Complete Server-Side Example

Browser Initialization

For browser-based applications, use the getBrowserInstance method:

Browser Authentication Flow

The browser instance supports OAuth-style authentication:

Environments

The Nevermined protocol supports multiple environments:

Production Environments

Choosing an Environment

Important: Agents and plans registered in one environment cannot be accessed from another. Always use the same environment throughout your application.

Environment Configuration Details

Each environment has specific API endpoints:
The library automatically configures these endpoints based on your chosen environment.

Verify Connection

After initialization, verify the connection is working:

Best Practices

  1. Environment Variables: Always store API keys in environment variables, never hardcode them
  2. Singleton Pattern: Create one Payments instance per application
  3. Environment Consistency: Use the same environment for all operations in a session
  4. Error Handling: Wrap initialization in try-catch blocks for production apps

Source References:
  • src/payments.ts (getInstance, getBrowserInstance methods)
  • src/environments.ts (environment configurations)
  • tests/e2e/fixtures.ts (createPaymentsBuilder, createPaymentsSubscriber)