DEV Community

Cover image for วิธีใช้ Gemini 3.5 Flash API
Thanawat Wongchai
Thanawat Wongchai

Posted on • Originally published at apidog.com

วิธีใช้ Gemini 3.5 Flash API

Gemini 3.5 Flash API เปิดตัวพร้อมกับโมเดลเมื่อวันที่ 19 พฤษภาคม 2026 Flash เป็นโมเดลเดียวในตระกูล 3.5 ที่มีให้บริการในปัจจุบัน ส่วน Pro จะเปิดตัวในเดือนมิถุนายน คู่มือนี้สรุปขั้นตอนสำหรับนักพัฒนา: ขอคีย์, เรียกใช้งานครั้งแรก, ส่งอินพุตหลายรูปแบบ, เปิดสตรีมมิ่ง, ใช้เครื่องมือ, และทดสอบ API ด้วย Apidog

ลองใช้ Apidog วันนี้

หากคุณเคยใช้ Gemini API มาก่อน รูปแบบการใช้งานยังเหมือนเดิม สิ่งที่ต้องเปลี่ยนคือชื่อโมเดลเป็น gemini-3.5-flash หากคุณเริ่มใหม่ สามารถเรียกใช้ Flash ได้ภายในประมาณ 10 นาที

Gemini 3.5 Flash API

คุณได้อะไรจาก Gemini 3.5 Flash API

สิ่งที่ต้องรู้ก่อนเริ่มใช้งาน:

  • gemini-3.5-flash: พร้อมใช้งานแล้ว, เร็ว, ถูก, รองรับหลายรูปแบบ (multimodal)
  • Endpoint รูปแบบ OpenAPI-style เดิม: ใช้แทนในโปรเจกต์ที่เรียก Gemini 3 หรือ 3.1 ได้ทันที
  • Free tier บน AI Studio: ประมาณ 1,500 คำขอต่อวันโดยไม่ต้องใช้บัตรเครดิต

ความสามารถหลักที่เปิดผ่าน Flash API:

  • อินพุต 1M โทเค็น, เอาต์พุต 64K โทเค็น
  • อินพุตข้อความ + รูปภาพ, เอาต์พุตข้อความ + โครงสร้าง
  • การเรียกใช้ฟังก์ชันและเครื่องมือแบบเนทีฟ (83.6% MCP Atlas)
  • การสตรีมคำตอบ เร็วกว่าโมเดลรุ่นใหม่ๆ ประมาณ 4 เท่าในแง่เอาต์พุตโทเค็นต่อวินาที
  • คะแนนการดึงข้อมูลจากบริบทขนาดยาวอยู่ด้านบนของตาราง MRCR v2 ของ Google
  • การให้เหตุผลจากแผนภูมิและเอกสาร (84.2% CharXiv)

สำหรับรายละเอียดราคา รวมถึงอัตราต่อโทเค็นและส่วนลดโหมดแบตช์ ดู คู่มือการกำหนดราคา Gemini 3.5 Flash

ขั้นตอนที่ 1: ขอ API Key ของ Gemini 3.5 Flash

เลือกได้ 2 เส้นทาง ขึ้นอยู่กับว่าคุณต้องการโควต้าฟรีหรือการใช้งานแบบ production

เส้นทาง A: Google AI Studio สำหรับแพ็คเกจฟรี

  1. ไปที่ aistudio.google.com
  2. ลงชื่อเข้าใช้ด้วยบัญชี Google
  3. คลิก Get API key ในแถบนำทางด้านซ้าย
  4. เลือกโปรเจกต์ที่มีอยู่แล้วหรือสร้างโปรเจกต์ใหม่
  5. คลิก Create API key
  6. คัดลอกคีย์และเก็บไว้ใน environment variable

ตัวอย่าง:

export GEMINI_API_KEY="YOUR_API_KEY"
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนนี้เหมือนกับ คู่มือการขอ API Key ของ Gemini ฟรี คีย์นี้ใช้กับ gemini-3.5-flash ได้ทันทีพร้อมโควต้าฟรีรายวัน

Google AI Studio API key

เส้นทาง B: Vertex AI สำหรับ production

