What You’ll Build
In this quickstart, you’ll route your first AI request through Lava Build to track usage and costs. By the end, you’ll have:- A Lava forward token for authentication
- A working API call routed through Lava’s proxy
- Usage data visible in your dashboard
Prerequisites: You need a Lava account and a backend server. Lava requires backend integration—direct frontend calls are blocked by CORS for security.
Step 1: Find Your Token
Your self forward token is auto-generated when you sign up. This token authenticates your requests and links them to your account for usage tracking.1
Navigate to Secret Keys
Go to Build > Secret Keys in your dashboard.
2
Copy Your Token
Find the “Self Forward Token” section and click the copy icon to copy your token to clipboard.
Step 2: Set Up Your Project
Create a.env.local file in your project root with these variables:
.env.local
About the API URL: You’ll construct the full endpoint by appending
/forward?u=<provider_url> to the base URL. The u parameter tells Lava which AI provider to route your request to.Example for OpenAI: ${LAVA_BASE_URL}/forward?u=https://api.openai.com/v1/chat/completionsStep 3: Understanding the Request URL
Let’s break down how Lava routes your request. The URL has three parts:| Part | Value | Purpose |
|---|---|---|
| Base URL | https://api.lavapayments.com/v1 | Lava’s API endpoint (from your .env.local) |
| Forward path | /forward | Tells Lava to route the request |
| Query parameter | ?u=https://api.openai.com/v1/chat/completions | Specifies which AI provider to call |
?u= parameter and routes your request to that endpoint. For this tutorial, we’re routing to OpenAI’s chat completions endpoint.
Step 4: Add Authentication
The forward token authenticates your request and links it to your account for usage tracking:Authorization header tells Lava which account to charge for this request.
Step 5: Define the Request Body
The request body is sent directly to OpenAI. It uses OpenAI’s standard format—no modifications needed:Step 6: Make the Request
Now let’s put it all together. Create a file calledtest-lava.js:
test-lava.js
Step 7: See the Results
You should see output like this:Step 8: Check the Dashboard
Lava automatically tracked usage and costs for your request. Let’s see what data is available:- Go to Monetize > Explore in your dashboard
- Find your request using the timestamp or request ID
- View the details Lava captured:
- Token counts: Input (13), output (8), total (21)
- Cost breakdown: Base cost charged to your wallet
- Model:
gpt-4o-mini - Provider: OpenAI
Step 9: Use in Your Application (Optional)
Once you’ve confirmed the test request works, you can add the same code to your application. Here’s the same request wrapped in a Next.js API route:app/api/chat/route.ts
Why backend only? Forward tokens contain credentials that charge your wallet. Lava blocks frontend requests with CORS to prevent token exposure and unauthorized charges.
What’s Next?
Forward Proxy Guide
Learn advanced configuration, streaming responses, and multi-provider routing
Architecture Overview
Understand how Lava’s transparent proxy works under the hood
How Lava Build Works
Lava acts as a transparent proxy between your application and AI providers:- Your app sends a request with the forward token
- Lava validates the token and checks wallet balance
- Request is forwarded to the upstream provider (OpenAI, Anthropic, etc.)
- Usage is metered automatically based on provider response
- Costs are charged to your Lava wallet using prepaid credits
Lava adds less than 20ms of latency through edge computing and Redis caching. Streaming responses work seamlessly.
Troubleshooting
Where is my forward token?
Where is my forward token?
See Step 1: Get Your Forward Token above for the exact location.If you don’t see it in your dashboard:
- Verify you’re logged into your merchant account (not a wallet-only account)
- Check that your account setup completed successfully
- Contact [email protected] if the Test Your Setup section is missing
401 Unauthorized Error
401 Unauthorized Error
Insufficient Balance Error
Insufficient Balance Error
Cause: Your Lava wallet doesn’t have enough creditsSolution:
- Navigate to Wallet > Billing in the dashboard
- Add a payment method and fund your wallet
- Enable autopay to automatically top up when balance runs low
CORS Error (Frontend)
CORS Error (Frontend)
Cause: Attempted to call Lava from client-side JavaScriptSolution:
- Move the API call to your backend server
- Lava blocks frontend requests for security (prevents token exposure)
Request Not Showing in Dashboard
Request Not Showing in Dashboard
Cause: Request may have failed before reaching Lava, or you’re looking at the wrong time rangeSolution:
- Check your application logs for errors
- Verify the request actually executed
- Adjust the time range filter in the dashboard
- Look for error responses in the HTTP status code
What's the difference between a secret key and a forward token?
What's the difference between a secret key and a forward token?
Secret key: Your base API credential (like a username/password). Managed in the Secrets page.Forward token: A composite credential that includes your secret key + connection ID + product ID. This is what you actually use in API calls.Self forward token: A special forward token that charges your own Lava wallet. Perfect for development and testing. Generated automatically using your “Default” secret key.For production apps: You’ll generate custom forward tokens programmatically for each customer using the
@lavapayments/nodejs SDK. See Monetize Quickstart.Can I create additional secret keys?
Can I create additional secret keys?
Yes, but most developers only need the auto-generated “Default” key.When to create additional keys:
- Separating dev/staging/prod environments
- Security rotation best practices
- Different team members or projects
Understanding Authentication
The Relationship Between Secret Keys and Forward Tokens
Lava uses a two-layer authentication system: Layer 1: Secret Keys- Base credentials you manage in your dashboard
- Used to prove you’re an authorized merchant
- Can be rotated for security
- Never sent directly in API calls
- Composite credentials combining: secret key + connection + product
- What you actually send in the
Authorizationheader - Generated programmatically (except self forward token)
- Each customer gets their own forward token
Self Forward Token: A Special Case
The self forward token is unique:- ✅ Auto-generated on signup (no SDK needed)
- ✅ Uses your “Default” secret key automatically
- ✅ Charges your own merchant wallet (not a customer’s)
- ✅ Perfect for development and testing
Support
Having trouble? We’re here to help:- Documentation: lavapayments.com/docs
- Support: [email protected]
- Dashboard: Check Monetize > Explore for detailed request logs