Modified

Gemini API and Google AI Studio

The Gemini API lets your own programs use Google's Gemini models. In Google AI Studio at aistudio.google.com you try the models in the browser, get an API key and get ready-made code. The Gemini API has a free tier for several models, among them the Flash models. If you need more, you pay per token, roughly a piece of a word.

  1. Sign in to Google AI Studioaistudio.google.com with your Google account
  2. Copy your API keycreated automatically for new users
  3. Put the key in an environment variableGEMINI_API_KEY, never in the code
  4. Install the SDKgoogle-genai for Python, @google/genai for JavaScript
  5. Send your first requestone question in, one answer out
Five steps from a Google account to the first answer from the Gemini API.

The basics

What is an API and an API key?

An API is a door into a program for other programs. Instead of you typing in the Gemini app, your program sends a question to Google's server. The answer comes back as plain text or as JSON, a text format that programs can read. Chatbots, translation services and apps that summarise documents work this way.

An API key is a long string of characters that tells Google who is sending the request. The key ties every call to your project, your limits and your bill. Treat the key like a password. Whoever has the key can use your account.

How an API request travels

Your programan app, a script, a web page$GEMINI_API_KEY
The question and the keythe key shows who pays
The answer as dataand how many tokens it used
The company's servergenerativelanguage.googleapis.comGemini
Tokens inwhat you send
Tokens outwhat the model writes, costs more
You pay for tokens in both directions. The key stays in the environment variable, never in the code.

What is Google AI Studio?

Google AI Studio is Google's web tool for trying the Gemini models. You test models and prompts, the instructions you write to the model. Then you press "Get code" and get the same request as code. In AI Studio you also create API keys and see your usage and limits.

Google AI Studio also has a build mode called Build. At aistudio.google.com/apps you build web apps and Android apps by describing them in plain language.

The Gemini API documentation has moved. At the time of writing, old links to ai.google.dev forward to aistudio.google.com/docs.

Who is the Gemini API for?

The Gemini API is for you if you want to build something of your own, for example an app, a web service, a script or an automation. You need to know a little programming, or let a coding agent such as Antigravity CLI write the code. If you just want to chat, the Gemini app is enough.

The Gemini API is aimed, according to its terms, at developers building for professional or business purposes. The same terms require you to be at least 18 and apply from 23 March 2026.

Get started

How do you get an API key for Gemini?

You create the API key in Google AI Studio. New users automatically get a project and an API key. You copy the key on the API keys page, aistudio.google.com/api-keys. You create more keys with the Create API key button.

You then put the key in an environment variable in the terminal. Both words are explained in the word list in part 2. Google's SDKs, the ready-made code packages for Gemini, read the variable GEMINI_API_KEY or GOOGLE_API_KEY. If both are set, GOOGLE_API_KEY wins.

On Mac and Linux you set the variable with the command below. On Windows you add the variable under Environment Variables in System Properties and then open a new terminal window.

Set the API key on Mac and Linux
Terminal
export GEMINI_API_KEY="YOUR_API_KEY"

Your first call to the Gemini API

Your first call sends a short question and prints the answer. The code below is Google's own quickstart. The code uses a Flash model and Google's Gen AI SDK, which exists for Python and JavaScript. The model name in the code applies at the time of writing, and current names are on Google's models page. The SDK finds the key in GEMINI_API_KEY on its own.

The command curl sends a web request straight from the terminal, without installing anything. With curl you see fastest that the key works.

You install the Python SDK with pip, and the JavaScript SDK with npm.

The Interactions API is Google's new standard way to call Gemini. The older generateContent still works but counts as legacy, according to Google's documentation. The old google-generativeai libraries have been deprecated since 30 November 2025, according to the same documentation. So do not follow older guides that use them.

First call with curl
Terminal
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -H "Api-Revision: 2026-05-20" \
  -d '{
    "model": "gemini-3.8-flash",
    "input": "Explain how AI works in a few words"
  }'
Install the Python SDK
Terminal
pip install -U google-genai
First call in Python
Terminal
from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3.8-flash",
    input="Explain how AI works in a few words"
)
print(interaction.output_text)
Install the JavaScript SDK
Terminal
npm install @google/genai
First call in JavaScript
Terminal
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

const interaction = await ai.interactions.create({
  model: "gemini-3.8-flash",
  input: "Explain how AI works in a few words",
});
console.log(interaction.output_text);

Which models does the Gemini API have?

The Gemini API has models in four families, according to Google's models page. Google releases new versions within each family at regular intervals. The model code, the name you write in the request, is listed for each version on Google's models page.

  • Argon is Google's largest model and is for hard tasks and long reasoning. Argon has a large context window, which is how much text the model reads at once.
  • Pro is for hard questions and code.
  • Flash is fast and cheap. Use Flash for code, agents and most apps.
  • Flash-Lite is the fastest and cheapest family. Flash-Lite suits large volumes of simple tasks.

Build further

How do you get JSON from the Gemini API?

Structured output means Gemini answers in a fixed format your program can read, instead of free text. You set response_format with mime_type application/json and a schema. The schema describes which fields the answer should have.

The SDKs accept schemas from Pydantic in Python and Zod in JavaScript. Pydantic and Zod are code libraries for describing data.

Structured output suits you when you save the answer in a database, fill in a form or pass the answer on to other code. Still check the answer in the code before you use it.

