Fix billing tier detection - use correct endpoint and pass API key

- Use /v1/metadata/balance endpoint (same as letta-code)
- Pass API key explicitly since it may not be in process.env yet
- Add debug logging for billing tier detection

🤖 Generated with [Letta Code](https://letta.com)

Co-Authored-By: Letta <noreply@letta.com>
This commit is contained in:
Sarah Wooders
2026-01-28 21:35:38 -08:00
parent 10aa377776
commit c7f8be5d7e
2 changed files with 18 additions and 9 deletions

View File

@@ -372,7 +372,9 @@ async function stepModel(config: OnboardConfig, env: Record<string, string>): Pr
let billingTier: string | null = null;
if (!isSelfHosted) {
spinner.start('Checking account...');
billingTier = await getBillingTier();
// Pass the API key explicitly since it may not be in process.env yet
const apiKey = config.apiKey || env.LETTA_API_KEY || process.env.LETTA_API_KEY;
billingTier = await getBillingTier(apiKey);
config.billingTier = billingTier ?? undefined;
spinner.stop(billingTier === 'free' ? 'Free plan' : `Plan: ${billingTier || 'unknown'}`);
}

View File

@@ -23,28 +23,35 @@ export interface ModelInfo {
/**
* Get billing tier from Letta API
* Uses /v1/metadata/balance endpoint (same as letta-code)
*/
export async function getBillingTier(): Promise<string | null> {
export async function getBillingTier(apiKey?: string): Promise<string | null> {
try {
const baseUrl = process.env.LETTA_BASE_URL || 'https://api.letta.com';
const apiKey = process.env.LETTA_API_KEY;
const key = apiKey || process.env.LETTA_API_KEY;
// Self-hosted servers don't have billing tiers
if (baseUrl !== 'https://api.letta.com') {
return null;
}
if (!apiKey) return 'free';
if (!key) return 'free';
const response = await fetch(`${baseUrl}/v1/users/me/balance`, {
headers: { 'Authorization': `Bearer ${apiKey}` },
const response = await fetch(`${baseUrl}/v1/metadata/balance`, {
headers: { 'Authorization': `Bearer ${key}` },
});
if (!response.ok) return 'free';
if (!response.ok) {
console.error(`[BillingTier] API returned ${response.status}`);
return 'free';
}
const data = await response.json() as { billing_tier?: string };
return data.billing_tier?.toLowerCase() ?? 'free';
} catch {
const tier = data.billing_tier?.toLowerCase() ?? 'free';
console.log(`[BillingTier] Got tier: ${tier}`);
return tier;
} catch (err) {
console.error(`[BillingTier] Error:`, err);
return 'free';
}
}