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.
What I need
Section titled “What I need”Step 1 - Open Google Colab
Section titled “Step 1 - Open Google Colab”-
Go to colab.research.google.com and sign in with my Google account.
-
Click New Notebook.
-
In the first cell, type this and run it (Shift+Enter):
print("Colab is working!")If it prints the message, Colab is ready.
Step 2 - Create a Gemini API key
Section titled “Step 2 - Create a Gemini API key”-
Open Google AI Studio and sign in.
-
Find Get API key (or API keys) in the interface and create a new API key.
-
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:
-
In Colab, click the key icon (🔑 Secrets) in the left sidebar.
-
Add a new secret - Name:
GEMINI_API_KEY, Value: paste your key. -
Toggle Notebook access on for that secret.
-
In a code cell, load it into an environment variable:
from google.colab import userdataimport osos.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”-
Install the Gemini SDK (the
!runs a shell command inside Colab):!pip -q install -U google-genai -
Make a first call.
genai.Client()automatically readsGEMINI_API_KEYfrom the environment:import osfrom google import genaiclient = genai.Client() # reads GEMINI_API_KEY from the environmentresp = 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.
Troubleshooting
Section titled “Troubleshooting”| Message | Likely cause | Fix |
|---|---|---|
Key loaded: False | Secret name doesn’t match | Name it exactly GEMINI_API_KEY (case-sensitive) |
SecretNotFoundError | Notebook access off | Toggle Notebook access on for the secret |
PermissionDenied / InvalidApiKey | Key invalid or expired | Generate a new key in AI Studio |
ResourceExhausted / quota exceeded | Free-tier rate limit hit | Wait 1-2 minutes, then retry |
ModelNotFound | Wrong model name | Use e.g. gemini-2.5-flash-lite |
ModuleNotFoundError: google.genai | SDK not installed | Re-run !pip -q install -U google-genai |
Revision summary
Section titled “Revision summary”| Must-know | One-line recall |
|---|---|
| Environment | Labs run in Google Colab (cloud notebooks) and call the Gemini API |
| API key = password | Identifies my account; whoever holds it can spend my quota |
| Safe storage | Put the key in Colab Secrets as GEMINI_API_KEY; never hard-code it |
| Load the key | userdata.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 leaks | Delete it in AI Studio and make a new one - treat it as compromised |