คู่มือเริ่มใช้งาน Gemini API ฉบับย่อ

คู่มือเริ่มใช้งานฉบับย่อนี้แสดงวิธีติดตั้งไลบรารี และทำการขอ API ของ Gemini ครั้งแรกโดยใช้ Interactions API

ก่อนเริ่มต้น

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

สร้างใน AI Studio ได้ฟรีเพื่อเริ่มต้นใช้งาน

สร้างคีย์ Gemini API

ติดตั้ง Google GenAI SDK

Python

ใช้ Python 3.9 ขึ้นไป เพื่อติดตั้งแพ็กเกจgoogle-genai โดยใช้คำสั่ง pip ต่อไปนี้

pip install -q -U google-genai

JavaScript

หากใช้ Node.js v18 ขึ้นไป ให้ ติดตั้ง Google Gen AI SDK สำหรับ TypeScript และ JavaScript โดยใช้คำสั่ง npm ต่อไปนี้

npm install @google/genai

ส่งคำขอแรก

คุณส่งคำขอไปยัง Gemini API ได้ 2 วิธี ดังนี้

  • (แนะนํา) Interactions API เป็น Primitive ใหม่ที่รองรับการใช้เครื่องมือแบบหลายขั้นตอน การจัดการเป็นกลุ่ม และโฟลว์การให้เหตุผลที่ซับซ้อนผ่านขั้นตอนการดำเนินการที่พิมพ์ หลังจากนี้เป็นต้นไป โมเดลใหม่ๆ ที่นอกเหนือจากตระกูลหลัก รวมถึงความสามารถด้าน Agentic AI และเครื่องมือใหม่ๆ จะเปิดตัวใน Interactions API เท่านั้น
  • generateContent มีวิธีสร้างการตอบกลับแบบง่ายๆ ที่ไม่เก็บสถานะจากโมเดล แม้ว่าเราจะแนะนำให้ใช้ Interactions API แต่ generateContent ก็ได้รับการรองรับอย่างเต็มที่

ตัวอย่างนี้ใช้ Interactions API เพื่อส่งคำขอไปยัง Gemini API โดยใช้โมเดล Gemini 3 Flash

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

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

Python

# This will only work for SDK newer than 2.0.0
from google import genai

# The client gets the API key from the environment variable `GEMINI_API_KEY`.
client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview", 
    input="Explain how AI works in a few words"
)

# Print the model's text response
for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)

JavaScript

// This will only work for SDK newer than 2.0.0
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 interaction = await ai.interactions.create({
    model: "gemini-3-flash-preview",
    input: "Explain how AI works in a few words",
  });

  const modelStep = interaction.steps.find(s => s.type === 'model_output');
  if (modelStep) {
    for (const contentBlock of modelStep.content) {
      if (contentBlock.type === 'text') console.log(contentBlock.text);
    }
  }
}

main();

REST

# Specifies the API revision to avoid breaking changes when they become default
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -H "Api-Revision: 2026-05-20" \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": "Explain how AI works in a few words"
  }'

โหมดแบบไม่เก็บสถานะ

โดยค่าเริ่มต้น Interactions API จะจัดการสถานะการสนทนาฝั่งเซิร์ฟเวอร์เมื่อคุณใช้ previous_interaction_id หากต้องการจัดการประวัติการสนทนาด้วยตนเองในฝั่งไคลเอ็นต์ คุณสามารถเลือกใช้โหมดแบบไม่เก็บสถานะได้โดยตั้งค่า store=false และส่งขั้นตอนที่สะสมไว้ในช่อง input ของคำขอที่ตามมา

ดูรายละเอียดและตัวอย่างแบบไม่เก็บสถานะแบบหลายรอบฉบับเต็มได้ที่คู่มือการสร้างข้อความ

ขั้นตอนถัดไป

ตอนนี้คุณได้ส่งคำขอ API แรกแล้ว คุณอาจต้องการดูคำแนะนำต่อไปนี้ที่แสดงการทำงานของ Gemini