LevelUpWith

Explore / Building Apps with the Gemini API / Lesson 1

Lesson 1: Google AI Studio and Your First API Key

Learn what Google AI Studio and the Gemini developer API are, why this course is independent of Google, and how to create an API key and keep it out of your code.

Free to read, no account needed. Part of Building Apps with the Gemini API.

By the end of this level you'll be able to

  • Create a Gemini API key in Google AI Studio and store it in an environment variable instead of in source code
  • Try a prompt in the AI Studio playground and use its 'Get code' view to see how the same request looks as an API call
  • Explain why this course checks Google's current documentation for every model name, limit and price instead of relying on memory
  • Run a short script that reads the API key from the environment variable, sends one prompt to Gemini, and confirm it worked by checking that response text comes back without an authentication error.

Why start here

Every Gemini app, from a weekend script to a production service, starts the same way: you get a credential, you call a model, and you get text back. This level covers that path, and one habit that will save you a lot of debugging later: keep your key out of your code.

A quick note before we start. This course is independent of Google. Google doesn't write, review or endorse it, and it doesn't copy Google's documentation. When you need the official word on something, go to Google's docs at ai.google.dev.

The two pieces: AI Studio and the developer API

Think of it like a kitchen. Google AI Studio (aistudio.google.com) is the tasting counter. It runs in your browser, you type a prompt, pick a model, tweak settings and see the answer at once. No code needed. The Gemini developer API is the back door your own programs come through to order the same food. Your Python or JavaScript sends an HTTP request, normally through Google's Gen AI SDKs (google-genai for Python, @google/genai for JavaScript).

The two are connected. AI Studio is also where you create the API key your code uses, and it can show you any playground prompt as code you can copy.

Why we check the docs instead of trusting memory

The Gemini API changes about once a month. Models get added, renamed, moved out of preview or shut down. Older models can become hard to get: Google's model list currently says access to the 2.5 models is limited to people who have used them before. Even the recommended way to call the API has changed. Since June 2026 Google's docs recommend the newer Interactions API (client.interactions.create) for new projects. The older generateContent call is now called legacy, though it is still fully supported.

So every model name, limit and price in this course is checked against Google's current docs, and you should do the same. A blog post from six months ago can give you a model ID that no longer exists. When you see a name with -preview in it, expect it to change or go away.

Step 1: Create your API key

  1. Sign in at aistudio.google.com with a Google account.
  2. Open the API keys page (aistudio.google.com/apikey). New users often find a default key already made for them. If you already use Google Cloud, you may need to import an existing project first.
  3. Create or copy a key.

Every key belongs to a Google Cloud project, and that project is what controls billing and permissions. Since 28 May 2026, new keys made in AI Studio are auth keys: they're tied to a service account and only work with the Gemini API by default. Older "standard" keys are being phased out. If you have an old key from a past project, make a new one instead of reusing it.

Gotcha: A key works like a password that's tied to your billing. If one leaks, make a new key, update your app to use it, then turn off the old one.

Step 2: Put the key in an environment variable

Never paste the key into a source file. One git push and it's public. Store it in an environment variable instead. The SDKs look for GEMINI_API_KEY or GOOGLE_API_KEY on their own. If both are set, GOOGLE_API_KEY wins, which is a common reason for "wrong key" confusion.

# macOS / Linux (current terminal only)
export GEMINI_API_KEY="paste-your-key-here"
# Windows PowerShell (current session only)
$env:GEMINI_API_KEY = "paste-your-key-here"
# To keep it for future sessions (open a new terminal afterwards):
setx GEMINI_API_KEY "paste-your-key-here"

If you'd rather use a .env file, add .env to .gitignore before your first commit.

Step 3: Try a prompt in the playground, then click "Get code"

In AI Studio, open a new prompt, pick a Flash model and type something like:

In two sentences, explain what an API key is to a new developer.

Run it. Then click Get code. AI Studio turns the exact request you just made (model, prompt, settings) into a snippet in Python, JavaScript, REST and other languages. This is the quickest way to see that the playground and the API are the same thing. Notice that the snippet never contains your key. It expects the key to come from the environment, just as we set it up.

Step 4: Run your first script

Install the SDK (pip install -U google-genai or npm install @google/genai), then run one of these.

