Skip to content

Google Gemini API Key: Create, Secure, and Use

By the end of this guide you will have a Google Gemini API key stored in an environment variable and a working Python and cURL call that returns a Gemini response. Keys are created through Google AI Studio rather than through the Google Cloud Console — no billing account is required to get started.

Requirement Notes
Google account Sign in at aistudio.google.com
Python 3.9+ or Node.js 18+ To run the verification samples
Paid tier (optional) Required for higher quotas and production workloads

The free tier provides a generous monthly quota (see §Available models and quotas). No credit card is needed to create a key.

  1. Go to aistudio.google.com and sign in with your Google account.
  2. Click Get API key in the left sidebar.
  3. Click Create API key in new project (Google creates a Cloud project automatically) or Create API key in existing project if you already have one.
  4. Copy the key immediately — it starts with AIzaSy. You can retrieve it again from the AI Studio keys page, but copying it now saves a trip back.

An unrestricted key can be used to call any Google API. Restricting it to the Gemini API limits the blast radius if the key leaks.

  1. Open console.cloud.google.com/apis/credentials and select your project.
  2. Click the pencil icon next to your key.
  3. Under API restrictions, select Restrict key and choose Generative Language API.
  4. Under Application restrictions, choose IP addresses if the key will only be used from a known server, or leave it unrestricted for local development.
  5. Click Save.

Linux / macOS (current shell session):

Terminal window
export GOOGLE_API_KEY="AIzaSy-your-key-here"

Windows PowerShell (current session):

Terminal window
$env:GOOGLE_API_KEY = "AIzaSy-your-key-here"

.env file for local development (add .env to .gitignore):

GOOGLE_API_KEY=AIzaSy-your-key-here

Load it in Python with python-dotenv:

from dotenv import load_dotenv
load_dotenv()

For production, use your platform’s secret store: Google Secret Manager, AWS Secrets Manager, Azure Key Vault, or equivalent.

Install the SDK, then run a minimal request.

Terminal window
pip install google-generativeai

Save as check_key.py and run with python check_key.py (Python 3.9+):

import google.generativeai as genai
import os
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content("Reply with the word VERIFIED.")
print(response.text)
# Expected output: VERIFIED

Or use cURL:

Terminal window
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=$GOOGLE_API_KEY" \
-H "Content-Type: application/json" \
-d '{"contents":[{"parts":[{"text":"Reply with the word VERIFIED."}]}]}'

A successful response returns HTTP 200 with candidates[0].content.parts[0].text containing your reply.

Model Context Free tier (RPM / RPD) Best for
gemini-2.5-pro 1 M tokens 5 / 25 Complex reasoning, long-document analysis
gemini-2.5-flash 1 M tokens 15 / 1,500 Fast general-purpose tasks
gemini-2.0-flash 1 M tokens 15 / 1,500 High-volume applications
text-embedding-004 2,048 tokens 1,500 / — Semantic search, RAG

Free-tier quotas and model availability change. Check ai.google.dev/pricing for current figures before building production rate-limit logic.

View keys: The AI Studio keys page and the Cloud Console credentials page both show your keys. The Cloud Console also shows last-used timestamps.

Delete a key: Click the trash icon in AI Studio or the Cloud Console and confirm. Applications using that key immediately receive API_KEY_INVALID errors.

Enabling the API: If you created the project manually rather than through AI Studio, you may need to enable the Generative Language API first. Go to console.cloud.google.com/apis/library, search for Generative Language API, and click Enable.

Error Likely cause Fix
API_KEY_INVALID Key copied incorrectly or deleted Verify the key value in the AI Studio keys page.
PERMISSION_DENIED Generative Language API not enabled Enable the API in Cloud Console › APIs & Services.
RESOURCE_EXHAUSTED Free-tier quota exceeded Wait for the quota to reset, or upgrade to a paid tier.
Invalid API key restrictions IP or referrer not in allowlist Update restrictions in Cloud Console › Credentials.