From 0b3cc09478ca5b7efa7c3b41e6e317edb76e1941 Mon Sep 17 00:00:00 2001 From: vipul674 Date: Tue, 4 Aug 2026 01:55:17 +0530 Subject: [PATCH] fix: guard /api/ai/roast against missing GEMINI_API_KEY The route created the GoogleGenerativeAI client at module level with an empty key, so calls threw a cryptic Google SDK error as a 500. Return a clear 'Gemini API key is not configured' error when the key is absent, matching the guard pattern used by project-tutor and cv-ai-generator. Also document GEMINI_API_KEY in .env.example. Closes #3109 --- .env.example | 5 +++++ src/app/api/ai/roast/route.ts | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index d441bbaf6..8c93fca2e 100644 --- a/.env.example +++ b/.env.example @@ -94,6 +94,11 @@ UPSTASH_REDIS_REST_TOKEN=your_upstash_redis_rest_token # console.groq.com → API Keys GROQ_API_KEY=your_groq_api_key +# ------------------------------------------------------- +# Gemini API Key (optional — enables the /api/ai/roast route) +# aistudio.google.com → Get API key +GEMINI_API_KEY=your_gemini_api_key + # ------------------------------------------------------- # Leaderboard Configuration # Controls concurrent user fetches during leaderboard builds diff --git a/src/app/api/ai/roast/route.ts b/src/app/api/ai/roast/route.ts index 2eb468a8c..a73950276 100644 --- a/src/app/api/ai/roast/route.ts +++ b/src/app/api/ai/roast/route.ts @@ -1,10 +1,19 @@ import { NextResponse } from 'next/server'; import { GoogleGenerativeAI } from '@google/generative-ai'; +const GEMINI_API_KEY = process.env.GEMINI_API_KEY || ''; + // Initialize the Google Generative AI SDK -const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY || ''); +const genAI = new GoogleGenerativeAI(GEMINI_API_KEY); export async function POST(req: Request) { + if (!GEMINI_API_KEY) { + return NextResponse.json( + { error: 'Gemini API key is not configured' }, + { status: 500 } + ); + } + try { const body = await req.json(); const { mode, stats } = body;