How to start with the Gemini API: get an API key and make your first call
Create a Gemini API key in Google AI Studio, keep it safe, install the official Python or JavaScript SDK and send your first request.
The Gemini API quickstart walks you through your first call with the Interactions API, which you can use from the Python and JavaScript SDKs or over REST [1]. This guide covers the first steps: get an API key, keep it safe, install the SDK and send one request [1].
Step 1: Get an API key
Like the official quickstart, this guide authenticates requests with an API key [1]. If you are a new user, Google AI Studio creates a default Google Cloud project and API key after you accept the Terms of Service [2]. If you already have a Google Cloud account, AI Studio does not create a default project, and you import your existing projects instead [2]. You can copy your key from the API keys page at https://aistudio.google.com/api-keys [1]. If you need another key, click Create API key in AI Studio and follow the dialog [1].
Every Gemini API key belongs to a Google Cloud project, which handles billing, collaborators and permissions [2]. Extra projects and projects imported from Google Cloud may need project verification before they can use the free tier [1].
New keys created in AI Studio are authorization keys, which are bound to a service account and restricted to the Gemini API by default [2]. The Gemini API rejects requests from unrestricted standard keys, so an older key may need restrictions before it works [2].
Step 2: Keep the key safe
Google recommends storing the key in the GEMINI_API_KEY or GOOGLE_API_KEY environment variable, which the client libraries pick up automatically [2]. If both are set, GOOGLE_API_KEY wins [2].
export GEMINI_API_KEY="YOUR_API_KEY"
Treat the key like a password, because anyone who has it can use your quota and run up charges [2]. The docs give these rules:
- Never commit API keys to Git or other source control [2].
- Do not hardcode keys in web or mobile apps; call the API from a backend proxy server instead [2].
- In production, keep keys in a secret store such as Google Cloud Secret Manager [2].
- If a key leaks, create a new key, deploy it, then disable the old key in the Cloud console [2].
Step 3: Install the SDK
For Python, install google-genai [1]:
pip install -U google-genai
For JavaScript or TypeScript, install @google/genai [1]:
npm install @google/genai
Step 4: Send your first request
In Python, create a client and call client.interactions.create [1]:
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="Explain how AI works in a few words"
)
print(interaction.output_text)
In JavaScript, the call looks like this [1]:
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const interaction = await ai.interactions.create({
model: "gemini-4-hydrogen",
input: "Explain how AI works in a few words",
});
console.log(interaction.output_text);
Neither example passes the key in code, because the client reads it from the environment variable [2]. The output_text property gives you the final text without digging through the full response [1].
Free tier and limits
The free tier gives limited access to certain models, with input and output tokens free of charge [3]. On the pricing page, gemini-4-hydrogen is free of charge for input and output on the free tier, while some models, such as Gemini 4 Carbon, are not available on the free tier [3].
Rate limits are counted per project, not per API key, and daily request quotas reset at midnight Pacific time [4]. Your current limits are shown in Google AI Studio and change with your usage tier [4]. To raise them, click Set up billing on the API keys page [1].
Common errors
429 RESOURCE_EXHAUSTEDmeans you hit a rate limit; wait and retry, ideally with exponential backoff [4][5].400and403point to problems like an invalid API key or bad syntax, so fix the request instead of retrying [5].- The official SDKs, such as the Python SDK, retry transient errors like
429and5xxautomatically with exponential backoff [5].
Sources
Written and checked by AI from the sources below
Fact-checked by gpt-6.1-sol on · Editorial policy