Gemini API のクイックスタート
このクイックスタートでは、ライブラリをインストールし、Interactions API を使用して最初の Gemini API リクエストを行う方法について説明します。
始める前に
Gemini API を使用するには、リクエストの認証、セキュリティ制限の適用、アカウントの使用状況の追跡を行うための API キーが必要です。
AI Studio で無料で作成して、使用を開始します。
Google GenAI SDK をインストールする
Python
Python 3.9+ 以降を使用して、次の
pip コマンドで
google-genai パッケージをインストールします。
pip install -q -U google-genai
JavaScript
Node.js v18+ 以降を使用して、次の npm コマンドで TypeScript と JavaScript 用の Google Gen AI SDK をインストールします。
npm install @google/genai
最初のリクエストを送信する
を使用して Gemini API にリクエストを送信する方法は 2 つあります。
- (推奨) Interactions API は、型付き実行ステップによるマルチステップ ツール使用、オーケストレーション、複雑な推論フローをネイティブにサポートする新しいプリミティブです。今後、コア メインライン ファミリー以外の新しいモデル、新しいエージェント機能とツールは、Interactions API でのみリリースされます。
generateContentを使用すると、モデルからシンプルなステートレス レスポンスを生成できます。Interactions API を使用することをおすすめしますが、generateContentは完全にサポートされています。
この例では、Interactions API を使用して、Gemini 3 Flash モデルを使用して Gemini API にリクエストを送信します。
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"
}'
ステートレス モード
デフォルトでは、previous_interaction_id を使用すると、Interactions API は会話の状態をサーバー側で管理します。会話履歴をクライアント側で自分で管理する場合は、store=false を設定し、後続のリクエストの input フィールドに累積されたステップを渡すことで、ステートレス モードを選択できます。
詳細と完全なマルチターン ステートレスの例については、テキスト生成ガイドをご覧ください。
次のステップ
最初の API リクエストを行ったので、Gemini の動作を示す次のガイドをご覧ください。