สำหรับ workload ที่ต้องมี billing, IAM, และ audit logs:

  1. เปิดใช้งาน Vertex AI API ใน Google Cloud Console
  2. สร้าง service account พร้อมสิทธิ์ aiplatform.user
  3. ดาวน์โหลด credentials JSON
  4. ยืนยันตัวตนผ่านคำสั่งนี้หรือใช้ไฟล์ JSON
gcloud auth application-default login
Enter fullscreen mode Exit fullscreen mode

Vertex ใช้รูปแบบ SDK ต่างจาก AI Studio เล็กน้อย ทีมส่วนใหญ่มักเริ่มจาก AI Studio แล้วค่อยย้ายไป Vertex เมื่อต้องการการควบคุมระดับองค์กร

ขั้นตอนที่ 2: ติดตั้ง SDK

Google GenAI SDK อย่างเป็นทางการมีให้สำหรับ Python, Node.js, Go และ Java

# Python
pip install -U google-genai

# Node.js
npm install @google/genai

# Go
go get google.golang.org/genai
Enter fullscreen mode Exit fullscreen mode

หากไม่ต้องการใช้ SDK คุณสามารถเรียก REST endpoint โดยตรงได้ ดูตัวอย่าง curl ในขั้นตอนถัดไป

ขั้นตอนที่ 3: เรียกใช้ Flash ครั้งแรก

Python

import os
from google import genai

client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

response = client.models.generate_content(
    model="gemini-3.5-flash",
    contents="Explain how OAuth 2.0 PKCE flow works in 3 short paragraphs."
)

print(response.text)
Enter fullscreen mode Exit fullscreen mode

Node.js

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const response = await ai.models.generateContent({
  model: "gemini-3.5-flash",
  contents: "Explain how OAuth 2.0 PKCE flow works in 3 short paragraphs.",
});

console.log(response.text);
Enter fullscreen mode Exit fullscreen mode

curl

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.5-flash:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{
      "parts": [{"text": "Explain how OAuth 2.0 PKCE flow works in 3 short paragraphs."}]
    }]
  }'
Enter fullscreen mode Exit fullscreen mode

หลังจากเรียกสำเร็จ คุณสามารถเพิ่มฟีเจอร์อื่น เช่น streaming, multimodal input, function calling หรือ JSON schema ได้ทันที

การสตรีมคำตอบ

Flash สร้างเอาต์พุตได้เร็ว การเปิด streaming ช่วยให้ UI แสดงผลระหว่างที่โมเดลกำลังตอบ

Python

stream = client.models.generate_content_stream(
    model="gemini-3.5-flash",
    contents="Write a 5-step tutorial on writing a REST API client in Go."
)

for chunk in stream:
    print(chunk.text, end="", flush=True)
Enter fullscreen mode Exit fullscreen mode

Node.js

const stream = await ai.models.generateContentStream({
  model: "gemini-3.5-flash",
  contents: "Write a 5-step tutorial on writing a REST API client in Go.",
});

for await (const chunk of stream) {
  process.stdout.write(chunk.text);
}
Enter fullscreen mode Exit fullscreen mode

สำหรับ REST endpoint ให้เปลี่ยน suffix จาก :generateContent เป็น :streamGenerateContent

อินพุตหลายรูปแบบด้วย Flash

Gemini 3.5 Flash รับรูปภาพพร้อมข้อความได้ คะแนน CharXiv Reasoning 84.2% ทำให้เหมาะกับงานอ่านแผนภูมิ, dashboard, และเอกสารภาพ

Python: ส่งรูปภาพจากดิสก์

import os
from google import genai
from google.genai import types

client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

with open("dashboard.png", "rb") as f:
    image_bytes = f.read()

response = client.models.generate_content(
    model="gemini-3.5-flash",
    contents=[
        types.Part.from_bytes(data=image_bytes, mime_type="image/png"),
        "Extract every metric in this dashboard as a JSON object."
    ]
)

print(response.text)
Enter fullscreen mode Exit fullscreen mode

ประเภท MIME ที่รองรับ:

  • image/png
  • image/jpeg
  • image/webp
  • image/heic
  • image/heif

ไฟล์ PDF และวิดีโอใช้งานได้ผ่าน types.Part.from_uri()

