Skip to main content

Installation

Install the Lava Node.js SDK using npm:
npm install @lavapayments/nodejs
Requirements: Node.js 18.0.0 or higher

Initialize the Client

Import and initialize the Lava client with your secret key:
import { Lava } from '@lavapayments/nodejs';

const lava = new Lava(process.env.LAVA_SECRET_KEY!, {
  apiVersion: '2025-04-28.v1'
});
Store your secret key in environment variables (e.g., LAVA_SECRET_KEY) to keep it secure. Never commit API keys to version control.

Your First Request: Complete Flow

Here’s a complete example showing the full integration workflow from checkout to AI request:
import { Lava } from '@lavapayments/nodejs';

const lava = new Lava(process.env.LAVA_SECRET_KEY!, {
  apiVersion: '2025-04-28.v1'
});

// Step 1: Create a checkout session for a new customer
const session = await lava.checkoutSessions.create({
  checkout_mode: 'onboarding',
  origin_url: 'https://yourapp.com',
  reference_id: 'user_123' // Your internal user ID
});

console.log('Checkout token:', session.checkout_session_token);
// Pass this token to your frontend to display the checkout UI

// Step 2: User completes payment on frontend
// (Use @lavapayments/checkout React library - not covered here)

// Step 3: After checkout completes, get the connection
const connections = await lava.connections.list({
  reference_id: 'user_123',
  limit: 1
});
const connection = connections.data[0];

// Step 4: Generate forward token for AI requests
const forwardToken = lava.generateForwardToken({
  connection_secret: connection.connection_secret,
  product_secret: process.env.LAVA_PRODUCT_SECRET!
});

// Step 5: Make an AI request through Lava's proxy
const response = await fetch(lava.providers.openai + '/chat/completions', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${forwardToken}`
  },
  body: JSON.stringify({
    model: 'gpt-4o-mini',
    messages: [
      { role: 'system', content: 'You are a helpful assistant.' },
      { role: 'user', content: 'Hello!' }
    ]
  })
});

const data = await response.json();
console.log('AI response:', data);

// Step 6: Check usage and balance
const updatedConnection = await lava.connections.retrieve(connection.connection_id);
console.log('Remaining balance:', updatedConnection.wallet.balance);

What’s Happening Here?

  1. Create checkout session - Generate a session for wallet creation and payment collection
  2. User completes checkout - Frontend displays Lava’s checkout UI (using @lavapayments/checkout)
  3. Retrieve connection - Get the connection details after checkout completes
  4. Generate forward token - Create authentication token combining secret key, connection, and product
  5. Make AI request - Call AI provider through Lava’s transparent proxy with automatic billing
  6. Check balance - Verify wallet balance and usage
The SDK automatically detects test vs production mode based on your secret key prefix:
  • aks_test_* → Routes to sandbox (sandbox-api.lavapayments.com)
  • Other prefixes → Routes to production (api.lavapayments.com)

Environment Variables

Set up these environment variables for the example above:
# Required
LAVA_SECRET_KEY='aks_your_secret_key_here'

# Optional - needed for product-specific pricing
LAVA_PRODUCT_SECRET='your_product_secret_here'
You can find your secret key and product secret in your Lava dashboard under Build > Secrets.

Next Steps

How the SDK Works

The SDK provides a clean TypeScript interface to Lava’s REST API with five main resource classes:
ResourcePurposeKey Methods
checkoutSessionsWallet onboarding and payment collectioncreate(), list(), retrieve()
connectionsManage wallet-merchant relationshipslist(), retrieve(), delete()
requestsTrack individual AI API callslist(), create(), retrieve()
usageRetrieve aggregated statisticsretrieve()
subscriptionsManage recurring billing planslistConfigs(), createConfig(), listActiveSubscriptions()
Plus utility methods:
  • generateForwardToken() - Create authentication tokens for AI requests
  • providers.* - Pre-configured URLs for 27 AI providers

Support

Need help getting started?