A Simple Guide to Calling an AI

Build smart apps that understand your data with the Google Gemini API.

What You'll Learn

This guide demonstrates how to connect to Google's Gemini AI, give it custom instructions (a "personality"), and provide it with specific data to generate personalized responses. We'll use climate and environmental data as our example to build a smart climate advisor.

How AI APIs Work: Beyond Simple Data

An API lets programs talk to each other. But an AI API is a leap forward in capability.

Standard API

You ask: "What's the temperature in New York?"

You get: A specific piece of data, like "72°F".

AI API

You ask: "Should I plant tomatoes this week in New York?"

You get: A reasoned recommendation based on multiple factors.

Part 1: Your First Gemini AI Call

Making a call to an AI involves sending your question to a specific URL. We'll use the gemini-2.5-flash-preview-05-20 model, which is great for fast, conversational tasks.

// Note: Replace YOUR_API_KEY with your actual key from ai.google.dev
const apiKey = "YOUR_API_KEY";
const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=${apiKey}`;

const payload = {
  contents: [{ parts: [{ text: 'What causes climate change?' }] }]
};

const response = await fetch(apiUrl, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify(payload)
});

const result = await response.json();
console.log(result.candidates[0].content.parts[0].text);

This gives a generic, encyclopedia-style answer. Now, let's make it smarter.

Part 2: Adding Custom Instructions

We can tell the AI *how* to behave using systemInstruction. This sets its personality and expertise.

const payload = {
  // This is the new part!
  systemInstruction: {
    parts: [{
      text: `You are a climate scientist. Always provide actionable advice.`
    }]
  },
  contents: [{ parts: [{ text: 'How can I reduce my carbon footprint?' }] }]
};

Now, the AI responds as a focused expert, not just a general assistant.

Part 3: Providing Your Specific Data

This is where the magic happens. We add facts about a user's situation directly into the instruction. The AI will use this "knowledge base" to tailor its answers.

const myClimateData = `
- City: Denver, Colorado
- Home Type: 1980s ranch, poor insulation
- Goals: Reduce energy costs by 40%
`;

const payload = {
  systemInstruction: {
    parts: [{
      text: `You are a personal climate advisor. Use the following data:
      USER'S SITUATION:
      ${myClimateData}`
    }]
  },
  contents: [{ parts: [{ text: 'What should I prioritize?' }] }]
};

With this context, the AI can move from generic to truly personal advice.

Part 4: Complete Interactive Example

This chat advisor uses all the concepts above. Enter your API key and ask it a question!

API Key Security

For this demo, we're putting the API key in the browser. In a real application, you should **never** expose your API key on the client-side. Keep it on a secure server.

🌱 Your Personal Climate Advisor

Profile: Denver, CO • 1980s Ranch • SUV Commuter

Hi! I have your profile for Denver. Ask me anything about reducing energy costs, preparing for wildfire season, or adapting to Colorado's changing climate. 🏔️