Gemini API reference

The Gemini API lets you access the latest generative models from Google. This API reference provides detailed information for the classes and methods available in the Gemini API SDKs. Pick a language and follow the setup steps to get started with building generative applications on your platform of choice.

Prerequisites

This quickstart assumes that you're familiar with building applications with Go.

To complete this quickstart, make sure that your development environment meets the following requirements:

  • Go 1.20+

Install the Gemini API SDK

To use the Gemini API in your own application, you need to get the Go SDK package in your module directory:

go get github.com/google/generative-ai-go

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 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 the Google Generative AI library and create a client.

import "github.com/google/generative-ai-go/genai"
import "google.golang.org/api/option"

ctx := context.Background()
// Access your API key as an environment variable (see "Set up your API key" above)
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("API_KEY")))
if err != nil {
    log.Fatal(err)
}
defer client.Close()

Make your first request

Use the generateContent method to generate text.

model := client.GenerativeModel("gemini-1.5-flash")
resp, err := model.GenerateContent(ctx, genai.Text("Write a story about a magic backpack."))
if err != nil {
	log.Fatal(err)
}

printResponse(resp)

What's next

Now that you have a model client, you can start programming with the Gemini API. Read through the reference to learn how to use key API features:

If you're just getting started, check out the following guides, which will help you understand the Gemini API programming model:

You might also want to check out the capabilities guides, which introduce Gemini API features and provide examples of programming with the SDKs:

Then, for more in-depth documentation of Gemini API methods and request parameters, browse the guides in this reference.