Skip to content

How to Create a Google Gemini API Key | Complete Guide

🔑 How to Create a Google Gemini API Key

Section titled “🔑 How to Create a Google Gemini API Key”

Learn how to create and manage Google Gemini API keys to access Google’s powerful generative AI models, including Gemini Pro, Gemini Pro Vision, and other AI capabilities.


A Google Gemini API key allows you to:

  • Access Google’s Gemini AI models programmatically
  • Integrate generative AI into your applications
  • Use multimodal capabilities (text, images, code)
  • Build AI-powered chatbots and assistants

Before creating an API key, ensure you have:

  1. Google Account → Sign up at https://accounts.google.com
  2. Google Cloud Project → Access to Google AI Studio
  3. Internet Connection → For API access

🔹 Step-by-Step: Creating a Google Gemini API Key

Section titled “🔹 Step-by-Step: Creating a Google Gemini API Key”
  1. Go to Google AI Studio
  2. Sign in with your Google account
  3. Accept the terms of service if prompted
  1. Click on “Get API key” in the left sidebar
  2. You’ll see the API key management page
  1. Click “Create API key in new project”
  2. Google will automatically:
    • Create a new Google Cloud project
    • Enable the Gemini API
    • Generate your API key

Option B: Create API Key in Existing Project

Section titled “Option B: Create API Key in Existing Project”
  1. Click “Create API key in existing project”
  2. Select your existing Google Cloud project from the dropdown
  3. Click “Create API key”

⚠️ IMPORTANT: Save your API key immediately!

  1. Copy the generated API key
  2. Store it securely in a password manager or environment variable
  3. Never share it publicly or commit it to version control

Example API key format:

AIzaSyAbCdEfGhIjKlMnOpQrStUvWxYz1234567890

  1. Go to Google AI Studio
  2. Click “Get API key” in the left menu
  3. You’ll see a list of all your API keys with:
    • Key name (first few characters)
    • Associated project
    • Creation date
    • Actions (Delete option)
  1. Find the key you want to remove
  2. Click the trash icon next to it
  3. Confirm deletion

⚠️ Warning: Deleting a key will immediately stop all applications using it.


  • Store keys in environment variables (.env file)
  • Use API key restrictions in Google Cloud Console
  • Rotate keys periodically
  • Use different keys for development and production
  • Monitor API usage regularly
  • Hardcode API keys in source code
  • Commit keys to Git repositories
  • Share keys in public forums
  • Use the same key across all projects
  • Embed keys in client-side applications

Section titled “🔹 Restricting Your API Key (Recommended)”

For enhanced security, restrict your API key:

  1. Visit Google Cloud Console
  2. Select your project
  3. Go to “APIs & Services”“Credentials”
  1. Find your API key and click “Edit”

  2. Set Application restrictions:

    • None (Not recommended)
    • HTTP referrers (for websites)
    • IP addresses (for servers)
    • Android apps
    • iOS apps
  3. Set API restrictions:

    • Select “Restrict key”
    • Choose “Generative Language API” (Gemini API)
  4. Click “Save”


Section titled “Method 1: Environment Variable (Recommended)”

Linux/macOS:

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

Windows CMD:

Terminal window
set GOOGLE_API_KEY="AIzaSy-your-api-key-here"

Windows PowerShell:

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

Create a .env file in your project:

GOOGLE_API_KEY=AIzaSy-your-api-key-here

Load it in your code:

Python:

from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("GOOGLE_API_KEY")

Node.js:

require('dotenv').config();
const apiKey = process.env.GOOGLE_API_KEY;

import google.generativeai as genai
import os
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Hello, Gemini!")
print(response.text)
Terminal window
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"contents": [{
"parts": [{
"text": "Hello, Gemini!"
}]
}]
}'
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
async function run() {
const result = await model.generateContent("Hello, Gemini!");
console.log(result.response.text());
}
run();

ModelDescriptionBest For
gemini-proText generationChatbots, content creation
gemini-pro-visionMultimodal (text + images)Image analysis, visual Q&A
gemini-1.5-proEnhanced capabilitiesComplex reasoning tasks
gemini-1.5-flashFast responsesReal-time applications

  • 60 requests per minute (RPM)
  • 1,500 requests per day (RPD)
  • 1 million tokens per month
  • Higher rate limits
  • More tokens per request
  • Priority support

Check current pricing: https://ai.google.dev/pricing

  1. Go to Google Cloud Console
  2. Navigate to “APIs & Services”“Dashboard”
  3. View your API usage metrics

Solution:

  • Verify the key is copied correctly
  • Ensure the Generative Language API is enabled
  • Check if the key has been deleted

Solution:

  • Enable the Generative Language API in your project
  • Go to API Library
  • Search for “Generative Language API” and enable it

Issue 3: “RESOURCE_EXHAUSTED” (Rate Limit)

Section titled “Issue 3: “RESOURCE_EXHAUSTED” (Rate Limit)”

Solution:

  • You’ve exceeded free tier limits
  • Implement exponential backoff
  • Upgrade to paid tier for higher limits

Issue 4: “Invalid API Key Restrictions”

Section titled “Issue 4: “Invalid API Key Restrictions””

Solution:

  • Check your API key restrictions in Google Cloud Console
  • Ensure your IP address or referrer is allowed
  • Update restrictions if needed

If you get permission errors:

  1. Go to Google Cloud Console
  2. Select your project
  3. Navigate to “APIs & Services”“Library”
  4. Search for “Generative Language API”
  5. Click “Enable”

Terminal window
pip install google-generativeai
Terminal window
npm install @google/generative-ai
Terminal window
go get github.com/google/generative-ai-go


You now know how to: ✅ Create a Google Gemini API key
✅ Secure and restrict your keys
✅ Use the API in your applications
✅ Monitor usage and troubleshoot issues
✅ Access multimodal AI capabilities

Start building intelligent applications with Google Gemini! 🎯