Gemini CLI requires an API key to authenticate with Google's Gemini models. This guide walks you through getting an API key and configuring it for use with Gemini CLI.
Getting Your API Key
Step 1: Access Google AI Studio
- Go to Google AI Studio
- Sign in with your Google account
- If prompted, accept the terms of service
Step 2: Create an API Key
- Click Create API Key
- Select an existing Google Cloud project or create a new one
- Click Create API key in new project (or select existing)
- Copy the generated API key immediately
Important: The key is only shown once. Store it securely. If you lose it, you'll need to create a new one.
API Key Format
Valid Gemini API keys:
- Start with
AIza - Are 39 characters long
- Example:
AIzaSyD-EXAMPLE-KEY-1234567890abcdef
Configuring the API Key
macOS and Linux
Add the API key to your shell profile:
For zsh (macOS default):
echo 'export GEMINI_API_KEY="AIzaSy..."' >> ~/.zshrc
source ~/.zshrc
For bash:
echo 'export GEMINI_API_KEY="AIzaSy..."' >> ~/.bashrc
source ~/.bashrc
Verify it's set:
echo $GEMINI_API_KEY
# Should display your key
Windows
Option 1: PowerShell (Current Session)
$env:GEMINI_API_KEY = "AIzaSy..."
Option 2: Permanent (System Environment)
# Run as Administrator
[System.Environment]::SetEnvironmentVariable("GEMINI_API_KEY", "AIzaSy...", "User")
Option 3: GUI
- Press
Win + X, select System - Click Advanced system settings
- Click Environment Variables
- Under "User variables", click New
- Variable name:
GEMINI_API_KEY - Variable value: Your API key
- Click OK to save
Restart your terminal after setting the variable.
Using a .env File (Alternative)
For project-specific configuration:
# Create .env file in your project
echo 'GEMINI_API_KEY=AIzaSy...' > .env
# Add to .gitignore (important!)
echo '.env' >> .gitignore
Gemini CLI automatically loads .env files from the current directory.
Verifying Your Setup
Test that everything is configured correctly:
# Check environment variable
gemini --version
# Test with a simple prompt
gemini "What is 2 + 2?"
If successful, you'll see Gemini's response. If not, see troubleshooting below.
Troubleshooting API Key Issues
Error: "API key not valid"
Causes and fixes:
-
Key copied incorrectly
# Check for extra whitespace echo "[$GEMINI_API_KEY]" # Should show [AIzaSy...] with no spaces before/after -
Key from wrong project
- Go to AI Studio API Keys
- Verify the key exists and matches what you configured
-
Gemini API not enabled
- Go to Google Cloud Console
- Enable the "Generative Language API"
-
Key was deleted or regenerated
- Create a new key from AI Studio
- Update your environment variable
Error: "GEMINI_API_KEY environment variable not set"
Fix:
# Check if variable exists
echo $GEMINI_API_KEY
# If empty, the profile wasn't sourced
source ~/.zshrc # or ~/.bashrc
# If still empty, check the file
grep GEMINI ~/.zshrc
Error: "Quota exceeded" or "Rate limit"
You've hit the free tier limits:
| Model | Free Tier Limit |
|---|---|
| Gemini 1.5 Flash | 15 requests/minute |
| Gemini 1.5 Pro | 2 requests/minute |
Options:
- Wait 60 seconds for the limit to reset
- Switch to Flash model for higher limits:
gemini --model gemini-1.5-flash "prompt" - Upgrade to paid tier in Google Cloud Console
Error: "Permission denied"
Your Google account may not have access:
- Verify you're using a personal Google account (not Workspace)
- Check if Gemini is available in your region
- Accept the terms of service at AI Studio
API Key Security Best Practices
Do:
- Store the key in environment variables, not in code
- Add
.envto.gitignore - Use different keys for development and production
- Rotate keys periodically
Don't:
- Commit API keys to version control
- Share keys in chat, email, or documentation
- Use the same key across multiple projects
- Expose keys in client-side code
If Your Key Is Compromised
- Go to AI Studio API Keys
- Delete the compromised key
- Create a new key
- Update your environment variables
- Review usage logs for unauthorized access
Using Multiple API Keys
For different projects or environments:
# In ~/.zshrc
export GEMINI_API_KEY_DEV="AIzaSy...dev"
export GEMINI_API_KEY_PROD="AIzaSy...prod"
# Default to dev
export GEMINI_API_KEY=$GEMINI_API_KEY_DEV
Switch between them:
# Use production key temporarily
GEMINI_API_KEY=$GEMINI_API_KEY_PROD gemini "production query"
API Key vs OAuth Authentication
Gemini CLI supports two authentication methods:
| Feature | API Key | OAuth (gcloud) |
|---|---|---|
| Setup complexity | Simple | Moderate |
| Best for | Personal use, CLI | Enterprise, Vertex AI |
| Rate limits | Free tier limits | Based on billing |
| Project association | Automatic | Manual configuration |
For most CLI users, API key authentication is recommended.
Next Steps
- Install Gemini CLI if you haven't already
- Configure MCP Integrations to extend capabilities
- Use YOLO Mode for auto-approving commands
- Set Up Vertex AI Enterprise for organizational use