Guia de início rápido da API Gemini

Este guia de início rápido mostra como instalar nossas bibliotecas e fazer sua primeira solicitação de API Gemini usando a API Interactions.

Antes de começar

O uso da API Gemini requer uma chave de API. Você pode criar uma sem custo financeiro para começar.

Criar uma chave da API Gemini

Instalar o SDK da IA generativa do Google

Python

Usando Python 3.9 ou mais recente, instale o google-genai pacote usando o seguinte comando pip:

pip install -q -U google-genai

JavaScript

Usando Node.js v18+, instale o SDK de IA Generativa do Google para TypeScript e JavaScript usando o seguinte comando npm:

npm install @google/genai

Faça sua primeira solicitação

Confira um exemplo que usa a API Interactions para enviar uma solicitação à API Gemini usando o modelo Gemini 3 Flash.

Se você definir sua chave de API como a variável de ambiente GEMINI_API_KEY, ela será selecionada automaticamente pelo cliente ao usar as bibliotecas da API Gemini. Caso contrário, será necessário transmitir sua chave de API como um argumento ao inicializar o cliente.

Observe que todos os exemplos de código na documentação da API Gemini pressupõem que você definiu a variável de ambiente 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"
  }'

A seguir

Agora que você fez sua primeira solicitação de API, talvez queira conferir os seguintes guias que mostram o Gemini em ação: