3.1 KiB
3.1 KiB
Troubleshooting Guide
API Key Not Loading from .env
Step 1: Verify .env File Exists and Location
The .env file must be in the project root (same directory as docker-compose.yml and main.py).
Check if it exists:
# Windows PowerShell
Test-Path .env
# Should return: True
Step 2: Check .env File Format
Your .env file should look exactly like this:
GEMINI_API_KEY=AIzaSyYourActualKeyHere123456789
Common mistakes:
- ❌
GEMINI_API_KEY = value(spaces around =) - ❌
GEMINI_API_KEY="value"(quotes) - ❌
GEMINI_API_KEY='value'(single quotes) - ❌
GEMINI_API_KEY= value(space after =) - ✅
GEMINI_API_KEY=value(correct!)
Step 3: Test if API Key is Loaded
-
If using Docker:
docker-compose restart docker-compose logs aifit-fastapiLook for:
✓ GEMINI_API_KEY loaded: AIzaSy... -
Use the debug endpoint:
- Open browser:
http://localhost:8000/debug/api-key-status - Or in Postman: GET
http://localhost:8000/debug/api-key-status - Should return:
{ "status": "ok", "message": "API key is loaded", "key_length": 39, "key_preview": "AIzaSy...", "key_ends_with": "xyz" }
- Open browser:
Step 4: If Using Docker - Restart After Creating .env
If you created the .env file after starting Docker:
-
Stop the container:
docker-compose down -
Start again:
docker-compose up --build
Docker needs to be restarted to pick up new .env files.
Step 5: Alternative - Set Environment Variable Directly
If .env still doesn't work, you can set it directly in docker-compose.yml:
environment:
- GEMINI_API_KEY=your_actual_key_here
# ... other vars
⚠️ Warning: Don't commit this to git! Use .env file instead.
Step 6: Verify API Key is Valid
- Check the key starts with
AIza(most Gemini keys do) - Make sure there are no extra characters or line breaks
- Try regenerating the key from Google AI Studio if needed
Step 7: Check Docker Logs
docker-compose logs aifit-fastapi
Look for:
✓ GEMINI_API_KEY loaded: ...= Good!⚠ API key seems too short= Key might be truncatedGEMINI_API_KEY not set= File not found or wrong format
Quick Fix Checklist
.envfile exists in project root.envfile has exactly:GEMINI_API_KEY=your_key(no spaces, no quotes)- Restarted Docker after creating/editing
.env - Checked
/debug/api-key-statusendpoint - Verified API key is valid (starts with
AIzatypically) - Checked Docker logs for loading confirmation
Still Not Working?
-
Test without Docker:
# Activate venv python -m venv venv venv\Scripts\activate # Windows # Install deps pip install -r requirements.txt # Run directly (will use .env) uvicorn main:app --reload -
Check if python-dotenv is installed:
pip list | findstr dotenv -
Manually set environment variable:
# Windows PowerShell $env:GEMINI_API_KEY="your_key_here" uvicorn main:app --reload