Gemini API quickstart

This quickstart shows you how to get started with the Gemini API using the SDK of your choice.


View on Google AI Run in Google Colab View notebook on GitHub

Prerequisites

To complete this quickstart locally, ensure that your development environment meets the following requirements:

  • Python 3.9+
  • An installation of jupyter to run the notebook.

Install the Gemini API SDK

The Python SDK for the Gemini API is contained in the google-generativeai package. Install the dependency using pip:

pip install -q -U google-generativeai

Authenticate

Set up your API key

To use the Gemini API, you'll need an API key. If you don't already have one, create a key in Google AI Studio.

Get an API key

Then, configure your key.

It is strongly recommended that you do not check an API key into your version control system but assign it as an environment variable instead:

export API_KEY=<YOUR_API_KEY>

Initialize the model

Before you can make any API calls, you need to import and initialize the model. Gemini 1.5 models are versatile and work with both text-only and multimodal prompts.

import google.generativeai as genai
import os

genai.configure(api_key=os.environ["API_KEY"])

model = genai.GenerativeModel('gemini-1.5-flash')

Make your first request

Generate text

response = model.generate_content("Write a story about an AI and magic")
print(response.text)

What's next

Now that you're set up to make requests to the Gemini API, you can use the full range of Gemini API capabilities to build your apps and workflows. To get started with Gemini API capabilities, see the following guides:

If you're new to working with generative AI or the Gemini API, check out the following guides, which will help you understand the Gemini API programming model:

For in-depth documentation of Gemini API methods and request parameters, see the guides in the API reference.