GeminiBeginner

How to Get a Gemini API Key from Google AI Studio

Create a free Gemini API key in Google AI Studio and store it safely as an environment variable.

6 minBeginner

Most Gemini coding setups, including scripts, the CLI in API mode, and SDK calls, authenticate with an API key. You generate keys in Google AI Studio, the free web console for the Gemini API. This guide walks through creating one and storing it so it does not leak into your code.

What you need

  • A Google account
  • A web browser
  • A terminal for setting an environment variable
  • About 5 minutes

Step 1: Open Google AI Studio

Go to aistudio.google.com and sign in with your Google account. AI Studio is the home for the Gemini API, separate from the consumer Gemini app. Accept the terms if you are prompted on first visit.

Step 2: Create an API key

Click Get API key in the left sidebar, then Create API key. You can attach the key to an existing Google Cloud project or let AI Studio create one for you. The key appears once, in a string starting with the letters AIza.

Google AI Studio
API keys
Name: My first key
Project: gen-lang-client-0123456789
Key: AIza**************************** [Copy]
[Create API key]
The Get API key panel with a newly created key.
Treat the key like a password
Anyone with this key can spend against your quota. Never paste it into source code, commits, or screenshots. If a key leaks, delete it in AI Studio and create a new one.

Step 3: Store it as an environment variable

The Gemini CLI and SDKs read the key from the GEMINI_API_KEY environment variable. Export it in your shell session, or add the line to your shell profile so it loads every time.

terminal
export GEMINI_API_KEY="AIza...your-key-here..."
# make it permanent (zsh):
echo 'export GEMINI_API_KEY="AIza...your-key-here..."' >> ~/.zshrc

Step 4: Verify the key works

Confirm the variable is set and that Gemini accepts it with a single curl call to the REST endpoint. A JSON response with a candidates field means the key is valid.

zsh - verify
$echo $GEMINI_API_KEY
AIza...your-key-here...
$curl "https://generativelanguage.googleapis.com/v1beta/models?key=$GEMINI_API_KEY"
{ "models": [ { "name": "models/gemini-2.5-flash", ... } ] }
$

Result

You have a working Gemini API key stored in an environment variable. Any tool that reads GEMINI_API_KEY, including the CLI in non-interactive mode and the official SDKs, can now authenticate without you pasting the key into files.

Watch related tutorials

Tags
#api-key#ai-studio#setup#environment#security