Geminibeginner

Gemini CLI API Key Setup: Get and Configure Your Google AI API Key

Complete guide to setting up your Gemini CLI API key. Get a Google AI Studio API key, configure GEMINI_API_KEY environment variable, and troubleshoot authentication errors.

8 min readUpdated January 2026

Want us to handle this for you?

Get expert help →

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

  1. Go to Google AI Studio
  2. Sign in with your Google account
  3. If prompted, accept the terms of service

Step 2: Create an API Key

  1. Click Create API Key
  2. Select an existing Google Cloud project or create a new one
  3. Click Create API key in new project (or select existing)
  4. 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

  1. Press Win + X, select System
  2. Click Advanced system settings
  3. Click Environment Variables
  4. Under "User variables", click New
  5. Variable name: GEMINI_API_KEY
  6. Variable value: Your API key
  7. 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:

  1. Key copied incorrectly

    # Check for extra whitespace
    echo "[$GEMINI_API_KEY]"
    # Should show [AIzaSy...] with no spaces before/after
    
  2. Key from wrong project

  3. Gemini API not enabled

  4. 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:

ModelFree Tier Limit
Gemini 1.5 Flash15 requests/minute
Gemini 1.5 Pro2 requests/minute

Options:

  1. Wait 60 seconds for the limit to reset
  2. Switch to Flash model for higher limits: gemini --model gemini-1.5-flash "prompt"
  3. Upgrade to paid tier in Google Cloud Console

Error: "Permission denied"

Your Google account may not have access:

  1. Verify you're using a personal Google account (not Workspace)
  2. Check if Gemini is available in your region
  3. Accept the terms of service at AI Studio

API Key Security Best Practices

Do:

  • Store the key in environment variables, not in code
  • Add .env to .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

  1. Go to AI Studio API Keys
  2. Delete the compromised key
  3. Create a new key
  4. Update your environment variables
  5. 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:

FeatureAPI KeyOAuth (gcloud)
Setup complexitySimpleModerate
Best forPersonal use, CLIEnterprise, Vertex AI
Rate limitsFree tier limitsBased on billing
Project associationAutomaticManual configuration

For most CLI users, API key authentication is recommended.


Next Steps

Frequently Asked Questions

Find answers to common questions

Get your API key from Google AI Studio at https://aistudio.google.com/apikey. Sign in with your Google account, click 'Create API Key', select a project (or create one), and copy the generated key. The key starts with 'AIza'.

Need Professional IT & Security Help?

Our team of experts is ready to help protect and optimize your technology infrastructure.