@lavapayments/nodejs SDK provides a typed client for all Lava API operations including checkout, customers, usage tracking, and forward token generation.
Installation
Initialize
Resources
Plus utility methods:
gateway()— Make requests through the gateway in a single callgenerateForwardToken()— Create authentication tokens for manual requestsLava.login()— Browser-based CLI authentication (static)Lava.exchangeAuthCode()— Exchange auth code for credentials (static)providers.*— Pre-configured URLs for 27 AI providers
Gateway Requests
Thegateway() 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)
Useformat 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.
openai, anthropic, google, bedrock. See Rewrite Proxy for details on format translation.
With Custom Headers
Some providers require additional headers. Pass them viaoptions.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.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: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: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
UseLava.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:
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
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