Loading...
Loading...
Google Gemini API with @google/genai SDK. Use for multimodal AI, thinking mode, function calling, or encountering SDK deprecation warnings, context errors, multimodal format errors.
npx skill4agent add secondsky/claude-skills google-gemini-api@google/generative-ai@google/genai@google/generative-aireferences/sdk-migration-guide.mdbun add @google/genai@1.27.0bun add @google/generative-ai # DO NOT USE!export GEMINI_API_KEY="your-api-key"import { GoogleGenAI } from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'Explain quantum computing in simple terms'
});
console.log(response.text);templates/basic-usage.tsreferences/models-guide.mdconst response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'Write a haiku about programming'
});
console.log(response.text);const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'Explain AI',
generationConfig: {
temperature: 0.7, // 0.0-2.0, default 1.0
topP: 0.95, // 0.0-1.0
topK: 40, // 1-100
maxOutputTokens: 1024,
stopSequences: ['END']
}
});references/generation-config.mdconst stream = await ai.models.generateContentStream({
model: 'gemini-2.5-flash',
contents: 'Write a long story'
});
for await (const chunk of stream) {
process.stdout.write(chunk.text);
}references/streaming-patterns.mdconst imageData = Buffer.from(imageBytes).toString('base64');
const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: [
{ text: 'What is in this image?' },
{
inlineData: {
mimeType: 'image/jpeg', // or image/png, image/webp
data: imageData
}
}
]
});mimeTypevideo/mp4video/mpegvideo/movaudio/wavaudio/mp3audio/flacapplication/pdfreferences/multimodal-guide.mdconst response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'What is the weather in San Francisco?',
tools: [{
functionDeclarations: [{
name: 'getWeather',
description: 'Get current weather for a location',
parameters: {
type: 'object',
properties: {
location: { type: 'string', description: 'City name' },
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] }
},
required: ['location']
}
}]
}]
});
// Handle function call
const call = response.functionCalls?.[0];
if (call) {
const result = await getWeather(call.args);
// Send result back to model
const final = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: [
...response.contents,
{
functionResponse: {
name: call.name,
response: result
}
}
]
});
console.log(final.text);
}const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'What is the weather in SF and NY?',
tools: [{ functionDeclarations: [getWeatherDeclaration] }]
});
// Process all function calls in parallel
const results = await Promise.all(
response.functionCalls.map(call =>
getWeather(call.args).then(result => ({
name: call.name,
response: result
}))
)
);
// Send all results back
const final = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: [
...response.contents,
...results.map(r => ({ functionResponse: r }))
]
});references/function-calling-patterns.mdconst chat = ai.models.startChat({
model: 'gemini-2.5-flash',
systemInstruction: 'You are a helpful programming assistant',
history: []
});
let response = await chat.sendMessage('Hello!');
console.log(response.text);
response = await chat.sendMessage('Explain async/await');
console.log(response.text);
// Get full history
console.log(chat.getHistory());const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
systemInstruction: 'You are a pirate. Always respond in pirate speak.',
contents: 'What is the weather today?'
});const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'Solve this math problem: If x + 2y = 10 and 3x - y = 4, what is x?',
generationConfig: {
thinkingConfig: {
thinkingBudget: 8192 // Max tokens for internal reasoning
}
}
});references/thinking-mode-guide.md@google/genai@google/generative-ainpm uninstall @google/generative-ai
bun add @google/genai@1.27.0API key not validexport GEMINI_API_KEY="your-key"models/gemini-3.0-flash is not found'gemini-2.5-pro'
'gemini-2.5-flash'
'gemini-2.5-flash-lite'Request payload size exceeds the limitreferences/context-caching-guide.mdResource has been exhaustedasync function generateWithRetry(request, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await ai.models.generateContent(request);
} catch (error) {
if (error.status === 429 && i < maxRetries - 1) {
const delay = Math.pow(2, i) * 1000; // 1s, 2s, 4s
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
throw error;
}
}
}references/error-catalog.mdreferences/top-errors.mdreferences/sdk-migration-guide.md@google/generative-aireferences/models-guide.mdreferences/error-catalog.mdreferences/top-errors.mdreferences/context-caching-guide.mdreferences/code-execution-patterns.mdreferences/grounding-guide.mdreferences/streaming-patterns.mdreferences/function-calling-patterns.mdreferences/multimodal-guide.mdreferences/generation-config.mdreferences/thinking-mode-guide.mdtemplates/basic-usage.tsreferences/error-catalog.mdtop-errors.mdsdk-migration-guide.mdmodels-guide.mdcontext-caching-guide.mdcode-execution-patterns.mdgrounding-guide.mdstreaming-patterns.mdfunction-calling-patterns.mdmultimodal-guide.mdgeneration-config.mdthinking-mode-guide.md