← Back to Blog
API Tutorial • Sep 2, 2026

Gemini image API: Setting Environment Variables on Windows

Learn how to set Gemini image API environment variables on Windows, from session-only exports to persistent user-level setup with setx and PowerShell.

When you call the Gemini image API from Python on Windows, the SDK reads your API key from an environment variable rather than from hard-coded text. The official quickstart demonstrates the Unix export syntax (Google AI quickstart), but Windows requires a different set of commands to set, persist, and verify that variable. This guide walks through every step that matters for a clean local setup on a Windows machine.

What the script actually looks for

The Nano Banana reference client in the project's nano_api.py shows the variable names the Python code is expected to read. The relevant lines are:

api_key = os.getenv("GOOGLE_API_KEY") or os.getenv("GEMINI_API_KEY")
if not api_key:
    raise RuntimeError("Set GOOGLE_API_KEY or GEMINI_API_KEY before running this script.")

Two practical takeaways follow directly from this source:

  • Either GOOGLE_API_KEY or GEMINI_API_KEY is accepted. The Google docs emphasize GEMINI_API_KEY; the reference client accepts both for compatibility.
  • If neither variable is set, the script aborts with a clear RuntimeError. There is no implicit fallback to a config file or a default key.

This means your Windows setup only needs to define one of those two names for the variable to work with the nano_api.py example.

Set the variable for the current session

A session-scoped variable lives only in the window where you set it. This is the safest way to test a new key without affecting other projects.

In Command Prompt (cmd.exe):

set GEMINI_API_KEY=your_api_key_here

In PowerShell:

$env:GEMINI_API_KEY = "your_api_key_here"

The value is available only until you close the shell. Open a new window and the variable is gone, which is convenient when you want to rotate a key quickly.

Persist the variable across sessions

For ongoing development you usually want the variable to survive logouts and reboots. Windows offers two user-level mechanisms.

Command Prompt, user-level persistence:

setx GEMINI_API_KEY "your_api_key_here"

PowerShell, user-level persistence:

[System.Environment]::SetEnvironmentVariable(
    "GEMINI_API_KEY",
    "your_api_key_here",
    "User"
)

Both approaches write to the Windows user environment registry hive. After running either command, close and reopen your terminal so the new value is loaded into the process. The machine-level equivalent uses "Machine" instead of "User" in PowerShell and requires an elevated shell; for personal development, the user scope is enough.

Important: setx silently truncates values that contain certain delimiters and does not require quotes for simple alphanumeric keys. If your key contains unusual characters, wrap it in double quotes exactly as shown above, and avoid the export keyword that the Google quickstart shows for POSIX shells.

Verify the variable is visible

Before you run any Python code, confirm the shell sees the value.

Command Prompt:

echo %GEMINI_API_KEY%

PowerShell:

echo $env:GEMINI_API_KEY

If the line is empty, the variable is not defined in the current scope. The most common causes are running the verification command in a window that was open before you used setx, or mixing the two syntaxes (%VAR% in PowerShell, $env:VAR in Command Prompt).

Wire the variable into a Gemini image API call

The Nano Banana reference script (nano_api.py) shows how the key is consumed by the SDK once the variable is in place:

import base64
import mimetypes
import os
from pathlib import Path
from google import genai

api_key = os.getenv("GOOGLE_API_KEY") or os.getenv("GEMINI_API_KEY")
client = genai.Client(api_key=api_key)
interaction = client.interactions.create(
    model="gemini-3.1-flash-image",
    input=[
        {"type": "text", "text": prompt},
        {
            "type": "image",
            "data": base64.b64encode(image_bytes).decode("utf-8"),
            "mime_type": mime_type,
        },
    ],
    response_format={"type": "image"},
)

A few notes on what this snippet does and does not tell you:

  • The image is sent as base64-encoded bytes with a guessed MIME type, matching the multimodal input format described in the Google quickstart.
  • response_format={"type": "image"} requests image output; the script then writes interaction.output_image.data to disk.
  • This is a hosted API call. The Gemini image model runs on Google's servers; the Python script only sends bytes over HTTPS. There is no local GPU involvement.

For the install step, the quickstart specifies pip install -U google-genai. Run that once in the Python environment you intend to use, then verify everything with a minimal call against a small local JPEG.

A practical Windows checklist

  1. Open the Windows shell you normally use for Python work.
  2. Run setx GEMINI_API_KEY "your_key" in Command Prompt or the PowerShell equivalent.
  3. Close and reopen the shell so the new registry entry is picked up.
  4. Verify with echo %GEMINI_API_KEY% or echo $env:GEMINI_API_KEY.
  5. Install the SDK: pip install -U google-genai.
  6. Run the reference client with a local image and editing prompt.

Where to go next

If the image comes back blank or the script prints No image returned, check the Nano Banana FAQ for prompt-shape and output troubleshooting. For a broader walkthrough of the editing flow used in nano_api.py, see the Nano Banana quickstart guide. If you would rather run an open-weight image model on your own hardware instead of using a hosted endpoint, the local-runs article covers the trade-offs between the cloud Gemini image API and self-hosted alternatives.

The environment variable is the smallest piece of the puzzle, but it is the one that breaks most often on Windows because the official quickstart only documents the Unix shell. Once it is in place, the rest of the Gemini image API Python workflow follows the same pattern as on any other platform.

Sources