Gemini

Authentication

Secure access to Gemini's APIs using API keys and environment variables. Learn how to authenticate requests for all Gemini features.

Create API Key

Getting Your API Key

🔑

Create API Key

Generate your authentication credentials through the Developer Console. This key grants programmatic access to all Gemini capabilities.

🛡️

Set Environment Variable

Store your API key securely using environment variables rather than hardcoded values. This protects credentials in both development and production environments.

{`# Linux/macOS export GEMINI_API_key="yourkeyhere" # Windows (CMD) setx GEMINI_API_key "yourkeyhere" # PowerShell [Environment]::SetEnvironmentVariable("GEMINI_api_key", "yourkeyhere", "User")`}

Verify Authentication

Confirm your setup works by making a test request. This verifies both the API key format and environment variable are correctly configured.

{`curl -X POST https://api.gemini.com/v1/complete \\ -H "Authorization: Bearer $GEMINI_api_key" \\ -d '{"prompt":"Test request"}'`}

Using API Keys

Authentication Headers

{`Authorization: Bearer YOUR_API_KEY`}

All API requests must include the Authorization header with a valid API key. Unauthenticated requests will return a 401 error.

CLI Authentication

{`gemini config set api_key "your_api_key_here"`}

Python Requests

{`import os import requests headers = { 'Authorization': f'Bearer {os.getenv("GEMINI_api_key")}' } response = requests.post('https://api.gemini.com/v1/complete', headers=headers, json={'prompt': 'Hello'})`}

Security & Best Practices

🔒

Rotate Keys Regularly

Change your API key every 30 days and regenerate credentials if you suspect any potential compromise.

⚠️

Never Share Keys Publicly

Don't include credentials in client-side code, open-source repositories, or public documentation.

🛡️

Use Project-Specific Keys

Create separate API keys for different applications or teams within your organization.

🛡️

Enable IP Whitelisting

Restrict API access to specific IP ranges for enterprise deployments.

CLI & SDK Authentication

Authentication Configuration

The Gemini CLI and SDK automatically look for the API key in environment variables. You can override this with explicit configuration files.

Linux/macOS

{`echo 'export GEMINI_API_KEY="your_api_key"' >> ~/.bashrc source ~/.bashrc`}

Windows

{`[System.Environment]::SetEnvironmentVariable("GEMINI_API_KEY", "your_api_key", "Machine")`}

Ready to Authenticate?

Get your free API key and start building with Gemini. All features require authentication for secure access.

Get API Key