diff --git a/services/geminiService.ts b/services/geminiService.ts index 2f6ff55..5456e8e 100644 --- a/services/geminiService.ts +++ b/services/geminiService.ts @@ -29,11 +29,18 @@ Guidelines: // Helper to get API key safely const getApiKey = (): string | undefined => { - try { - return process.env.API_KEY; - } catch (e) { + // Try different sources (Vite defines these at build time) + const key = + (typeof process !== 'undefined' && process.env ? process.env.API_KEY || process.env.GEMINI_API_KEY : undefined) || + (import.meta as any).env?.VITE_GEMINI_API_KEY || + (import.meta as any).env?.API_KEY || + (import.meta as any).env?.GEMINI_API_KEY; + + // Ensure it's not a literal "undefined" string from a failed build injection + if (key === "undefined" || key === "null" || !key) { return undefined; } + return key; }; const formatCurrency = (val: number) => `€${val.toLocaleString('de-DE', { minimumFractionDigits: 0, maximumFractionDigits: 0 })}`; diff --git a/vite.config.ts b/vite.config.ts index 0c237f3..4f731fd 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -49,8 +49,10 @@ export default defineConfig(({ mode }) => { }, plugins: [react()], define: { - 'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY), - 'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY) + 'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY || ""), + 'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY || ""), + 'import.meta.env.VITE_GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY || ""), + 'import.meta.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY || "") }, resolve: { alias: {