このガイドでは、Interactions API を使用して Gemini API を使い始める方法について説明します。1 分以内に最初の API 呼び出しを行い、テキスト生成、マルチモーダル理解、画像生成、構造化出力、ツール、関数呼び出し、エージェント、バックグラウンド実行について学習します。
Interactions API は、Python と JavaScript の SDK と REST を通じて利用できます。
1. API キーを取得する
Gemini API を使用するには、リクエストの認証、セキュリティ上限の適用、アカウントの使用状況の追跡を行うための API キーが必要です。
- Google AI Studio では、新規ユーザー向けにプロジェクトと API キーが自動的に作成されます。API キーのページからコピーできます。
- 新しいキーが必要な場合は、AI Studio で [API キーを作成] をクリックし、ダイアログに沿って新しいキーとプロジェクトのペアを追加します。
鍵を環境変数として設定します。
export GEMINI_API_KEY="YOUR_API_KEY"
有料ティアにアップグレードする
有料階層にアップグレードすると、レートの上限が引き上げられます。また、Cloud Billing の設定が必要になります。
- AI Studio の API キーページまたはプロジェクトページで、[お支払い情報を設定] をクリックします。
- Cloud Billing ダイアログに沿って、請求先アカウントを作成またはリンクし、お支払い方法を追加して、有料クレジットで最低 10 ドル(または同等の通貨)を前払いします。
- API の使用状況は、Google AI Studio の [ダッシュボード] > [使用状況] で確認できます。
詳細については、お支払いページをご覧ください。
2. SDK をインストールして最初の呼び出しを行う
SDK をインストールし、1 回の API 呼び出しでテキストを生成します。
Python
SDK をインストールします。
pip install -U google-genai
クライアントを初期化してリクエストを行います。
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.6-flash",
input="Explain how AI works in a few words"
)
print(interaction.output_text)
JavaScript
SDK をインストールします。
npm install @google/genai
クライアントを初期化してリクエストを行います。
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const interaction = await ai.interactions.create({
model: "gemini-3.6-flash",
input: "Explain how AI works in a few words",
});
console.log(interaction.output_text);
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3.6-flash",
"input": "Explain how AI works in a few words"
}'
回答:
{
"id": "v1_ChdpQUFvYXI...",
"status": "completed",
"usage": {
"total_tokens": 197,
"total_input_tokens": 8,
"total_output_tokens": 12
},
"created": "2026-06-09T12:01:25Z",
"steps": [
{
"type": "thought",
"signature": "EvEFCu4FAQw..."
},
{
"type": "model_output",
"content": [
{
"type": "text",
"text": "AI learns patterns from data, then uses those patterns to make predictions or decisions on new data."
}
]
}
],
"object": "interaction",
"model": "gemini-3.6-flash",
}
REST を使用すると、API はメタデータ、使用状況の統計情報、ターンのステップバイステップの履歴を含む完全な Interaction リソースを返します。
SDK は完全なレスポンスを公開しますが、最終的な出力に直接アクセスするための interaction.output_text や interaction.output_image などの便利なプロパティも提供します。レスポンス構造について詳しくは、インタラクションの概要をご覧ください。システム メッセージと生成構成の詳細については、テキスト生成ガイドをご覧ください。
3. レスポンスをストリーミングする
よりスムーズなやり取りを実現するには、レスポンスを生成しながらストリーミングします。各 step.delta イベントは、すぐに表示できるテキストのチャンクを配信します。
Python
from google import genai
client = genai.Client()
stream = client.interactions.create(
model="gemini-3.6-flash",
input="Explain how AI works",
stream=True
)
for event in stream:
print(event)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const stream = await ai.interactions.create({
model: "gemini-3.6-flash",
input: "Explain how AI works",
stream: true,
});
for await (const event of stream) {
console.log(event);
}
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions?alt=sse" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
--no-buffer \
-d '{
"model": "gemini-3.6-flash",
"input": "Explain how AI works",
"stream": true
}'
ストリーミングの場合、サーバーはサーバー送信イベント(SSE)のストリームで応答します。各イベントには、タイプと JSON データが含まれます。
回答:
event: interaction.created
data: {"interaction":{"id":"v1_Chd...","status":"in_progress","model":"gemini-3.6-flash"},"event_type":"interaction.created"}
event: step.start
data: {"index":0,"step":{"type":"thought"},"event_type":"step.start"}
event: step.delta
data: {"index":0,"delta":{"signature":"EvEFCu4F...","type":"thought_signature"},"event_type":"step.delta"}
event: step.stop
data: {"index":0,"event_type":"step.stop"}
event: step.start
data: {"index":1,"step":{"type":"model_output"},"event_type":"step.start"}
event: step.delta
data: {"index":1,"delta":{"text":"AI ","type":"text"},"event_type":"step.delta"}
event: step.delta
data: {"index":1,"delta":{"text":"works ","type":"text"},"event_type":"step.delta"}
event: step.s