Official Node.js SDK for integrating Lava’s monetization and AI proxy APIs
The @lavapayments/nodejs SDK provides a typed client for all Lava API operations including checkout, customers, usage tracking, and forward token generation.
import { Lava } from '@lavapayments/nodejs';const lava = new Lava();
The client reads your secret key from the LAVA_SECRET_KEY environment variable automatically. You can also pass it explicitly: new Lava('aks_live_...'). Never commit API keys to version control.
The gateway() method makes a request through Lava’s gateway in a single call. It handles forward token generation, URL construction, and body serialization automatically.
Use format to send requests in one LLM format and have Lava translate to the provider’s native format automatically. The provider format is auto-detected from the target URL.
// Send OpenAI-shaped requests to Anthropic — Lava translates both request and responseconst data = await lava.gateway('https://api.anthropic.com/v1/messages', { format: 'openai', body: { model: 'claude-haiku-4-5', messages: [{ role: 'user', content: 'Hello!' }], },});// Response is in OpenAI format, regardless of provider
Supported formats: openai, anthropic, google, bedrock. See Rewrite Proxy for details on format translation.
Pass the raw provider URL to gateway() (e.g., https://api.openai.com/v1/chat/completions), not lava.providers.* URLs. The method constructs the forwarding URL internally.
Passing customer_id without meter_slug will throw an error. Provide both for customer billing, use meter_slug alone for meter-only mode, or omit both for merchant billing.
The gateway() method returns parsed JSON. For streaming responses, use generateForwardToken() with manual fetch() instead.
When you need customer billing, metering, or BYOK, generate a forward token that bundles your credentials with billing parameters.Meter-only authentication — use this when you want usage tracking on a specific meter without billing a customer:
Unmanaged mode (bring your own key) lets you use Lava’s proxy for usage tracking and analytics without setting up wallet billing. You supply your own provider API key; the AI provider charges your account directly. Lava may still charge a service fee for metering.
Lava.login() loads the active secret key, creating one if needed.If you already have an authorization code (from a custom callback flow), exchange it directly:
Lava.login() requires a Node.js environment. It starts a local HTTP server and opens the system browser. It is not intended for use in production web applications.
List available AI models in OpenAI-compatible format. When authenticated with a spend key, results are filtered by the key’s allowed models and providers:
const { subscription } = await lava.customers.getSubscription('conn_abc123');if (subscription) { subscription.plan.name; // plan name subscription.credits.total_remaining; // total available credits subscription.credits.cycle_remaining; // from included credit + rollover subscription.credits.bundle_remaining; // from bundle purchases + rollover subscription.cycle_end_at; // current cycle end date subscription.pending_change; // null, or { type: 'cancellation' | 'downgrade', effective_at }}
// Set auto top-up bundle for a customer's subscriptionawait lava.subscriptions.update('as_abc123', { auto_top_up_bundle_id: 'cb_pack_id'});// Disable auto top-upawait lava.subscriptions.update('as_abc123', { auto_top_up_bundle_id: null});
A complete integration from checkout to AI request:
import { Lava } from '@lavapayments/nodejs';const lava = new Lava();// 1. Create a checkout session for a new customerconst session = await lava.checkoutSessions.create({ checkout_mode: 'subscription', origin_url: 'https://yourapp.com', plan_id: 'sc_plan_id'});// Pass session.checkout_session_token to the checkout SDK// 2. After checkout completes, your frontend receives a customer_id// Store it in your database, associated with your user// 3. Generate a forward token for AI requestsconst forwardToken = lava.generateForwardToken({ customer_id: 'conn_...', // from your database meter_slug: process.env.LAVA_METER_SLUG!});// 4. Make an AI request through Lava's proxyconst 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();// 5. Check remaining creditconst { subscription } = await lava.customers.getSubscription('conn_...');// subscription?.credits.total_remaining
The SDK automatically detects test vs production mode based on your secret key prefix:
aks_test_* routes to sandbox (sandbox-api.lava.so)