What is function calling in the Gemini API?

Function calling lets Gemini ask your program to run a function, for example to book a meeting or fetch the weather. A function is a piece of code that does one set thing. You describe the functions in the request. Gemini answers with which function to run and with which values.

Gemini never runs the function itself. Your code runs the function and sends the result back, and then Gemini writes an answer to the user. So always check the values before your code does something that cannot be undone.

How a tool call travels

  1. Sends the question and the available tools
  2. Replies: run the tool, with these values
  3. Runs the code itself, for example looks up an order
  4. Sends the result back
  5. Writes the answer with the result
The model never runs the code itself. Check the values before your program does something that cannot be undone.

Can the Gemini API read images, PDFs, audio and video?

The Gemini API reads images, PDF files, audio and video in the same request as the text. Smaller files can go directly in the request, according to Google's documentation. How large the files may be and how many pages a PDF may have is in the same documentation.

You upload larger files with the Files API, Google's service for files. The Files API has limits per file and per project, according to the documentation. Google deletes the files automatically after 48 hours, the same source says.

Price and choice

How much does the Gemini API cost?

The Gemini API is priced per million tokens, split into tokens in and tokens out. Tokens in are your question, and tokens out are the answer. An answer costs more per token than a question.

The price depends on the model. Argon and Pro cost more per token than Flash and Flash-Lite. For some models, long prompts cost more per token than short ones, according to Google's pricing page.

Flash-Lite, for example, costs 0.30 dollars per million tokens in and 2.50 dollars per million tokens out on the paid tier at the time of writing, according to Google's pricing page. Prices are given in USD and change. Google changes prices with 30 days' notice, according to the terms.

Is the Gemini API free, and what happens to your data?

The Gemini API has a free tier. At the time of writing, Flash and Flash-Lite are free within the limits, while Argon and Pro are only on the paid tier, according to Google's pricing page.

The free tier and the paid tier treat your data differently. According to the Gemini API terms, Google uses content in unpaid services to improve its products, and reviewers may read it. Google writes that you should not send sensitive, confidential or personal information there. On the paid tier, Google does not use your prompts and responses to improve its products.

Users in Sweden have a special rule. Sweden is in the EEA, which is the EU plus Norway, Iceland and Liechtenstein. If you are in the EEA, Switzerland or the UK, the paid-tier rules on data apply to all services, according to the terms. The rule also covers AI Studio and the free quota. An app you make available to users in the EEA may only use paid services, according to the same terms.

What limits does the Gemini API have?

The Gemini API limits how much you may send, with so-called rate limits. Google's page on limits measures them in requests per minute, tokens per minute and requests per day. The abbreviations are RPM, TPM and RPD. Limits apply per project, not per key, and the daily limit resets at midnight Pacific time.

Google shows the exact limits per model only in AI Studio, under Rate limit.

Limits rise in several tiers, according to Google's page on limits:

  • Free is the tier you start on.
  • Tier 1 is yours when you link a billing account.
  • Higher tiers are yours when you have paid more and the account has existed for a while. The amounts and times are on the same page.

Google AI Studio or Vertex AI?

Google has two routes to Gemini for developers. One is the Gemini Developer API, which you reach through AI Studio. The other is Google's cloud platform for companies, Gemini Enterprise Agent Platform. Google Cloud describes the platform as an evolution of Vertex AI, and Google's comparison page calls it Gemini platform for short.

Google's comparison recommends the Gemini Developer API for most developers, unless they need specific enterprise controls. You reach both routes with the same SDK. The Google Cloud platform signs in with service accounts, which are accounts for programs instead of people. The platform may also have other regions, which are other places where the servers stand.

Gemini API, OpenAI API or Claude API?

The Gemini API, the OpenAI API and the Claude API work the same way. You have a key, send text in a request, get an answer and pay per token. All three have SDKs for Python and JavaScript, structured output and function calling.

The Gemini API stands apart through its free tier and its link to Google, for example Google Search as a source and AI Studio as a place to try things. Pick the API that suits your project. Once you understand one of them, the code is easy to switch. Advice on writing good instructions for the models is in the prompt guide.

Common questions about the Gemini API and AI Studio

Is Google AI Studio free?

Google AI Studio is free to use in the browser. The Gemini API terms count direct use of AI Studio as an unpaid service, except when the account has an active Google Cloud billing account.

Is the Gemini API available in Sweden?

The Gemini API is available in Sweden. Sweden is on the list of countries where the service is available, in Google's documentation at the time of writing.

Where do you find your API key?

The API key is on the API keys page in Google AI Studio, aistudio.google.com/api-keys. New users get a key automatically.

Can you use the Gemini API without programming?

The Gemini API needs code. The build mode in AI Studio, however, creates apps from a description in plain language. A coding agent can also write the code for you.

What does error 429 mean?

Error 429 RESOURCE_EXHAUSTED means you have hit a rate limit, according to Google's troubleshooting page. Wait and retry with longer and longer pauses. Google's SDKs do this automatically. You get higher limits if you link a billing account.

Which language should the prompt be in?

Gemini understands Swedish, so you can write prompts and get answers in Swedish. If the language matters, state it in the system instruction, the fixed instructions that apply to the whole conversation.

Sources: Google, quickstart · Google, API keys · Google, models · Google, pricing · Google, limits · Google, terms · Google, Vertex comparison