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.

  1. Navigate to aistudio.google.com and sign in with your Google account.
  2. Click "Get API Key" in the left navigation sidebar.
  3. Click "Create API Key" and select an existing Google Cloud project, or let AI Studio create a new one automatically.
  4. 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.
AI Studio API keys work with the free tier, which includes 30 RPM for Gemini 2.5 Flash and 5 RPM for Gemini 2.5 Pro. This is enough for development and testing, but you will want to enable billing for production workloads.

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.

  1. Go to the Google Cloud Console and create or select a project.
  2. Enable the Vertex AI API by navigating to APIs & Services and searching for "Vertex AI API."
  3. Create a service account under IAM & Admin with the "Vertex AI User" role.
  4. Generate a JSON key file for the service account and download it.
  5. 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:

# 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")
If your API key is accidentally exposed in a public repository, revoke it immediately in the Google Cloud Console and generate a new one. Exposed keys can be exploited within minutes by automated scanners.

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

Get Started with 轻舟 AI

Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more

Sign Up Free