# hello_gemini.py
import os
import sys
from google import genai

# Fail fast with a clear message instead of a confusing auth error
if not (os.environ.get("GEMINI_API_KEY") or os.environ.get("GOOGLE_API_KEY")):
    sys.exit("No API key found: set GEMINI_API_KEY first.")

client = genai.Client()  # reads the key from the environment, never from code

interaction = client.interactions.create(
    model="gemini-3.8-flash",  # current stable Flash model at the time of writing; check the models page
    input="Reply with one short sentence confirming you can hear me.",
)
print(interaction.output_text)
// hello_gemini.mjs  (run with: node hello_gemini.mjs)
import { GoogleGenAI } from "@google/genai";

if (!process.env.GEMINI_API_KEY && !process.env.GOOGLE_API_KEY) {
  throw new Error("No API key found: set GEMINI_API_KEY first.");
}

const ai = new GoogleGenAI({}); // empty config: the SDK finds the key itself

const interaction = await ai.interactions.create({
  model: "gemini-3.8-flash",
  input: "Reply with one short sentence confirming you can hear me.",
});
console.log(interaction.output_text);

Step 5: Check that it worked

It worked if you see a line of model text, for example:

Yes, I can hear you loud and clear!

The wording will be different each time, and that's fine. What matters is that text came back and there was no authentication or permission error. If something fails:

  • "No API key found": the variable isn't set in this terminal. setx only takes effect in new windows.
  • Auth or permission error: check for a stray GOOGLE_API_KEY that's taking priority, a copy-paste mistake, or an old standard key.
  • Model not found: the model ID has changed. Look it up on the current models page.

Key takeaways

  • AI Studio is the browser playground and the place where you make keys. The developer API is how your code reaches the same models.
  • This course is independent of Google. Always check model names, limits and prices against Google's current docs.
  • New AI Studio keys are auth keys tied to a Google Cloud project. Treat them like passwords.
  • Store the key in GEMINI_API_KEY. The SDK reads it automatically, and GOOGLE_API_KEY takes priority if both are set.
  • "Get code" turns any playground prompt into a ready-to-run API call.
  • You're done when your script prints model text with no authentication error.

A 6-question knowledge check follows this lesson in the app, free and without an account.

More like this

  • Google Cloud Professional Cloud Developer Exam Prep

    Prepare for Google Cloud's Professional Cloud Developer exam section by section, with short code and gcloud examples and original scenario questions. You'll le…

    Exam prep · Google · Cloud & Compute · Expert · 50 levels · 4 free · Professionally curated by levelupwith.com

  • Google Cloud Professional Cloud Architect Exam Prep

    Practise making architecture decisions the way the Professional Cloud Architect exam expects. For each one you work through the requirements, options and trade…

    Exam prep · Google · Cloud & Compute · Expert · 62 levels · 4 free · Professionally curated by levelupwith.com

  • Support Pro: Helping Users Move to Google Workspace

    Get first and second line support ready to carry users through a Microsoft 365 to Google Workspace cutover. You will prepare before cutover, map each Microsoft…

    Course · Google · Suite Support Pro: Configure and Fix · Expert · 40 levels · 4 free · Professionally curated by levelupwith.com

  • Google Workspace Support Pro: Gemini for End Users

    Diagnose and fix real Gemini tickets in Google Workspace. You'll work out which Gemini a user has from their edition and add-ons, read the Gemini app and servi…

    Course · Google · Suite Support Pro: Configure and Fix · Expert · 40 levels · 4 free · Professionally curated by levelupwith.com

  • Google Workspace Support Pro: Drive and Shared Drives

    Diagnose and fix Google Drive problems the way experienced support engineers do: read Admin console Drive policy and explain what it does, find out who can rea…

    Course · Google · Suite Support Pro: Configure and Fix · Expert · 34 levels · 4 free · Professionally curated by levelupwith.com

  • Google Workspace Support Pro: Meet and Chat

    Diagnose and fix the Meet and Chat faults that reach first and second line support: audio, video, screen sharing, spaces, external guests, recordings and room…

    Course · Google · Suite Support Pro: Configure and Fix · Expert · 40 levels · 4 free · Professionally curated by levelupwith.com