Getting a Gemini API Key
How to Get a Google Gemini API Key: AI Studio and Vertex AI Setup
To use the Gemini API in your applications, you need an API key that authenticates your requests with Google's servers. There are two primary ways to obtain one: through Google AI Studio for quick experimentation, or through Google Cloud Vertex AI for production deployments. This guide covers both methods step by step.
Method 1: Google AI Studio (Recommended for Getting Started)
Google AI Studio is the fastest way to get a Gemini API key. It requires only a Google account and takes less than two minutes.
- Navigate to aistudio.google.com and sign in with your Google account.
- Click "Get API Key" in the left navigation sidebar.
- Click "Create API Key" and select an existing Google Cloud project, or let AI Studio create a new one automatically.
- Your API key will appear on screen. Copy it immediately and store it in a secure location such as an environment variable or a secrets manager.
Method 2: Google Cloud Vertex AI (For Production Use)
Vertex AI provides enterprise-grade access to Gemini models with additional features like VPC Service Controls, customer-managed encryption keys, and SLA guarantees. Authentication uses service accounts rather than simple API keys.
- Go to the Google Cloud Console and create or select a project.
- Enable the Vertex AI API by navigating to APIs & Services and searching for "Vertex AI API."
- Create a service account under IAM & Admin with the "Vertex AI User" role.
- Generate a JSON key file for the service account and download it.
- Set the environment variable to authenticate:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
# Using Vertex AI with Python
from google import genai
client = genai.Client(
vertexai=True,
project="your-project-id",
location="us-central1"
)
response = client.models.generate_content(
model="gemini-2.5-pro",
contents="Summarize the benefits of API gateways."
)
print(response.text)
AI Studio vs Vertex AI: Which Should You Choose?
| Feature | AI Studio | Vertex AI |
|---|---|---|
| Setup time | Under 2 minutes | 15-30 minutes |
| Authentication | Simple API key | Service account / OAuth |
| Free tier | Yes, generous limits | Limited free credits for new accounts |
| SLA | No | Yes (99.9%) |
| Best for | Prototyping, personal projects | Production, enterprise apps |
Securing Your API Key
Regardless of which method you use, API key security is critical. Follow these best practices:
- Never hardcode keys in your source code. Use environment variables or a secrets manager like Google Secret Manager, AWS Secrets Manager, or HashiCorp Vault.
- Restrict key permissions in the Google Cloud Console. Limit the key to only the Generative Language API.
- Set application restrictions to limit which IP addresses or referrers can use the key.
- Rotate keys regularly and revoke any keys that may have been exposed.
- Use .gitignore to ensure
.envfiles and credential files are never committed to version control.
# Store your key as an environment variable
export GEMINI_API_KEY="your-api-key-here"
# Reference it in your code
import os
api_key = os.environ.get("GEMINI_API_KEY")
Using Your API Key with a Relay Service
For developers who need reliable access to the Gemini API from regions with connectivity challenges, or who want to manage multiple API keys with load balancing and automatic failover, a relay service like claude4u.com simplifies key management. You configure your Gemini credentials once on the relay platform and use a single unified API key for all your requests, with built-in rate limit handling and retry logic.
Troubleshooting Common Key Issues
- API key not working: Verify the Generative Language API is enabled in your Google Cloud project.
- 403 Forbidden: Check that your key has not been restricted to specific IPs or referrers that do not match your current environment.
- Quota exceeded: Free tier limits reset daily. Enable billing to increase your quota or use multiple projects.
- Region restrictions: Some regions may have limited access to AI Studio. A relay service can help bypass geographic limitations.
Get Started with 轻舟 AI
Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more
Sign Up Free
轻舟 AI