How to use the Google Gemini API (Python and Node)

Google Gemini 2.5 Pro and Flash offer long context windows, multimodal inputs, and competitive pricing. This guide covers getting started with the Gemini API, function calling, and using the 1M token context window effectively.

Get your API key

Get a free Gemini API key at ai.google.dev. The free tier is generous — Gemini 2.0 Flash is completely free up to rate limits. For production, add billing at console.cloud.google.com and use Gemini 2.5 Pro for complex tasks or Flash for speed and cost efficiency.

Gemini 2.5 Flash is typically the best choice for most applications: it processes 1M tokens of context at a fraction of GPTGPTGenerative Pre-trained Transformer — the model architecture and family name behind OpenAI's most famous models, from GPT-2 to GPT-5.Learn more →-4o's price, supports multimodalMultimodalCapable of processing and generating multiple types of data — such as text, images, audio, and video — within a single model.Learn more → inputs, and achieves strong benchmarkBenchmarkA standardized test or set of tasks used to evaluate and compare the capabilities of different AI models on a common scale.Learn more → scores. Reserve 2.5 Pro for tasks that need deeper reasoning.

Install and make your first call

Python: `pip install google-genai`. Then: `from google import genai; client = genai.Client(api_key='YOUR_API_KEY'); response = client.models.generate_content(model='gemini-2.5-flash', contents='What is the capital of France?'); print(response.text)`.

Node.js: `npm install @google/genai`. Then: `const { GoogleGenerativeAI } = require('@google/genai'); const genai = new GoogleGenerativeAI(process.env.GEMINI_API_KEY); const model = genai.getGenerativeModel({model: 'gemini-2.5-flash'}); const result = await model.generateContent('Hello'); console.log(result.response.text())`.

Use the 1M token context window

Gemini 2.5 Pro and Flash both support up to 1 million tokens of context — enough for an entire codebase, a book, or hours of meeting transcripts. Pass large documents directly in the promptPromptThe input text sent to a language model — the question, instruction, or context that triggers a response.Learn more →: no chunkingChunkingThe process of splitting documents into smaller passages for embedding and retrieval in RAG systems, where chunk size and overlap affect retrieval quality.Learn more → or RAGRAGRetrieval-Augmented Generation — a technique that enhances LLM responses by first retrieving relevant documents from an external knowledge base and including them in the prompt.Learn more → required for most documents.

For very large inputs, use the Files API to upload once and reference repeatedly: `file = client.files.upload(path='large_document.pdf'); response = client.models.generate_content(model='gemini-2.5-flash', contents=[file, 'Summarise the key arguments'])`. Uploaded files are stored for 48 hours.

Function calling with Gemini

Define tools as Python functions with type annotations and docstrings. Gemini infers the JSON Schema from the type hints: `def get_weather(city: str) -> dict: '''Get current weather for a city. Args: city: The city name.''' ...`. Pass the function to `tools=[get_weather]` in the generate call.

Gemini supports parallel function callingFunction CallingA capability that allows language models to return structured calls to developer-defined functions or tools, enabling AI systems to interact with external APIs, databases, and services.Learn more → — it can request multiple tool calls in a single turn. Handle the response by checking `response.candidates[0].content.parts` for `function_call` parts, executing each, and returning results as `function_response` parts.

Streaming and chat sessions

Stream responses: `for chunk in client.models.generate_content_stream(model='gemini-2.5-flash', contents='Write a poem'): print(chunk.text, end='', flush=True)`.

For multi-turn conversations, use a chat session which manages history automatically: `chat = client.chats.create(model='gemini-2.5-flash'); response = chat.send_message('Hello'); response2 = chat.send_message('What did I just say?')`. The session remembers all previous turns.