การเรียกใช้ฟังก์ชันและเครื่องมือด้วย Flash

Function calling คือจุดที่ Flash เหมาะกับ agent และ workflow อัตโนมัติ คะแนน MCP Atlas 83.6% หมายความว่า Flash เลือกเครื่องมือที่ถูกต้องได้อย่างน่าเชื่อถือมากกว่ารุ่น 3.1

Python

from google.genai import types

weather_tool = types.Tool(
    function_declarations=[{
        "name": "get_current_weather",
        "description": "Get the current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
            },
            "required": ["city"]
        }
    }]
)

response = client.models.generate_content(
    model="gemini-3.5-flash",
    contents="What's the weather in Singapore right now?",
    config=types.GenerateContentConfig(tools=[weather_tool])
)

for part in response.candidates[0].content.parts:
    if part.function_call:
        print(f"Call: {part.function_call.name}")
        print(f"Args: {dict(part.function_call.args)}")
Enter fullscreen mode Exit fullscreen mode

Flash จะส่งคืนอ็อบเจกต์ function_call พร้อมชื่อฟังก์ชันและอาร์กิวเมนต์ จากนั้นคุณต้อง:

  1. ตรวจสอบชื่อฟังก์ชัน
  2. validate arguments
  3. เรียกใช้ฟังก์ชันจริงในระบบของคุณ
  4. ส่งผลลัพธ์กลับเข้า conversation
  5. ให้โมเดลตอบต่อ

รูปแบบนี้ตรงกับที่ทีมใช้งานกับ Gemini 3 Flash API

เอาต์พุตที่มีโครงสร้างด้วย JSON mode

หากต้องการเอาต์พุตที่ parse ได้ ให้ตั้งค่า MIME type และ schema ของคำตอบ

response = client.models.generate_content(
    model="gemini-3.5-flash",
    contents="List 3 popular API testing tools with their pricing.",
    config=types.GenerateContentConfig(
        response_mime_type="application/json",
        response_schema={
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "price_per_month": {"type": "number"},
                    "free_tier": {"type": "boolean"}
                },
                "required": ["name", "free_tier"]
            }
        }
    )
)

import json
data = json.loads(response.text)
Enter fullscreen mode Exit fullscreen mode

แนวทางนี้ช่วยลดการ parse ด้วย regex และลด retry loop ที่เกิดจาก JSON ไม่ถูกต้อง

การกำหนดราคา ณ เดือนพฤษภาคม 2026

อัตรา pay-as-you-go สำหรับ gemini-3.5-flash:

ระดับ อินพุต เอาต์พุต
มาตรฐาน ~1.50 ดอลลาร์ / 1M โทเค็น ~9.00 ดอลลาร์ / 1M โทเค็น
อินพุตที่แคช อัตราลดลง ไม่มี
โหมดแบตช์ ลด ~50% ลด ~50%

สำหรับ workload แบบ batch ที่ไม่ต้องการ latency แบบ real-time ให้พิจารณา โหมดแบตช์ของ Gemini API ซึ่งให้ส่วนลด 50%

สำหรับรายละเอียดการคำนวณราคาและตัวอย่างต้นทุนจริงของ SaaS workload หรือ agent loops ดู รายละเอียดการกำหนดราคา Flash และดูข้อมูลอ้างอิงอย่างเป็นทางการที่ Gemini Developer API pricing

การทดสอบ Gemini 3.5 Flash integration ด้วย Apidog

SDK call ที่ทำงานได้เป็นเพียงขั้นแรก Production integration ยังต้องทดสอบหลายส่วน:

  • streaming chunks
  • tool call payload
  • multimodal request
  • JSON schema validation
  • retry เมื่อ error
  • rate limit
  • mock response สำหรับ downstream service

Apidog Gemini Flash testing

