Back to blog
AIGuides & Tutorials

How to Get Your Gemini API Key

This guide walks you through everything from creating your API key in AI Studio to testing your first request, setting up restrictions, and preparing for production on Google Cloud. A clear, practical walkthrough for developers who want to get started fast and securely.

Kateryna PoryvayKateryna Poryvay

Kateryna Poryvay

6 min read
How to Get Your Gemini API Key

Step 1: Access Google AI Studio

Navigate to aistudio.google.com and sign in with your Google account. Google AI Studio is the quickest way to start experimenting with Gemini models and obtain an API key for development purposes.

For production use, you'll eventually want to migrate to Google Cloud Platform's Vertex AI, but AI Studio is perfect for getting started quickly with minimal setup.

Step 2: Accept Terms and Conditions

On your first visit to Google AI Studio, you'll need to:

  • Review and accept Google's Generative AI terms of service
  • Confirm your region (Gemini API availability varies by location)
  • Acknowledge the data usage and privacy policies
  • Choose whether to share usage analytics with Google

Step 3: Navigate to API Keys Section

How to Get Your Gemini API Key 1

Once in Google AI Studio, locate the "Get API key" button in the left sidebar or click on the key icon in the navigation menu. This will take you to the API key management interface.

The interface displays:

  • Your existing API keys (if any)
  • Associated Google Cloud projects
  • Usage statistics for each key
  • Options to create new keys or delete existing ones

Step 4: Create Your API Key

How to Get Your Gemini API Key 2

Click on "Create API key" and you'll be presented with two options: Option A: Create key in new project (Recommended for beginners)

  • Google automatically creates a new Google Cloud project for you
  • Simpler setup with no additional configuration required
  • Ideal for testing and development

Option B: Create key in existing project

  • Select from your existing Google Cloud projects
  • Better for production environments
  • Allows integration with other Google Cloud services

Select your preferred option and click "Create API key".

Step 5: Save Your API Key

How to Get Your Gemini API Key 3

Important: Your API key will be displayed immediately. Copy and store it securely:

AIza_____________________

Google AI Studio keys typically start with "AIza". Unlike some platforms, you can view your key again later in the console, but it's still best practice to store it securely immediately.

How to Get Your Gemini API Key 4

Storage recommendations:

  • Environment variable: GEMINI_API_KEY or GOOGLE_AI_API_KEY
  • Use Google Secret Manager for production deployments
  • Store in .env files (remember to add to .gitignore)
  • Never commit keys to version control

Step 6: Configure API Restrictions (Recommended)

For security, configure API key restrictions:

  1. Click on your newly created key in the console
  2. Navigate to "API restrictions" or "Edit API key"
  3. Configure the following:
    • Application restrictions: Limit usage by IP address, HTTP referrers, or mobile apps
    • API restrictions: Restrict key to only Gemini/Generative AI APIs
    • Usage quotas: Set requests per minute/day limits

Step 7: Test Your API Key

Verify your key with a simple test request using curl:


curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=YOUR_API_KEY_HERE" \
  -H "Content-Type: application/json" \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{
        "text": "Hello, Gemini!"
      }]
    }]
  }'

Replace YOUR_API_KEY_HERE with your actual API key. A successful response will contain Gemini's generated content.

Step 8: Understand Pricing and Quotas

Google offers generous free tiers for Gemini:

Free Tier Limits (as of current offering):

  • 60 requests per minute (RPM)
  • 1 million tokens per month
  • Available for Gemini Pro and Gemini Pro Vision

Monitor your usage:

  1. Check Google AI Studio dashboard for quick usage stats
  2. For detailed metrics, visit Google Cloud Console
  3. Set up billing alerts before reaching limits
  4. Enable email notifications for quota approaching

Step 9: Set Up Google Cloud Platform (For Production)

For production use, migrate to Google Cloud Platform:

  1. Go to console.cloud.google.com
  2. Create or select a project
  3. Enable the "Generative Language API"
  4. Navigate to "APIs & Services" > "Credentials"
  5. Create credentials > API Key
  6. Configure billing (required for production use)

This provides:

  • Higher rate limits
  • Better monitoring and logging
  • Integration with other Google Cloud services
  • Enterprise support options

Security Best Practices

Protect your Gemini API keys:

  • Use API key restrictions: Always configure application and API restrictions
  • Rotate keys regularly: Generate new keys every 60-90 days
  • Monitor usage: Check for unusual patterns in the Google Cloud Console
  • Use service accounts for production: More secure than API keys for server applications
  • Implement client-side protections: Never expose keys in frontend applications

Managing Multiple Environments

