Skip to content

Setup - Google Colab & the Gemini API

Generative AI for Managers - TUHH Institute of Entrepreneurship · part of my Technology Management MBA · study notes for revision.


Every lab in this course runs in Google Colab (a free, browser-based notebook - no installs on my laptop) and calls the Gemini API (Google’s LLM service). This page is the one-time setup so the notebooks in Days 1-5 just work. It takes about 15 minutes.

A laptop + modern browser (Chrome)A Google account
  1. Go to colab.research.google.com and sign in with my Google account.

  2. Click New Notebook.

  3. In the first cell, type this and run it (Shift+Enter):

    print("Colab is working!")

    If it prints the message, Colab is ready.

  1. Open Google AI Studio and sign in.

  2. Find Get API key (or API keys) in the interface and create a new API key.

  3. Save it somewhere safe - a password manager is ideal. Treat it exactly like a password.

Step 3 - Store the key safely in Colab (Secrets)

Section titled “Step 3 - Store the key safely in Colab (Secrets)”

The right way to give a notebook the key without writing it into the code:

  1. In Colab, click the key icon (🔑 Secrets) in the left sidebar.

  2. Add a new secret - Name: GEMINI_API_KEY, Value: paste your key.

  3. Toggle Notebook access on for that secret.

  4. In a code cell, load it into an environment variable:

    from google.colab import userdata
    import os
    os.environ["GEMINI_API_KEY"] = userdata.get("GEMINI_API_KEY")
    print("Key loaded:", bool(os.environ.get("GEMINI_API_KEY")))

    If it prints Key loaded: True, you’re set.

Step 4 - Install the SDK and make a first call

Section titled “Step 4 - Install the SDK and make a first call”
  1. Install the Gemini SDK (the ! runs a shell command inside Colab):

    !pip -q install -U google-genai
  2. Make a first call. genai.Client() automatically reads GEMINI_API_KEY from the environment:

    import os
    from google import genai
    client = genai.Client() # reads GEMINI_API_KEY from the environment
    resp = client.models.generate_content(
    model="gemini-2.5-flash-lite",
    contents="Say hello in one short sentence."
    )
    print(resp.text)

    If you see a greeting, the whole setup works.

MessageLikely causeFix
Key loaded: FalseSecret name doesn’t matchName it exactly GEMINI_API_KEY (case-sensitive)
SecretNotFoundErrorNotebook access offToggle Notebook access on for the secret
PermissionDenied / InvalidApiKeyKey invalid or expiredGenerate a new key in AI Studio
ResourceExhausted / quota exceededFree-tier rate limit hitWait 1-2 minutes, then retry
ModelNotFoundWrong model nameUse e.g. gemini-2.5-flash-lite
ModuleNotFoundError: google.genaiSDK not installedRe-run !pip -q install -U google-genai
Must-knowOne-line recall
EnvironmentLabs run in Google Colab (cloud notebooks) and call the Gemini API
API key = passwordIdentifies my account; whoever holds it can spend my quota
Safe storagePut the key in Colab Secrets as GEMINI_API_KEY; never hard-code it
Load the keyuserdata.get("GEMINI_API_KEY") → env var; genai.Client() reads it automatically
Install & call!pip install -U google-genai; client.models.generate_content(model=..., contents=...)
If a key leaksDelete it in AI Studio and make a new one - treat it as compromised