Esta guía te ayudará a comenzar a usar la API heredada de generateContent. Para proyectos y aplicaciones nuevos, te recomendamos usar la nueva API de Interactions, que es la forma más sencilla y eficaz de compilar con modelos y agentes de Gemini.
En esta guía de inicio rápido, se muestra cómo instalar nuestras
bibliotecas y realizar tu primera solicitud, transmitir
respuestas, crear conversaciones de varios turnos y usar herramientas con el método estándar
generateContent.
Obtén una clave de API
Para usar la API de Gemini, debes tener una clave de API para autenticar tus solicitudes, aplicar límites de seguridad y hacer un seguimiento del uso de tu cuenta.
- Google AI Studio crea automáticamente un proyecto y una clave de API para los usuarios nuevos. Puedes copiarla desde la página Claves de API.
- Si necesitas una clave nueva, haz clic en Crear clave de API en AI Studio y sigue el diálogo para agregar un nuevo par de clave y proyecto.
Crear una clave de API de Gemini
Configura tu clave como una variable de entorno:
export GEMINI_API_KEY="YOUR_API_KEY"
Actualiza al nivel pagado
Si actualizas al nivel pagado, aumentan tus límites de frecuencia y se requiere configurar Facturación de Cloud.
- Haz clic en Configurar facturación en las páginas Claves de API o Proyectos de AI Studio.
- Sigue el diálogo de Facturación de Cloud para crear o vincular una cuenta de facturación, agregar una forma de pago y pagar por adelantado un mínimo de USD 10 (o su equivalente en moneda) en créditos pagados.
- Consulta el uso de la API en Google AI Studio en Panel > Uso.
Consulta la página Facturación para obtener más información.
Instala el SDK de Google GenAI
Python
Con Python 3.9 o versiones posteriores, instala el
google-genai paquete
con el siguiente
comando pip:
pip install -q -U google-genai
JavaScript
Con Node.js v18+, instala el SDK de IA generativa de Google para TypeScript y JavaScript con el siguiente comando npm:
npm install @google/genai
Generar texto
Usa el método models.generate_content para
generar una respuesta de texto.
Python
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemini-3.6-flash",
contents="Explain how AI works in a few words"
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const response = await ai.models.generateContent({
model: "gemini-3.6-flash",
contents: "Explain how AI works in a few words",
});
console.log(response.text);
}
main();
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.6-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in a few words"
}
]
}
]
}'
Cómo mostrar las respuestas en tiempo real
De forma predeterminada, el modelo muestra una respuesta solo después de que se completa todo el proceso de generación. Para una experiencia más rápida e interactiva, puedes transmitir los fragmentos de respuesta a medida que se generan.
Python
response = client.models.generate_content_stream(
model="gemini-3.6-flash",
contents="Explain how AI works in detail"
)
for chunk in response:
print(chunk.text, end="", flush=True)
JavaScript
async function main() {
const responseStream = await ai.models.generateContentStream({
model: "gemini-3.6-flash",
contents: "Explain how AI works in detail",
});
for await (const chunk of responseStream) {
process.stdout.write(chunk.text);
}
}
main();
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.6-flash:streamGenerateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
--no-buffer \
-X POST \
-d '{
"contents": [
{
"parts": [
{
"text": "Explain how AI works in detail"
}
]
}
]
}'
Conversaciones de varios turnos
Para las conversaciones de varios turnos, los SDKs proporcionan un auxiliar chats con estado para
crear una experiencia de chat de varios turnos
que administra automáticamente el historial de conversaciones.
Python
chat = client.chats.create(model="gemini-3.6-flash")
response1 = chat.send_message("I have 2 dogs in my house.")
print("Response 1:", response1.text)
response2 = chat.send_message("How many paws are in my house?")
print("Response 2:", response2.text)
JavaScript
async function main() {
const chat = ai.chats.create({ model: "gemini-3.6-flash" });
let response = await chat.sendMessage({ message: "I have 2 dogs in my house." });
console.log("Response 1:", response.text);
response = await chat.sendMessage({ message: "How many paws are in my house?" });
console.log("Response 2:", response.text);
}
main();
REST
# REST is stateless. You must pass the full conversation history in the request.
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.6-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"role": "user",
"parts": [{"text": "I have 2 dogs in my house."}]
},
{
"role": "model",
"parts": [{"text": "That is nice! Two dogs mean you have plenty of company."}]
},
{
"role": "user",
"parts": [{"text": "How many paws are in my house?"}]
}
]
}'
Usar herramientas
Extiende las capacidades del modelo fundamentando las respuestas con la Búsqueda de Google para acceder a contenido web en tiempo real. El modelo decide automáticamente cuándo buscar, ejecuta consultas y sintetiza una respuesta.
Python
from google import genai
from google.genai import types
config = types.GenerateContentConfig(
tools=[types.Tool(google_search=types.GoogleSearch())]
)
response = client.models.generate_content(
model="gemini-3.6-flash",
contents="Who won the euro 2024?",
config=config
)
print(response.text)
metadata = response.candidates[0].grounding_metadata
if metadata.web_search_queries:
print("\nSearch queries executed:")
for query in metadata.web_search_queries:
print(f" - {query}")
if metadata.grounding_chunks:
print("\nSources:")
for chunk in metadata.grounding_chunks:
print(f" - [{chunk.web.title}]({chunk.web.uri})")
JavaScript
async function main() {
const response = await ai.models.generateContent({
model: "gemini-3.6-flash",
contents: "Who won the euro 2024?",
config: {
tools: [{ googleSearch: {} }]
}
});
console.log(response.text);
const metadata = response.candidates[0]?.groundingMetadata;
if (metadata?.webSearchQueries) {
console.log("\nSearch queries executed:");
for (const query of metadata.webSearchQueries) {
console.log(` - ${query}`);
}
}
if (metadata?.groundingChunks) {
console.log("\nSources:");
for (const chunk of metadata.groundingChunks) {
console.log(` - [${chunk.web.title}](${chunk.web.uri})`);
}
}
}
main();
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.6-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"contents": [
{
"parts": [
{"text": "Who won the euro 2024?"}
]
}
],
"tools": [
{
"google_search": {}
}
]
}'
La API de Gemini también admite otras herramientas integradas:
- Ejecución de código: Permite que el modelo escriba y ejecute código de Python para resolver problemas matemáticos complejos.
- Contexto de URL: Te permite fundamentar las respuestas en URLs de páginas web específicas que proporciones.
- Búsqueda de archivos: Te permite subir archivos y fundamentar las respuestas en su contenido mediante la búsqueda semántica.
- Google Maps: Te permite fundamentar las respuestas en datos de ubicación y buscar lugares, instrucciones y mapas.
- Uso de la computadora: Permite que el modelo interactúe con una pantalla, un teclado y un mouse virtuales de la computadora para realizar tareas.
Llamar a funciones personalizadas
Usa llamada a función para conectar
modelos a tus herramientas y APIs personalizadas. El modelo determina cuándo llamar a tu función y muestra un functionCall en la respuesta para que la ejecute tu aplicación.
En este ejemplo, se declara una función de temperatura simulada y se verifica si el modelo quiere llamarla.
Python
from google import genai
from google.genai import types
weather_function = {
"name": "get_current_temperature",
"description": "Gets the current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g. San Francisco",
},
},
"required": ["location"],
},
}
tools = types.Tool(function_declarations=[weather_function])
config = types.GenerateContentConfig(tools=[tools])
contents = ["What's the temperature in London?"]
response = client.models.generate_content(
model="gemini-3.6-flash",
contents=contents,
config=config,
)
part = response.candidates[0].content.parts[0]
if part.function_call:
fc = part.function_call
print(f"Model requested function: {fc.name} with args {fc.args}")
mock_result = {"temperature": "15C", "condition": "Cloudy"}
contents.append(response.candidates[0].content)
fn_response_part = types.Part.from_function_response(
name=fc.name,
response=mock_result,
id=fc.id
)
contents.append(types.Content(role="user", parts=[fn_response_part]))
final_response = client.models.generate_content(
model="gemini-3.6-flash",
contents=contents,
config=config,
)
print("Final Response:", final_response.text)
JavaScript
import { GoogleGenAI, Type } from '@google/genai';
async function main() {
const weatherFunction = {
name: 'get_current_temperature',
description: 'Gets the current temperature for a given location.',
parameters: {
type: Type.OBJECT,
properties: {
location: {
type: Type.STRING,
description: 'The city name, e.g. San Francisco',
},
},
required: ['location'],
},
};
const contents = [{
role: 'user',
parts: [{ text: "What's the temperature in London?" }]
}];
const response = await ai.models.generateContent({
model: 'gemini-3.6-flash',
contents: contents,
config: {
tools: [{ functionDeclarations: [weatherFunction] }],
},
});
if (response.functionCalls && response.functionCalls.length > 0) {
const fc = response.functionCalls[0];
console.log(`Model requested function: ${fc.name}`);
const mockResult = { temperature: "15C", condition: "Cloudy" };
contents.push(response.candidates[0].content);
contents.push({
role: 'user',
parts: [{
functionResponse: {
name: fc.name,
response: mockResult,
id: fc.id
}
}]
});
const finalResponse = await ai.models.generateContent({
model: 'gemini-3.6-flash',
contents: contents,
config: {
tools: [{ functionDeclarations: [weatherFunction] }],
},
});
console.log("Final Response:", finalResponse.text);
}
}
main();
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.6-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [
{
"role": "user",
"parts": [{"text": "What'\''s the temperature in London?"}]
}
],
"tools": [
{
"functionDeclarations": [
{
"name": "get_current_temperature",
"description": "Gets the current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g. San Francisco"
}
},
"required": ["location"]
}
}
]
}
]
}'
¿Qué sigue?
Ahora que ya comenzaste a usar la API de Gemini, explora las siguientes guías para compilar aplicaciones más avanzadas: