Get a Gemini API Key and Set the GEMINI_API_KEY Environment
Create an API key in Google AI Studio, add it as the GEMINI_API_KEY environment variable, and authentication is ready.
- Category
- Beginner guide
- Official sources
- 5
- Read time
- 5 min
- Last checked
- 2026.09.24
Gemini API Authentication Environment and Account Conditions

To apply the Gemini API to a project, obtain a dedicated authentication key from Google AI Studio and register it as a system environment variable. Writing the API key directly in source code creates a security risk when the repository is shared, so referencing an environment variable is recommended.
Key issuance procedures and permission conditions vary by account type and operating environment. Before starting, compare the account conditions table below to confirm whether permission approval is required.
| Category | Personal Google Account | Organizational Google Workspace Account |
|---|---|---|
| Prerequisite | Google account login | AI Studio is on by default for all editions (an admin can turn it off) |
| Issuance Path | Direct to Google AI Studio | Google Cloud Console integration required |
| Security Setting | Personal environment variable registration | Project-level IAM permission separation |
| Exception | Immediate issuance possible | No access if an admin turned AI Studio off |
For a personal account, go to the Google AI Studio website and generate the key. If AI Studio does not open on an organizational account, an admin has turned it off, so ask the admin to enable it.
Register the GEMINI_API_KEY Variable and Python Call Code
When setting the environment variable, use the name GEMINI_API_KEY. Loading the configured key from source code allows the model to be called securely without exposing authentication information.
Below are the Windows Command Prompt command to register the environment variable and a Python integration code example.
setx GEMINI_API_KEY "YOUR_GEMINI_API_KEY_HERE"
import os from google import genai
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY")) response = client.models.generate_content(model="gemini-3.8-flash", contents="Hello World") print(response.text)
When using the integration example above, replace YOUR_GEMINI_API_KEY_HERE with the actual issued key string and run the command. After execution, verify that the response text appears in the terminal.
5-Step Procedure from AI Studio Access to Windows Environment Variable Registration
API key issuance and system variable registration are performed in the Google AI Studio interface and the Windows System Settings menu.
- Open a browser, go to the Google AI Studio website, and sign in with your Google account.
- Click the Get API key button at the top of the left menu and select Create API key.
- Copy the displayed API key string and save it temporarily in a secure note.
- Type Edit the system environment variables in the Windows search bar, run it, and click the Environment Variables (N) button.
- Under User variables, click New (N), enter GEMINI_API_KEY as the variable name, paste the copied key as the variable value, and click OK.
After registration, close and reopen any running Command Prompt or development editor so the changed environment variable takes effect.
Git Repository and Code Security Exclusion Settings
The issued API key must not be uploaded to a Git repository or external web server. If the authentication key leaks to a public repository, a third party can consume the quota and exceed the account limit.
Create a .gitignore file in the project root folder and add the .env string to it. If environment variables are managed in a separate .env file, configure it so that file is excluded from version control.
Do not leave the API key string as a comment or string constant in source code. In team projects, do not share the key itself; instruct each member to register their own environment variable.
API Key Authentication Error Diagnosis and Resolution
If an error occurs after registering the key and running the code, check the terminal error message and take action.
If an API_KEY_INVALID error occurs, verify that the variable value contains no spaces or typos. If the variable name was mistyped, correct the string in the os.environ.get("GEMINI_API_KEY") call to match exactly, including case.
If 403 PERMISSION_DENIED appears, the key's permissions or project access are the problem: check which project the key belongs to in AI Studio and create a new key if needed. Keys work per project, and if one may have leaked, replace it immediately.
In a correctly configured environment, running echo %GEMINI_API_KEY% in the terminal outputs the configured key string normally.
Revision history · 2026-09-24
This article was revised against the provider’s official documentation. Korean note
Sources
ai.google.dev — Using Gemini API keys (2026-09-24)
ai.google.dev — Gemini API libraries (2026-09-24)
ai.google.dev — Gemini models (2026-09-24)
ai.google.dev — Gemini API and Google Workspace (2026-09-24)
ai.google.dev — API errors (2026-09-24)
Open provider document