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 Node.js.

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

  • Node.js v18+
  • npm

Install the Gemini API SDK

To use the Gemini API in your own application, you need to install the GoogleGenerativeAI package for Node.js:

npm install @google/generative-ai

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.

const { GoogleGenerativeAI } = require("@google/generative-ai");

Make your first request

Use the generateContent method to generate text.

// Make sure to include these imports:
// import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });

const prompt = "Write a story about a magic backpack.";

const result = await model.generateContent(prompt);
console.log(result.response.text());

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.