Gemini Developer API

รับคีย์ Gemini API และส่งคําขอ API รายการแรกได้ในไม่กี่นาที

หากคุณตั้งค่าคีย์ API เป็นตัวแปรสภาพแวดล้อม GEMINI_API_KEY ไคลเอ็นต์จะรับคีย์โดยอัตโนมัติเมื่อใช้ไลบรารี Gemini API ไม่เช่นนั้น คุณจะต้องส่งคีย์ API เป็นอาร์กิวเมนต์เมื่อเริ่มต้นใช้งานไคลเอ็นต์

Python

from google import 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`.
const ai = new GoogleGenAI({});

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()
    // The client gets the API key from the environment variable `GEMINI_API_KEY`.
    client, err := genai.NewClient(ctx, nil))
    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 `GEMINI_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" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [
      {
        "parts": [
          {
            "text": "Explain how AI works in a few words"
          }
        ]
      }
    ]
  }'

พบกับโมเดล

ใช้ Gemini ใน Google AI Studio

สำรวจ API

การสร้างรูปภาพแบบเนทีฟ

สร้างและแก้ไขรูปภาพที่ตรงตามบริบทสูงได้โดยตรงด้วย Gemini 2.0 Flash

สำรวจบริบทแบบยาว

ป้อนโทเค็นหลายล้านรายการลงในโมเดล Gemini และดึงข้อมูลเชิงลึกจากรูปภาพ วิดีโอ และเอกสารที่ไม่มีโครงสร้าง

สร้างเอาต์พุตที่มีโครงสร้าง

จำกัดให้ Gemini ตอบกลับด้วย JSON ซึ่งเป็นรูปแบบ Structured Data ที่เหมาะสำหรับการประมวลผลอัตโนมัติ

เริ่มสร้างด้วย Gemini API

เริ่มต้นใช้งาน