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.
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.
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.
export GEMINI_API_KEY="AIza...your-key-here..."
# make it permanent (zsh):
echo 'export GEMINI_API_KEY="AIza...your-key-here..."' >> ~/.zshrcStep 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.
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
9:47
6:41
12:38
12:00
10:00
1:42:18