Skip to main content
The @lavapayments/nodejs SDK provides a typed client for all Lava API operations including checkout, customers, usage tracking, and forward token generation.

Installation

Requirements: Node.js 18.0.0 or higher

Initialize

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.

Resources

Plus utility methods:
  • gateway() — Make requests through the gateway in a single call
  • generateForwardToken() — Create authentication tokens for manual requests
  • Lava.login() — Browser-based CLI authentication (static)
  • Lava.exchangeAuthCode() — Exchange auth code for credentials (static)
  • providers.* — Pre-configured URLs for 27 AI providers

Gateway Requests

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.

Parameters

With Customer Billing

Meter-Only Mode

Track usage on a meter without billing a specific customer:

With Format Translation (Rewrite)

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.
Supported formats: openai, anthropic, google, bedrock. See Rewrite Proxy for details on format translation.

With Custom Headers

Some providers require additional headers. Pass them via options.headers — the Authorization header is always set to the forward token automatically.
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.

Authentication

Using Your Secret Key (Simplest)

The simplest way to authenticate is to pass your secret key directly — no forward token needed. Costs are charged to your merchant wallet:
Or use the gateway() method which handles this automatically.

Generating Forward Tokens

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:
Customer-based authentication — use this when billing a specific customer’s wallet with your pricing configuration:
Billing-disabled authentication — proxy requests through a customer’s meter without charging their wallet:
Unmanaged (bring your own key) authentication — use your own provider API key with Lava for usage tracking only:
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.

CLI Authentication

Use Lava.login() to authenticate from a CLI or script. It opens the browser, waits for the user to authorize, and returns credentials:
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.

Key Management

Spend Keys

Spend keys give scoped access to your wallet for AI requests:

Model Discovery

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:

Key Operations

Create a Checkout Session

Checkout modes: subscription (recurring plan), credit_bundle (add-on credits).

Retrieve a Customer

Check Subscription Status

Update a Subscription

Report Usage (Post-Request Billing)

For advanced scenarios where you track usage outside of Lava’s proxy:

Get Usage Statistics

Common Patterns

Pagination

All list methods support cursor-based pagination:

Error Handling

Gateway Flow Example

A minimal integration for developers using Lava as an AI proxy:
Gateway mode charges costs to your merchant wallet. To bill customers instead, see the Monetization Flow below.

Monetization Flow Example

A complete integration from checkout to AI request:
The SDK automatically detects test vs production mode based on your secret key prefix:
  • aks_test_* routes to sandbox (sandbox-api.lava.so)
  • Other prefixes route to production (api.lava.so)

Next Steps

API Reference

Complete API documentation with all parameters and response types

Forward Proxy

Route AI requests through Lava’s proxy