Skip to content

Commit f8cbbb1

Browse files
author
Deepak Pandey
committed
Fix OPENROUTER_API_KEY build error - move environment variable check to runtime
- Fixed app/api/ai/route.ts: Moved OPENROUTER_API_KEY check from module level to inside callOpenRouterAPI function - This prevents build-time errors when environment variables are not available - Build now passes successfully with 142/142 pages generated - All environment variable checks now happen at runtime, not build time
1 parent 91f088f commit f8cbbb1

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

app/api/ai/route.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,8 @@ export const runtime = 'nodejs';
6868

6969
// Initialize OpenRouter AI
7070
// OpenRouter API configuration
71-
const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY;
7271
const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1/chat/completions';
7372

74-
if (!OPENROUTER_API_KEY) {
75-
throw new Error('OPENROUTER_API_KEY is required');
76-
}
77-
7873
// Create Supabase client function to avoid build-time initialization
7974
function getSupabaseClient() {
8075
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
@@ -84,6 +79,12 @@ function getSupabaseClient() {
8479

8580
// Function to call OpenRouter API with DeepSeek V3.1 and free fallbacks
8681
async function callOpenRouterAPI(prompt: string): Promise<string> {
82+
const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY;
83+
84+
if (!OPENROUTER_API_KEY) {
85+
throw new Error('OPENROUTER_API_KEY is required');
86+
}
87+
8788
const models = [
8889
"deepseek/deepseek-chat-v3.1:free", // Primary - DeepSeek V3.1 FREE
8990
"deepseek/deepseek-chat-v3.1:free", // Retry V3.1 if first fails

0 commit comments

Comments
 (0)