Skip to content

Latest commit

 

History

History
150 lines (107 loc) · 3.45 KB

File metadata and controls

150 lines (107 loc) · 3.45 KB

Fix Google Gemini API Key Issues

Your API Key

You've added: AIzaSyClkP29LlTNu833ZFObivr6LuwBOdq59_E

Steps to Fix

Step 1: Verify .env File Format

Make sure your backend/.env file looks exactly like this (no spaces around =):

PORT=5000
GOOGLE_GEMINI_API_KEY=AIzaSyClkP29LlTNu833ZFObivr6LuwBOdq59_E

Important:

  • No spaces before or after the = sign
  • No quotes around the API key
  • No trailing spaces

Step 2: Restart Backend Server

IMPORTANT: After adding/changing the .env file, you MUST restart the backend server:

  1. Stop the backend server (press Ctrl+C in the terminal)

  2. Start it again:

    cd backend
    npm start
  3. You should see: ✓ Google Gemini API key found

Step 3: Test the API Key

  1. Make sure backend is running

  2. Open browser and go to:

    http://localhost:5000/api/test-gemini
    
  3. You should see a JSON response:

    • If working: {"success": true, "message": "Gemini API is working!"}
    • If error: Check the error message

Step 4: Test the Chatbot

  1. Open frontend: http://localhost:3000
  2. Click the chatbot button (💬)
  3. Ask: "Hello"
  4. Check backend terminal for logs:
    • Should see: "Attempting to use Google Gemini API..."
    • Should see: "Gemini API success!"

Common Issues

Issue 1: API Key Not Being Read

Symptoms:

  • Backend shows: "GOOGLE_GEMINI_API_KEY not found"
  • Chatbot uses fallback assistant

Solution:

  1. Check .env file is in backend/.env (not backend/backend/.env)
  2. Make sure no spaces around =
  3. Restart backend server

Issue 2: Invalid API Key Error

Symptoms:

  • Error: "API key not valid" or "401" or "403"

Solution:

  1. Verify API key is correct (no typos)
  2. Check if API key is enabled in Google Cloud Console
  3. Make sure Gemini API is enabled for your project

Issue 3: Model Not Found

Symptoms:

  • Error: "404" or "model not found"

Solution:

  • The code will automatically try gemini-1.5-flash first, then gemini-pro
  • Both should work, but if not, check your API key permissions

Issue 4: Rate Limit Exceeded

Symptoms:

  • Error: "429" or "rate limit"

Solution:

  • Free tier has rate limits
  • Wait a few minutes and try again
  • Or upgrade your Google Cloud plan

Debug Steps

  1. Check .env file location:

    cd backend
    cat .env

    Should show your API key

  2. Check if server reads it:

    • Look for "✓ Google Gemini API key found" when starting backend
  3. Test API directly:

    • Visit: http://localhost:5000/api/test-gemini
    • Check the response
  4. Check backend logs:

    • When you send a chat message, check backend terminal
    • Look for error messages
  5. Check browser console:

    • Open DevTools (F12) → Console tab
    • Look for error messages

Still Not Working?

  1. Verify API key is valid:

  2. Check API is enabled:

  3. Try regenerating API key:

    • Create a new key
    • Update .env file
    • Restart backend
  4. Check backend terminal output:

    • Share the exact error message you see
    • This will help diagnose the issue

Quick Test

Run this in your browser (while backend is running):

http://localhost:5000/api/test-gemini

This will tell you exactly what's wrong with the API key!