Gemini Developer API

只需几分钟时间,即可获取 Gemini API 密钥并发出第一个 API 请求。

Python

from google import genai  client = genai.Client(api_key="YOUR_API_KEY")  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";  const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });  async function main() {   const response = await ai.models.generateContent({     model: "gemini-2.5-flash",     contents: "Explain how AI works in a few words",   });   console.log(response.text); }  await main(); 

Go

package main  import (     "context"     "fmt"     "log"      "google.golang.org/genai" )  func main() {     ctx := context.Background()     client, err := genai.NewClient(ctx, &genai.ClientConfig{         APIKey:  "YOUR_API_KEY",         Backend: genai.BackendGeminiAPI,     })     if err != 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,     )     if err != nil {         log.Fatal(err)     }     fmt.Println(result.Text()) } 

Java

package com.example;  import com.google.genai.Client; import com.google.genai.types.GenerateContentResponse;  public class GenerateTextFromTextInput {   public static void main(String[] args) {     // The client gets the API key from the environment variable `GOOGLE_API_KEY`.     Client client = new Client();      GenerateContentResponse response =         client.models.generateContent(             "gemini-2.5-flash",             "Explain how AI works in a few words",             null);      System.out.println(response.text());   } } 

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=YOUR_API_KEY" \   -H 'Content-Type: application/json' \   -X POST \   -d '{     "contents": [       {         "parts": [           {             "text": "Explain how AI works in a few words"           }         ]       }     ]   }' 

探索 API

原生图片生成

使用 Gemini 2.0 Flash 原生生成和编辑高度贴合情境的图片。

探索长上下文

向 Gemini 模型输入数百万个令牌,从非结构化图片、视频和文档中提取理解。

生成结构化输出

限制 Gemini 以 JSON 格式(一种适合自动处理的结构化数据格式)进行响应。

开始使用 Gemini API 进行构建

开始使用