From ee4f3766cd855e9b4f543191478782b0b5fc36f0 Mon Sep 17 00:00:00 2001 From: Philip Date: Wed, 28 Jan 2026 17:49:18 -0800 Subject: [PATCH] feat(gemini): Add API key validation and error handling Includes a check for the presence of the API key before initializing the Gemini AI client. Logs an error and returns an empty array if the API key is not found, preventing runtime errors and providing clearer feedback. --- services/geminiService.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/services/geminiService.ts b/services/geminiService.ts index 1604d21..8bbcbba 100644 --- a/services/geminiService.ts +++ b/services/geminiService.ts @@ -2,7 +2,15 @@ import { GoogleGenAI, Type } from "@google/genai"; import { Question } from "../types"; const generateQuestions = async (topic: string, count: number = 5): Promise => { - const ai = new GoogleGenAI({ apiKey: process.env.API_KEY }); + // Use process.env.API_KEY as per guidelines. + const apiKey = process.env.API_KEY; + + if (!apiKey) { + console.error("API Key is missing. Check your .env file."); + return []; + } + + const ai = new GoogleGenAI({ apiKey }); try { const response = await ai.models.generateContent({