Organize keys for different purposes:

  • Development: Unrestricted key for local testing
  • Staging: Key with moderate restrictions for pre-production
  • Production: Highly restricted key with IP allowlisting
  • CI/CD: Separate key for automated testing with specific quotas

Use naming conventions:

  • gemini-dev-frontend-2025
  • gemini-prod-backend-api
  • gemini-staging-mobile-app

Troubleshooting Common Issues

"API key not valid" error:

  • Verify the key is correctly copied (no extra spaces)
  • Check if the Generative Language API is enabled in your project
  • Ensure your key hasn't been deleted or regenerated

Quota exceeded errors:

  • Monitor your usage in Google AI Studio
  • Check both per-minute and monthly limits
  • Consider upgrading to paid tier for higher limits
  • Implement request queuing and retry logic

Regional availability issues:

  • Gemini API isn't available in all regions
  • Check Google's documentation for current availability
  • Consider using VPN or proxy for development (not recommended for production)

Advanced Configuration

Using OAuth 2.0 instead of API keys (More secure for production):


from google.auth import default
from google.auth.transport.requests import Request

credentials, project = default()
credentials.refresh(Request())
# Use credentials.token as your authentication

Setting up Vertex AI for enterprise use:

  • Provides additional models and capabilities
  • Better integration with Google Cloud services
  • Enhanced security and compliance features
  • Custom model fine-tuning options

Monitoring and Analytics

Track your API usage effectively:

  1. Google AI Studio Dashboard:

    • Quick overview of requests and tokens
    • Recent activity and error rates
    • Model-specific usage breakdown
    • Google Cloud Console:
  2. Detailed metrics and logs

    • Custom dashboards and alerts
    • Cost analysis and forecasting
    • API latency monitoring
  3. Set up alerts:

    • Approaching quota limits
    • Unusual usage patterns
    • Error rate spikes
    • Billing thresholds

Migration from Other Providers

Key differences when switching to Gemini:

  • API structure: Different request/response format than OpenAI or Anthropic
  • Model names: Use "gemini-pro" or "gemini-pro-vision" instead of GPT or Claude
  • Authentication: API key in URL parameter vs headers
  • Token counting: Different tokenization method
  • Content filtering: Built-in safety settings that may need adjustment

Available Models and Capabilities

With your API key, access various Gemini models:

  • Gemini Pro: Text generation, chat, and reasoning
  • Gemini Pro Vision: Multimodal understanding (text + images)
  • Gemini Ultra: Most capable model (limited availability)
  • Embedding models: For semantic search and similarity

Each model has different pricing and rate limits, so choose based on your specific needs.

Next Steps

With your API key ready:

  • Explore the official Gemini API documentation
  • Test different models in Google AI Studio's playground
  • Implement streaming responses for real-time applications
  • Explore multimodal capabilities with Gemini Pro Vision
  • Set up proper error handling and retry mechanisms
  • Consider implementing caching to optimize token usage

Additional Resources

Remember to regularly review Google's terms of service and usage policies. The Gemini API is rapidly evolving with new features and models being released frequently, so stay informed through official channels for the latest updates and capabilities.

Ready to get started?

Scale your integration strategy and deliver the integrations your customers need in record time.

Ready to get started?
Talk to an expert

Trusted by fast-moving product & engineering teams

Nmbrs
Benefex
Invoice2go by BILL
Trengo
Ponto | Isabel Group
Apideck Blog

Insights, guides, and updates from Apideck

Discover company news, API insights, and expert blog posts. Explore practical integration guides and tech articles to make the most of Apideck's platform.

Why Building Native Integrations In-House Is Slowing Your Product Down
Unified APIIndustry insights

Why Building Native Integrations In-House Is Slowing Your Product Down

Building integrations in-house feels smart at first, until it slows your entire product down. This article reveals the hidden costs of native integrations and shows how unified APIs let you scale faster, cut maintenance, and keep your team focused on what really matters.

Kumar Harsh

9 min read
Apideck Is Now an Official Workday Partner
Unified APICompany news

Apideck Is Now an Official Workday Partner

Apideck is now an official Workday Silver Partner. This partnership gives us direct access to Workday’s platform technologies, enabling more secure, scalable, and reliable integrations. Learn how Apideck’s unified APIs simplify Workday connectivity and help SaaS companies accelerate time-to-market and win more enterprise deals.

Bernard Willems

Bernard Willems

3 min read
How to Connect with the HubSpot API
Unified APIGuides & TutorialsCRM

How to Connect with the HubSpot API

Learn how to connect to HubSpot API with OAuth 2.0, handle rate limits, manage lifecycle stages, and avoid common integration pitfalls. Complete implementation guide with TypeScript examples, webhook setup, and solutions to undocumented API quirks.

Saurabh Rai

Saurabh Rai

13 min read