In this categoryGemini ยท 24
- How to Install the Gemini CLI and Run Your First PromptStart
- How to Get a Gemini API Key from Google AI Studio
- How to Call the Gemini API from Python
- How to Give Gemini CLI Project Context with a GEMINI.md File
- How to Choose the Right Gemini Model for Coding Tasks
- How to Fix a Bug in Your Codebase with the Gemini CLI
- How to Set Up Gemini Code Assist in VS Code
- How to Stream Gemini Responses in Node.js
- How to Feed a Long Document into Gemini's Long Context
- How to Run the Gemini CLI Non-Interactively in Scripts
- How to Analyze a Video File with the Gemini API
- How to Connect an MCP Server to the Gemini CLI
- How to Fix Common Gemini CLI Authentication Errors
- How to Cache Long Context in the Gemini API to Cut Costs
- How to Summarize a YouTube Video with Gemini
- How to Analyze PDFs and Docs in the Gemini Chat App
- How to Extract Data from Images and Screenshots with Gemini
- How to Generate a Short Video with Veo in Gemini
- How to Ground Gemini Answers in Your Own Documents
- How to Use Gemini Inside Google Docs to Draft Faster
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
22:15
16:10
12:25
14:45
20:30
35:20New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.