Gemini API: Key, SDK and the Interactions API
Short answer. To make your first Gemini API call you need three things: a key, which you put in the GEMINI_API_KEY environment variable; the SDK (pip install -U google-genai for Python, npm install @google/genai for JavaScript); and a call to client.interactions.create with a model and some text. As of 30 October 2026 the official quickstart uses the Interactions API, which the documentation names as the recommended interface for new projects; the older generateContent "remains fully supported". The difference that matters to you is not syntax: Interactions store the conversation on Google's servers, for one day on the free tier and 55 days in paid projects, unless you switch it off. This article shows the call and then the one choice you should not make by inertia.
Series "Google Gemini for Developers", article 2 of 6. I am not affiliated with Google. Sources consulted on 30 October 2026: quickstart, Interactions API, changelog. The code below follows the documentation examples; where I could not compare it line by line I say so.
Step 1: the key, and where NOT to put it
The key is created from Google AI Studio, the same interface where, as we will see in article 5, you read your project's quotas. The documentation sets it up like this:
export GEMINI_API_KEY="YOUR_KEY"
Two non-negotiable rules. The key never goes into code that runs in the browser of your site's visitors: anyone who opens the developer tools copies it, and from then on they spend on your project. The call must be made from your server (a backend, a serverless function), which in turn authenticates the user and limits requests. Second rule: the key does not go into a repository, not even a private one, and not into a sample file. An environment variable, a secret manager, and a .env file excluded from version control are enough to start.
One detail that changes how you organise keys: the limits page says limits apply per project, not per key. Three keys in the same project share the same quota. If you want to isolate two applications, you need one project for each, not one key for each.
Step 2: the first call
In Python, following the quickstart:
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.8-flash",
input="Explain how a B-tree index works in three sentences",
)
print(interaction.output_text)
And in JavaScript:
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const interaction = await ai.interactions.create({
model: "gemini-3.8-flash",
input: "Explain how a B-tree index works in three sentences",
});
console.log(interaction.output_text);
The client reads the key from GEMINI_API_KEY, so it never appears in the code. The returned object, an Interaction, represents one complete turn: it holds a sequence of "steps" (steps) with the model's thoughts, tool calls, results and final output, and it has an id. output_text is the shortcut for the final text. The model is the one from article 1: put it in a constant, not in ten places in the code.
Step 3: remembering the conversation without resending it all
Here is what is new compared with generateContent. To continue a dialogue you pass the identifier of the previous turn:
turn2 = client.interactions.create(
model="gemini-3.8-flash",
input="When is a B-tree index worse than a full scan?",
previous_interaction_id=turn1.id,
)
The server retrieves the history and you do not resend it: less code, fewer tokens in the request, and the model sees the same context it already processed. But there are two traps the documentation declares and that people copying the example skip. First: tools, system instructions and generation configuration apply only to the interaction where you set them: they must be redeclared on every turn. If you forget to put back the function the model must be able to call, on the next turn it does not see it. Second: server-side storage is the condition that makes all this work, and that is where the choice I discuss now arises.
The choice: server-side state, or not
Interactions are stored by default (store=true). The periods given in the documentation are 55 days for paid projects and 1 day for free ones; paid projects can reduce them to 7, 14, 28 or 55 days from the AI Studio settings. If you set store=false you work "statelessly": you send the full history with every request, and you lose two things, previous_interaction_id for later turns and background execution (background=true, meant for long tasks such as deep-research agents).
| With state (store=true) | Stateless (store=false) | |
|---|---|---|
| Code | Shorter: you pass an id | Longer: you keep and resend the history |
| Data at Google | Stored 1 or 55 days | Not stored as an interaction |
| Background tasks | Yes | No |
| Step observability | Yes, also afterwards | Only in the response |
| Explicit cache | Not supported in Interactions | Same (you switch to generateContent) |
There is also a recent change to know about. The changelog of 26 October 2026 carries "Generate Content API storage on by default": new paid projects using generateContent and its variants now store requests by default (store=true), like Interactions; existing projects keep their current setting. You can decline per request with store=false or by turning off logging in the "Logs and Datasets" panel. The entry does not say what is stored or for how long: I do not know and I do not write it. If your project is new, read the original changelog entry before sending customer data.
Three use cases, from smallest to largest
Everyday life: Paolo and the rental contract
Who and what they did by hand. Paolo, a tenant, needs to understand how much notice he must give to terminate. The contract is a twenty-page PDF he has never read in full. Hypothesis: an hour of reading, or a paid consultation for a single question. What changes. The model page says gemini-3.8-flash accepts text, images, video, audio and PDF as input. With twenty pages, assuming about 15,000 tokens, reading costs 15,000 × 0.75 / 1,000,000 ≈ one cent. How to pass the PDF to the API I did not verify in this article: the right path is the documentation's file guide. What goes wrong. Three things. The contract contains a name, an address and amounts: on the free tier the terms allow Google to use the content and human reviewers to read it. Then, a model answering "notice is six months" without citing the clause is useless and dangerous: always ask for the literal text of the clause and the page number, and check it yourself. Finally, a wrong notice period is real economic damage: the answer is a pointer to where to look, not legal advice.
Small business: the professional firm and incoming mail
Who and what they did by hand. An accounting firm of three people receives about 60 client messages a day: missing documents, deadlines, reminders. Hypothesis: twenty minutes a day sorting them by client and urgency. What changes. A small service on their own server reads the messages and labels them (urgent, documents, information) with gemini-3.1-flash-lite. That is 60 × 22 = 1,320 messages a month; at 800 input tokens and 100 output tokens each, that is 1.056 million in and 0.132 million out: 1.056 × 0.25 + 0.132 × 1.50 = about 0.46 dollars a month. What goes wrong. Here the cost is irrelevant and the constraint is another: professional secrecy and the processing of client data. The project must be paid (never the free tier); the automatic 55-day storage of interactions is an additional processing to declare in the privacy notice, or to switch off with store=false, and labelling needs no conversation memory: each message is an independent request. If the case does not need state, do not turn it on.
Large enterprise: a manufacturer with three plants
Who and what they did by hand. The quality department of a producer with three plants receives about 800 non-conformity reports a month and prepares a weekly summary for management by hand. Hypothesis: two person-days a month. What changes. A nightly process summarises the reports by line and by cause. With 2,000 input tokens and 300 output tokens each: 1.6 million in and 0.24 out, which with gemini-3.8-flash makes 1.6 × 0.75 + 0.24 × 3.75 = 2.10 dollars a month. What goes wrong. The cost is a rounding error; the problems are about governance. Limits are per project, so one project per plant isolates quota and spend. Interactions stored for 55 days must go into the data retention policy and the record of processing; the reports contain names of operators and suppliers. And above all someone must own the model choice: in article 1 we saw that a deprecated identifier is routed to another model with no notice in the code, and a summary reaching management must be reproducible.
What to do now
- Create the key, put it in
GEMINI_API_KEY, run the ten lines of Python or JavaScript above. If it prints an answer, the technical step is done. - Decide explicitly whether you need state. If each request is independent (classification, extraction, summary), use
store=falseand spare yourself the problem. - Write in the README the date you consulted the documentation and the model used.
- Before passing third-party data, read article 5 on tiers, limits and privacy.
What I did not verify
- Passing files and images to the API (it exists, but I did not re-read the full example).
- Streaming and background execution: the Interactions page points to separate guides I did not read.
- What is stored, and for how long, with
generateContenton new paid projects.
The series
- Gemini for Developers: Models and Pricing
- This article
- Functions, structured JSON and Google Search
- One-million-token context, thinking and caching
- Tiers, limits and data privacy
- After Gemini CLI: Antigravity, gemini-api and the comparison
If you are interested in the protocol through which these models connect to external tools, this site has an introduction to the Model Context Protocol.
Sources consulted on 30 October 2026
- Quickstart (packages, client, call, environment variable).
- Interactions API (state, 1 and 55 day storage,
store,previous_interaction_id). - Changelog (26 October 2026 entry on
generateContentstorage). - Rate limits (limits per project, not per key).
- Gemini 3.8 Flash model page (supported inputs).
Commenti
Caricamento commenti...
Sign in to comment