AI
AI Certified
Professional AI Certification
Get Started
6lesson.complete • 1/18
Courses›Google Gemini Pro›Gemini Fundamentals & API Setup
📖 Gemini Fundamentals & API Setup

Introduction to the Gemini API

⏱ 12 min·Lesson 1 of 18FREE

Introduction to the Gemini API

Overview

Google's Gemini API gives developers access to state-of-the-art multimodal AI models. Whether you're building a chatbot, a document analyser, or a complex reasoning pipeline, Gemini provides the foundation. This lesson walks you through obtaining API access, making your first call, and understanding the response structure.

Getting Your API Key

Navigate to Google AI Studio and sign in with your Google account. Click Get API Key → Create API Key. Store it securely:

bash
export GEMINI_API_KEY="AIza..."

Never commit API keys to source control. Use environment variables or a secrets manager in production.

Installing the SDK

Google provides official SDKs for multiple languages. For TypeScript/JavaScript:

bash
npm install @google/generative-ai

For Python:

bash
pip install google-generativeai

Your First Request

typescript
import { GoogleGenerativeAI } from '@google/generative-ai';

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);

async function main() {
  const model = genAI.getGenerativeModel({ model: 'gemini-2.5-flash' });

  const result = await model.generateContent(
    'Explain the difference between machine learning and deep learning.'
  );

  console.log(result.response.text());
}

main();

Understanding the Response Object

A Gemini API response includes:

  • candidates: Array of generated responses (usually one)
  • text(): Helper method to extract the text content
  • promptFeedback: Safety ratings for your input
  • usageMetadata: Token counts (promptTokenCount, candidatesTokenCount, totalTokenCount)

Python Equivalent

python
import google.generativeai as genai
import os

genai.configure(api_key=os.environ["GEMINI_API_KEY"])

model = genai.GenerativeModel("gemini-2.5-flash")
response = model.generate_content("What are the core principles of REST APIs?")
print(response.text)

Multi-Turn Conversations

Gemini supports chat sessions that maintain history:

typescript
const chat = model.startChat({
  history: [
    { role: 'user', parts: [{ text: 'Hello, I want to learn about TypeScript.' }] },
    { role: 'model', parts: [{ text: 'Great! TypeScript adds static types to JavaScript...' }] },
  ],
});

const response = await chat.sendMessage('What are generics?');
console.log(response.response.text());

Pro Tips

  • Use gemini-2.5-flash for development and cost-sensitive workloads
  • Use gemini-2.5-pro for tasks requiring deep reasoning
  • Always handle API errors gracefully — network issues and quota limits happen

Key Takeaways

  • The Gemini API is accessible via Google AI Studio (API key) or Vertex AI (GCP IAM)
  • Official SDKs handle authentication, retries, and response parsing
  • Chat sessions maintain conversation history automatically
  • Monitor token usage to control costs
🎓

Enjoyed this lesson?

Unlock all 18 lessons for a one-time payment.

Get Full Access — £29 →