After updating GEMINI_API_KEY in Railway environment variables, the bot still shows "API key expired" errors.
The bot loads environment variables only at startup. After changing environment variables in Railway, you MUST restart the deployment:
- Go to your Railway project dashboard
- Click on your service/deployment
- Click the "Deployments" tab
- Find the latest deployment
- Click the three dots (⋯) menu
- Select "Redeploy" or "Restart"
Alternatively:
- Go to Settings → Deploy → Click "Redeploy"
- Or trigger a new deployment by pushing a commit (even a small change)
- In Railway dashboard, go to your service
- Click "Variables" tab
- Verify:
- Variable name is exactly:
GEMINI_API_KEY(case-sensitive) - Value is set (not empty)
- No extra spaces or quotes around the value
- The key is valid and not expired
- Variable name is exactly:
After restarting, check the logs to see if the new key is being used:
# In Railway dashboard, go to "Deployments" → Click on latest deployment → "Logs"
# Look for:
# - "Initialized Gemini client (new SDK): gemini-3-flash-preview"
# - No "API key expired" errorsYou can test if your new API key is valid:
# In Railway shell or locally
python3 -c "
import os
from google import genai
os.environ['GEMINI_API_KEY'] = 'YOUR_NEW_KEY_HERE'
client = genai.Client()
try:
result = client.models.generate_content(
model='gemini-3-flash-preview',
contents='Hello'
)
print('✅ API key is valid!')
except Exception as e:
print(f'❌ API key error: {e}')
"Issue: "API key expired" even after restart
- Solution: The new key might also be expired. Generate a fresh key from Google AI Studio
Issue: Variable not found
- Solution: Check variable name spelling:
GEMINI_API_KEY(exact case)
Issue: Bot still using old key
- Solution: Make sure you restarted the deployment, not just saved the variable
Issue: Multiple services/environments
- Solution: Make sure you updated the variable in the correct service/environment
- Updated
GEMINI_API_KEYin Railway Variables - Verified variable name is exactly
GEMINI_API_KEY - Verified value is set (not empty)
- Restarted/Redeployed the Railway service
- Checked logs after restart
- Verified new API key is valid (not expired)
The bot loads the API key in this order:
- At startup:
Config.from_env()readsGEMINI_API_KEYfrom environment - When initializing:
GeminiClient.__init__()setsos.environ['GEMINI_API_KEY']from config - When using:
genai.Client()reads fromos.environ['GEMINI_API_KEY']
Important: This happens only once at startup. Railway environment variables are injected when the container starts, so you must restart to pick up changes.