Apidog ช่วยจัดการ Gemini Flash API ใน workspace เดียว:

  • บันทึก Flash endpoint เป็น request: วาง URL เต็ม, แนบ x-goog-api-key, แล้วกด Send
  • เล่นซ้ำข้ามเวอร์ชันโมเดล: เปลี่ยน gemini-3.5-flash เป็น gemini-3-flash ใน request เดิมเพื่อเทียบผลลัพธ์
  • ดู streaming response แบบ inline: แสดง chunks ที่สตรีมเข้ามาพร้อม timing
  • ตรวจสอบ JSON schema: ใช้ assertion จับ response ที่เปลี่ยนหลังปรับ prompt
  • mock Flash endpoint: สร้าง response จำลองเพื่อทดสอบ downstream code โดยไม่ใช้ quota
  • สร้าง test scenario สำหรับ agent loops: เรียก Flash หลายครั้งพร้อมตรวจสอบ tool call ระหว่างขั้นตอน

วิธีเริ่มต้น:

  1. ดาวน์โหลด Apidog
  2. สร้าง request ใหม่
  3. ตั้ง URL เป็น Flash endpoint
  4. เพิ่ม header x-goog-api-key
  5. นำเข้า payload จากตัวอย่าง curl
  6. กด Send และบันทึกเป็น test case

การจัดการข้อผิดพลาดและ rate limit

รหัสข้อผิดพลาดที่ควร handle:

  • 400: request ไม่ถูกต้อง เช่น contents ผิดรูปแบบ หรือ MIME type ไม่รองรับ
  • 401: API key ไม่ถูกต้อง
  • 403: quota หมด หรือโมเดลยังไม่เปิดใช้งาน
  • 429: ถูกจำกัดอัตรา ให้รอสักครู่แล้วลองใหม่
  • 500/503: server-side error ให้ retry ด้วย exponential backoff

ตัวอย่าง retry wrapper:

import time
from google import genai

