Send your first Gemini API request with a maintainable setup.
This Gemini API tutorial takes a Python project from API access to its first text response using the official Google Gen AI SDK. It keeps credentials and model selection in environment variables so the same example can move from local testing to a server without publishing secrets or hardcoding a temporary model name.
Gemini API setup checklist
Project and key
Create or import a Google Cloud project in Google AI Studio, then generate an appropriate Gemini API key.
Official SDK
Install the maintained google-genai package instead of copying examples from deprecated client libraries.
Environment
Store GEMINI_API_KEY and GEMINI_MODEL outside source control and read them only on the server.
First request
Send a small generate-content request, inspect the response, and handle missing, blocked, or failed output.
Prepare a project and API access
Use Google AI Studio to create or import the Google Cloud project that owns billing, quota, collaborators, and keys. Confirm that your account has permission to create credentials before changing application code.
- Keep development and production resources in separate projects when their access or billing boundaries differ.
- Review the current key type and restrictions shown by Google AI Studio.
- Never paste a real key into documentation, screenshots, issue trackers, or chat messages.
Install the Google Gen AI SDK
Create an isolated Python environment and install the official google-genai package. Pin or record tested dependency versions for reproducible deployments, then review release notes before upgrades.
- Use pip install -U google-genai for an initial local setup.
- Do not mix current examples with older package names or incompatible SDK methods.
- Run a minimal import check before debugging credentials or network behavior.
Configure key and model safely
Read GEMINI_API_KEY and GEMINI_MODEL from the runtime environment. Keeping the model in configuration makes version changes deliberate and prevents an old tutorial identifier from becoming an accidental production dependency.
- Add local secret files to ignore rules and never commit their contents.
- Use the deployment platform's secret store for production credentials.
- Choose a currently supported model that matches the required input, latency, quality, and cost.
Send and inspect the first request
Create a client, call generate_content with the configured model and a small prompt, then inspect the returned text. A successful transport does not prove the answer is accurate or suitable for the application.
- Handle missing text, safety blocks, invalid requests, authentication failures, quota errors, and timeouts.
- Log status, latency, model, and request identifiers without logging keys or sensitive prompts.
- Verify the result against a simple expected behavior before adding chat history, files, tools, or streaming.
Prepare the prototype for production
Put the API call behind your own server boundary. Add input validation, authentication, rate limits, timeout and retry rules, cost controls, privacy review, and task-specific evaluation before exposing it to users.
- Treat model output as untrusted data and validate it before rendering or using tools.
- Use bounded exponential backoff only for retryable failures and avoid duplicate side effects.
- Monitor token use, latency, error categories, safety outcomes, and quality regressions.
Python
Install google-genai, set GEMINI_API_KEY and GEMINI_MODEL in the server environment, then run this minimal generate-content request.
import os
from google import genai
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
model = os.environ["GEMINI_MODEL"]
response = client.models.generate_content(
model=model,
contents="Explain context windows in three concise points.",
)
print(response.text)