วิธีใช้ Gemini API เริ่มต้น: สร้าง API key และเรียกใช้ครั้งแรก
สร้าง Gemini API key ใน Google AI Studio เก็บ key ให้ปลอดภัย ติดตั้ง SDK ทางการของ Python หรือ JavaScript แล้วส่ง request แรก
quickstart ของ Gemini API พาเราเรียกใช้ครั้งแรกผ่าน Interactions API ซึ่งใช้ได้ทั้งจาก SDK ของ Python, JavaScript และผ่าน REST [1] คู่มือนี้ทำตามขั้นแรกๆ คือเอา API key เก็บ key ให้ปลอดภัย ติดตั้ง SDK แล้วส่ง request แรก [1]
ขั้นที่ 1: เอา API key
คู่มือนี้ยืนยันตัวตนของ request ด้วย API key เหมือนใน quickstart ทางการ [1] ถ้าเป็นผู้ใช้ใหม่ Google AI Studio จะสร้าง Google Cloud project ตั้งต้นและ API key ให้หลังจากเรากดยอมรับ Terms of Service [2] แต่ถ้ามีบัญชี Google Cloud อยู่แล้ว AI Studio จะไม่สร้าง project ตั้งต้นให้ ต้อง import project ที่มีอยู่เข้ามาแทน [2] ก๊อป key ได้จากหน้า API keys ที่ https://aistudio.google.com/api-keys [1] ถ้าอยากได้ key เพิ่ม กด Create API key ใน AI Studio แล้วทำตาม dialog [1]
Gemini API key ทุกอันผูกกับ Google Cloud project ซึ่งเป็นตัวจัดการ billing, คนร่วมทีม และสิทธิ์ต่างๆ [2] project ที่สร้างเพิ่มหรือ import มาจาก Google Cloud อาจต้องผ่าน project verification ก่อนถึงจะใช้ free tier ได้ [1]
key ใหม่ที่สร้างใน AI Studio เป็น authorization key ที่ผูกกับ service account และจำกัดให้ใช้กับ Gemini API ตั้งแต่แรก [2] Gemini API ปฏิเสธ request จาก standard key ที่ไม่มีการจำกัด (unrestricted) ดังนั้น key เก่าอาจต้องตั้ง restriction ก่อนถึงจะใช้ได้ [2]
ขั้นที่ 2: เก็บ key ให้ปลอดภัย
Google แนะนำให้เก็บ key ไว้ใน environment variable ชื่อ GEMINI_API_KEY หรือ GOOGLE_API_KEY ซึ่ง client library จะดึงไปใช้เอง [2] ถ้าตั้งไว้ทั้งสองตัว GOOGLE_API_KEY จะถูกใช้ก่อน [2]
export GEMINI_API_KEY="YOUR_API_KEY"
ให้มอง key เหมือนรหัสผ่าน เพราะถ้าหลุด คนอื่นเอาไปใช้ quota ของเราและทำให้เกิดค่าใช้จ่ายได้ [2] เอกสารวางกฎไว้แบบนี้:
- ห้าม commit API key ขึ้น Git หรือ source control อื่น [2]
- อย่า hardcode key ไว้ในเว็บหรือแอปมือถือ ให้เรียก API ผ่าน backend proxy server แทน [2]
- บน production ให้เก็บ key ใน secret store เช่น Google Cloud Secret Manager [2]
- ถ้า key หลุด ให้สร้าง key ใหม่ deploy ให้เรียบร้อย แล้วค่อยปิด key เก่าใน Cloud console [2]
ขั้นที่ 3: ติดตั้ง SDK
ฝั่ง Python ติดตั้ง google-genai [1]:
pip install -U google-genai
ฝั่ง JavaScript หรือ TypeScript ติดตั้ง @google/genai [1]:
npm install @google/genai
ขั้นที่ 4: ส่ง request แรก
ใน Python สร้าง client แล้วเรียก client.interactions.create [1]:
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="Explain how AI works in a few words"
)
print(interaction.output_text)
ใน JavaScript หน้าตาเป็นแบบนี้ [1]:
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const interaction = await ai.interactions.create({
model: "gemini-4-hydrogen",
input: "Explain how AI works in a few words",
});
console.log(interaction.output_text);
ทั้งสองตัวอย่างไม่ได้ใส่ key ในโค้ด เพราะ client อ่านจาก environment variable ให้เอง [2] ส่วน property output_text ช่วยให้ได้ข้อความคำตอบสุดท้ายทันที ไม่ต้องไล่แกะ response ทั้งก้อน [1]
Free tier และ limit
free tier ให้ใช้บางโมเดลแบบจำกัด โดยไม่คิดค่า input และ output token [3] ในหน้า pricing gemini-4-hydrogen ไม่คิดค่า input และ output บน free tier ส่วนบางโมเดล เช่น Gemini 4 Carbon ไม่มีให้ใช้บน free tier [3]
rate limit นับต่อ project ไม่ใช่ต่อ API key และ quota รายวันจะรีเซ็ตตอนเที่ยงคืนเวลา Pacific [4] ดู limit ปัจจุบันได้ใน Google AI Studio และ limit จะเปลี่ยนตาม usage tier ของเรา [4] ถ้าอยากได้ limit สูงขึ้น กด Set up billing ในหน้า API keys [1]
Error ที่เจอบ่อย
แหล่งอ้างอิง
เขียนและตรวจโดย AI จากแหล่งอ้างอิงด้านล่าง
ตรวจข้อเท็จจริงโดย gpt-6.1-sol เมื่อ · นโยบายบรรณาธิการ