def call_with_retry(client, model, prompt, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.models.generate_content(model=model, contents=prompt)
        except Exception as e:
            if attempt == max_retries - 1:
                raise
            time.sleep(2 ** attempt)
Enter fullscreen mode Exit fullscreen mode

โควต้า free tier รีเซ็ตทุกวัน โดย Flash มีประมาณ 15 คำขอต่อนาที และประมาณ 1,500 คำขอต่อวัน ส่วน production quota รีเซ็ตทั้งรายนาทีและรายวัน

สำหรับ workload ปริมาณมาก ให้พิจารณา batch mode หรือ fallback แบบแบ่งระดับไปยัง Gemini 3 Flash เมื่อถึงขีดจำกัด

การย้ายจาก Gemini 3.1 ไปยัง 3.5 Flash

โปรเจกต์ส่วนใหญ่เปลี่ยนแค่ชื่อโมเดล

# ก่อนหน้านี้
model="gemini-3.1-pro"  # หรือ gemini-3.1-flash

# หลังจากนั้น
model="gemini-3.5-flash"
Enter fullscreen mode Exit fullscreen mode

สิ่งที่ควรทดสอบหลังเปลี่ยนโมเดล:

  1. Tool schemas: ตรวจว่า schema เดิมยังทำงานกับ tool call ได้ถูกต้อง
  2. ความเร็ว streaming: Flash สตรีมเร็วขึ้นประมาณ 4 เท่า UI อาจต้อง throttle การแสดงผล
  3. Token budget: ขีดจำกัด 1M / 64K ยังเหมือนเดิม แต่ prompt เดิมอาจสร้าง output tokens ต่างจากเดิม
  4. รูปแบบการปฏิเสธ: safety behavior อาจต่างกันใน edge cases

สำหรับรายละเอียดรูปแบบ SDK เพิ่มเติม ดู คู่มือ Gemini 3.1 Pro API

รูปแบบการใช้งาน Flash ทั่วไป

1. วิเคราะห์เอกสารบริบทขนาดยาว

with open("large_report.pdf", "rb") as f:
    pdf_bytes = f.read()

response = client.models.generate_content(
    model="gemini-3.5-flash",
    contents=[
        types.Part.from_bytes(data=pdf_bytes, mime_type="application/pdf"),
        "Summarize the financial outlook from this report in 5 bullet points."
    ]
)
Enter fullscreen mode Exit fullscreen mode

บริบท 1M โทเค็นของ Flash ช่วยให้จัดการ PDF ขนาดใหญ่ได้โดยไม่ต้องแบ่งไฟล์เป็นหลายส่วนในหลายกรณี

2. Agent loop พร้อม tool calling

conversation = [{"role": "user", "parts": [{"text": "Book me a flight to Tokyo"}]}]

while True:
    response = client.models.generate_content(
        model="gemini-3.5-flash",
        contents=conversation,
        config=types.GenerateContentConfig(tools=[flight_search_tool, booking_tool])
    )

    part = response.candidates[0].content.parts[0]
    if not part.function_call:
        print(part.text)
        break

    result = execute_tool(part.function_call)
    conversation.append({"role": "model", "parts": [part]})
    conversation.append({"role": "user", "parts": [{"function_response": result}]})
Enter fullscreen mode Exit fullscreen mode

นี่คือรูปแบบลูปที่เกี่ยวข้องกับคะแนน Terminal-Bench 2.1 ของ Flash (76.2%) และเหมาะกับ agent ที่ต้องเรียกเครื่องมือหลายขั้นตอน

คำถามที่พบบ่อย

มีแพ็คเกจฟรีสำหรับ Gemini 3.5 Flash API หรือไม่?

มี ผ่าน Google AI Studio พร้อมโควต้ารายวันประมาณ 1,500 คำขอต่อวัน โดยไม่ต้องใช้บัตรเครดิต

Flash รองรับ OpenAI-compatible endpoints หรือไม่?

รองรับ Google มี OpenAI-compatible shim ที่ /v1beta/openai/ คุณสามารถตั้งค่า base_url ใน OpenAI SDK และใช้ Gemini API key ได้ ชื่อโมเดลยังคงเป็น gemini-3.5-flash

ใช้ Flash กับ LangChain หรือ LlamaIndex ได้หรือไม่?

ได้ ทั้งสองมี integration กับ Gemini ในตัว ให้ส่ง model="gemini-3.5-flash" ใน wrapper ที่ใช้งาน

Gemini 3.5 Pro จะเปิดตัวเมื่อไหร่?

มิถุนายน 2026 ตามประกาศเปิดตัวของ Google จนกว่าจะถึงตอนนั้น Flash เป็นโมเดล 3.5 เพียงรุ่นเดียวที่มีให้ใช้งาน

ขนาดรูปภาพสูงสุดสำหรับ Flash คือเท่าไหร่?

แนะนำที่ 3072×3072 รูปภาพที่ใหญ่กว่าจะถูก resample สำหรับงาน OCR หนักๆ ดู เวิร์กโฟลว์ Gemini 2.0 Flash OCR ซึ่งใช้รูปแบบเดียวกันได้

ทดสอบ streaming endpoints ใน Apidog ได้อย่างไร?

เปิด request, ตั้ง endpoint suffix เป็น :streamGenerateContent แล้ว Apidog จะแสดง SSE chunks ทันทีที่มาถึง เหมาะสำหรับดีบักคำตอบที่ไม่สมบูรณ์

ดู API logs ได้ที่ไหน?

ใน AI Studio ใต้เมนู “Activity” หรือใน Vertex AI ใต้ “Logs Explorer” สำหรับ production deployment

สิ่งที่ควรสร้างเป็นอันดับแรก

โปรเจกต์เริ่มต้นที่เหมาะกับสัปดาห์แรกของการใช้ Flash:

  • บอทถามตอบ PDF: ใส่ PDF ลงในบริบท 1M แล้วถามคำถามพร้อมรับคำตอบแบบมีอ้างอิง
  • pipeline แปลงแผนภูมิเป็น JSON: ส่งภาพ dashboard แล้วดึงข้อมูลเป็นโครงสร้าง
  • ตัวแทนฝ่ายสนับสนุนลูกค้า: ใช้ function calling เชื่อม CRM และทำงานซ้ำๆ อัตโนมัติ
  • ผู้ช่วย review code: ส่ง diff หลายไฟล์ แล้วให้เอาต์พุตแบบมี severity
  • agent สำหรับ internal search: รวมบริบท 1M กับการเรียก API ภายใน

สำหรับทุกโปรเจกต์ ใช้ workflow เดียวกัน: เขียน prompt, ครอบด้วย SDK call, ตรวจรูปแบบ response ด้วย Apidog, แล้วค่อยนำไป deploy

Top comments (0)