Kurzanleitung: Gemini API

In dieser Kurzanleitung erfahren Sie, wie Sie unsere Bibliotheken installieren und Ihre erste Gemini API-Anfrage mit der Interactions API stellen.

Hinweis

Für die Verwendung der Gemini API ist ein API-Schlüssel erforderlich. Sie können kostenlos einen erstellen, um loszulegen.

Gemini API-Schlüssel erstellen

Google GenAI SDK installieren

Python

Installieren Sie mit Python 3.9+ das google-genai-Paket mit dem folgenden pip-Befehl:

pip install -q -U google-genai

JavaScript

Installieren Sie mit Node.js v18+ das Google Gen AI SDK für TypeScript und JavaScript mit dem folgenden npm-Befehl:

npm install @google/genai

Erste Anfrage senden

Hier ist ein Beispiel, in dem die Interactions API verwendet wird, um eine Anfrage mit dem Gemini 3 Flash-Modell an die Gemini API zu senden.

Wenn Sie Ihren API-Schlüssel als Umgebungsvariable GEMINI_API_KEY festlegen, wird er automatisch vom Client übernommen, wenn Sie die Gemini API-Bibliotheken verwenden. Andernfalls müssen Sie Ihren API-Schlüssel als Argument beim Initialisieren des Clients übergeben.

Beachten Sie, dass in allen Codebeispielen in der Gemini API-Dokumentation davon ausgegangen wird, dass Sie die Umgebungsvariable GEMINI_API_KEY festgelegt haben.

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"
  }'

Nächste Schritte

Nachdem Sie Ihre erste API-Anfrage gesendet haben, können Sie sich die folgenden Anleitungen ansehen, in denen Gemini in Aktion gezeigt wird: