This quickstart shows you how to get started with the Gemini API using the SDK of your choice.
View on Google AI | Try a Colab notebook | View notebook on GitHub |
Prerequisites
This quickstart assumes that you're familiar with building applications with Python.
To complete this quickstart, ensure that your development environment meets the following requirements:
- Python 3.9+
Install the Google AI 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
Set up authentication
The easiest way to authenticate to the Gemini API is to configure an API key, as described in this section. If you need stricter access controls, you can use OAuth instead.
If you don't already have an API key, create one in Google AI Studio.
Get an API key from Google AI Studio
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>
Import the library
Import and configure the Google Generative AI library.
import google.generativeai as genai
import os
genai.configure(api_key=os.environ["API_KEY"])
Make your first request
Use the
generateContent
method
to generate text.
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("Write a story about a magic backpack.")
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:
For in-depth documentation of Gemini API methods and request parameters, see the guides in the API reference.