Gemini API quickstart

This quickstart shows you how to install our libraries and make your first Gemini API request.

Before you begin

You need a Gemini API key. If you don't already have one, you can get it for free in Google AI Studio.

Install the Google GenAI SDK

Python

Using Python 3.9+, install the google-genai package using the following pip command:

pipinstall-q-Ugoogle-genai

JavaScript

Using Node.js v18+, install the Google Gen AI SDK for TypeScript and JavaScript using the following npm command:

npminstall@google/genai

Go

Install google.golang.org/genai in your module directory using the go get command:

gogetgoogle.golang.org/genai

Java

If you're using Maven, you can install google-genai by adding the following to your dependencies:

<dependencies>
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

C#

Install googleapis/go-genai in your module directory using the dotnet add command

dotnetaddpackageGoogle.GenAI

Apps Script

  1. To create a new Apps Script project, go to script.new.
  2. Click Untitled project.
  3. Rename the Apps Script project AI Studio and click Rename.
  4. Set your API key
    1. At the left, click Project Settings The icon for project settings.
    2. Under Script Properties click Add script property.
    3. For Property, enter the key name: GEMINI_API_KEY.
    4. For Value, enter the value for the API key.
    5. Click Save script properties.
  5. Replace the Code.gs file contents with the following code:

Make your first request

Here is an example that uses the generateContent method to send a request to the Gemini API using the Gemini 2.5 Flash model.

If you set your API key as the environment variable GEMINI_API_KEY, it will be picked up automatically by the client when using the Gemini API libraries. Otherwise you will need to pass your API key as an argument when initializing the client.

Note that all code samples in the Gemini API docs assume that you have set the environment variable GEMINI_API_KEY.

Python

fromgoogleimport genai
# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()
response = client.models.generate_content(
 model="gemini-2.5-flash", contents="Explain how AI works in a few words"
)
print(response.text)

JavaScript

import{GoogleGenAI}from"@google/genai";
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
constai=newGoogleGenAI({});
asyncfunctionmain(){
constresponse=awaitai.models.generateContent({
model:"gemini-2.5-flash",
contents:"Explain how AI works in a few words",
});
console.log(response.text);
}
main();

Go

packagemain
import(
"context"
"fmt"
"log"
"google.golang.org/genai"
)
funcmain(){
ctx:=context.Background()
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
client,err:=genai.NewClient(ctx,nil)
iferr!=nil{
log.Fatal(err)
}
result,err:=client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
genai.Text("Explain how AI works in a few words"),
nil,
)
iferr!=nil{
log.Fatal(err)
}
fmt.Println(result.Text())
}

Java

packagecom.example;
importcom.google.genai.Client;
importcom.google.genai.types.GenerateContentResponse;
publicclass GenerateTextFromTextInput{
publicstaticvoidmain(String[]args){
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
Clientclient=newClient();
GenerateContentResponseresponse=
client.models.generateContent(
"gemini-2.5-flash",
"Explain how AI works in a few words",
null);
System.out.println(response.text());
}
}

C#

usingSystem.Threading.Tasks;
usingGoogle.GenAI;
usingGoogle.GenAI.Types;
publicclassGenerateContentSimpleText{
publicstaticasyncTaskmain(){
// The client gets the API key from the environment variable `GEMINI_API_KEY`.
varclient=newClient();
varresponse=awaitclient.Models.GenerateContentAsync(
model:"gemini-2.5-flash",contents:"Explain how AI works in a few words"
);
Console.WriteLine(response.Candidates[0].Content.Parts[0].Text);
}
}

Apps Script

// See https://developers.google.com/apps-script/guides/properties
// for instructions on how to set the API key.
constapiKey=PropertiesService.getScriptProperties().getProperty('GEMINI_API_KEY');
functionmain(){
constpayload={
contents:[
{
parts:[
{text:'Explain how AI works in a few words'},
],
},
],
};
consturl='https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';
constoptions={
method:'POST',
contentType:'application/json',
headers:{
'x-goog-api-key':apiKey,
},
payload:JSON.stringify(payload)
};
constresponse=UrlFetchApp.fetch(url,options);
constdata=JSON.parse(response);
constcontent=data['candidates'][0]['content']['parts'][0]['text'];
console.log(content);
}

REST

curl"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent"\
-H"x-goog-api-key: $GEMINI_API_KEY"\
-H'Content-Type: application/json'\
-XPOST\
-d'{
 "contents": [
 {
 "parts": [
 {
 "text": "Explain how AI works in a few words"
 }
 ]
 }
 ]
 }'

What's next

Now that you made your first API request, you might want to explore the following guides that show Gemini in action:

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年12月18日 UTC.