Configure Your API Key
Once you have your Google AI API key, you need to configure it with Gemini CLI. Here are several methods to do so.
Method 1: Environment Variable (Recommended)
The most secure way is to set your API key as an environment variable:
For macOS/Linux:
# Add to ~/.bashrc, ~/.zshrc, or ~/.profile
export GEMINI_API_KEY="your-api-key-here"
# Reload your shell configuration
source ~/.bashrc # or ~/.zshrc
For Windows:
Option 1: Command Prompt (temporary)
set GEMINI_API_KEY=your-api-key-here
Option 2: PowerShell (temporary)
$env:GEMINI_API_KEY="your-api-key-here"
Option 3: System Environment Variables (permanent)
- Open System Properties → Advanced → Environment Variables
- Click "New" under User variables
- Variable name: GEMINI_API_KEY
- Variable value: your-api-key-here
- Click OK and restart your terminal
Method 2: Gemini Config Command
Use Gemini CLI's built-in configuration command:
gemini config set api-key YOUR_API_KEY
This stores your API key in a configuration file at ~/.gemini/config.json
Method 3: Configuration File
Create or edit the configuration file directly:
1. Create the config directory:
# macOS/Linux
mkdir -p ~/.gemini
# Windows
mkdir %USERPROFILE%\.gemini
2. Create config.json:
{ "apiKey": "your-api-key-here", "model": "gemini-pro", "temperature": 0.7, "maxTokens": 1000 }
Method 4: Per-Command API Key
You can also provide the API key for individual commands:
gemini --api-key YOUR_API_KEY "Your prompt here"
Warning: This method may expose your API key in shell history. Use environment variables or config files for better security.
Verify Your Configuration
Test that your API key is properly configured:
1. Check configuration:
gemini config show
2. Test with a simple command:
gemini "Hello, can you hear me?"
3. Check API status:
gemini status
Configuration Priority
Gemini CLI checks for API keys in this order:
- 1Command-line flag
--api-key flag in the command
- 2Environment variable
GEMINI_API_KEY environment variable
- 3Configuration file
~/.gemini/config.json
Troubleshooting
Error: API key not found
Make sure you've set the API key correctly:
# Check if environment variable is set
echo $GEMINI_API_KEY # macOS/Linux
echo %GEMINI_API_KEY% # Windows CMD
echo $env:GEMINI_API_KEY # Windows PowerShell
Error: Invalid API key
Verify that you've copied the entire key without any extra spaces or characters. Try regenerating a new key in Google AI Studio if the problem persists.
You're All Set!
Your API key is now configured. Start using Gemini CLI: