Gemini API のクイックスタート
このクイックスタートでは、ライブラリをインストールし、Interactions API を使用して最初の Gemini API リクエストを行う方法について説明します。
始める前に
Gemini API を使用するには API キーが必要です。無料で作成して開始できます。
Google GenAI SDK をインストールする
Python
Python 3.9 以降を使用して、次の pip コマンドを使用して google-genai パッケージをインストールします。
pip install -q -U google-genai
JavaScript
Node.js v18 以降を使用して、次の npm コマンドを使用して Google Gen AI SDK for TypeScript と JavaScript をインストールします。
npm install @google/genai
最初のリクエストを送信する
次の例では、Interactions API を使用して、Gemini 3 Flash モデルを使用して Gemini API にリクエストを送信しています。
API キーを環境変数 GEMINI_API_KEY として設定すると、Gemini API ライブラリを使用するときにクライアントによって自動的に選択されます。それ以外の場合は、クライアントを初期化するときに引数として API キーを渡す必要があります。
Gemini API ドキュメントのすべてのコードサンプルでは、環境変数 GEMINI_API_KEY が設定されていることを前提としています。
Python
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
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
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-flash-preview",
"input": "Explain how AI works in a few words"
}'
次のステップ
最初の API リクエストが完了したので、Gemini の動作を示す次のガイドをご覧ください。