🔵
Gemini API 使い方ガイド
Google の Gemini API の使い方。Gemini 2.5 Pro / Flash 対応。無料枠が大きく学習に最適。随時更新。
最終更新: 2026年3月
1. APIキー取得
- Google AI Studio にアクセス
- Get API key → Create API key
- export GEMINI_API_KEY=”…” で環境変数に設定
💡 Gemini API は無料枠が非常に大きく、個人開発や学習には最適です。
2. SDK インストール
Python
pip install google-genai
TypeScript / Node.js
npm install @google/genai
3. 最初のAPI呼び出し
Python
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-pro",
contents="日本語で自己紹介してください"
)
print(response.text)
TypeScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const response = await ai.models.generateContent({
model: "gemini-2.5-pro",
contents: "日本語で自己紹介してください",
});
console.log(response.text);
4. モデル一覧
| モデル | 特徴 | 用途 |
|---|---|---|
| Gemini 2.5 Pro | 最高性能、100万トークン | 複雑な推論、長文分析 |
| Gemini 2.5 Flash | 高速・低コスト | チャット、分類、大量処理 |
💡 Gemini の強み: 100万トークンの超長コンテキスト、大きな無料枠、Google検索との統合(Grounding)。
5. マルチモーダル入力
画像、PDF、動画、音声を直接入力できます。
from google.genai import types
from pathlib import Path
image = types.Part.from_bytes(
data=Path("photo.jpg").read_bytes(),
mime_type="image/jpeg"
)
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[image, "この画像を説明してください"]
)
6. ストリーミング
for chunk in client.models.generate_content_stream(
model="gemini-2.5-flash",
contents="短い物語を書いて"
):
print(chunk.text, end="")
7. 料金体系
| モデル | 入力 / 1M tokens | 出力 / 1M tokens | 無料枠 |
|---|---|---|---|
| Gemini 2.5 Pro | $1.25 / $2.50 | $10 / $15 | あり |
| Gemini 2.5 Flash | $0.15 | $0.60 / $3.50 | あり(大容量) |
※ 無料枠は1日あたりのリクエスト制限あり。学習・プロトタイピングには十分。