C.W.K.
Stream
Lesson 01 of 04 · published

Installation & GoogleGenAI

~10 min · typescript, node, google-genai

Level 0Spark
0 XP0/35 lessons0/10 achievements
0/140 XP to next level140 XP to go0% complete

The package you want is @google/genai

The official TypeScript/JavaScript SDK is @google/genai (note the slash — npm scoped package). It replaces @google/generative-ai, which reached EOL in August 2025. Requires Node.js ≥ 20.

One class to construct

The entry point is GoogleGenAI. Three construction patterns mirror the Python SDK:

  • new GoogleGenAI({ apiKey: '...' }) — explicit key.
  • new GoogleGenAI({}) — reads GOOGLE_API_KEY from env (Node.js).
  • new GoogleGenAI({ vertexai: true, project: '...', location: '...' }) — Vertex AI.

Submodules

The constructed ai object exposes the same surfaces as the Python client:

  • ai.models — generate, stream, count tokens.
  • ai.chats — multi-turn chat.
  • ai.files — File API uploads.
  • ai.caches — context caching.
  • ai.live — live multimodal sessions.

Helper imports you'll use a lot

The package also exports schema helpers and content factories:

  • Type — schema type enum (Type.STRING, Type.OBJECT, etc.).
  • createUserContent, createPartFromUri — multimodal content builders.
  • FunctionCallingConfigModeAUTO, ANY, NONE.
  • ApiError — the exception class.

Code

Install·bash
npm install @google/genai
# or
pnpm add @google/genai
# or
bun add @google/genai
Construction patterns·typescript
import {
  GoogleGenAI,
  Type,
  ApiError,
  FunctionCallingConfigMode,
  ThinkingLevel,
  createUserContent,
  createPartFromUri,
} from '@google/genai';

// 1. Explicit key
const ai = new GoogleGenAI({ apiKey: 'AIzaSy...' });

// 2. From env (set GOOGLE_API_KEY first)
const ai = new GoogleGenAI({});

// 3. Vertex AI
const ai = new GoogleGenAI({
  vertexai: true,
  project: 'my-gcp-project',
  location: 'us-central1',
});

// 4. Pin a specific API version
const ai = new GoogleGenAI({ apiKey: 'KEY', apiVersion: 'v1' });
First call·typescript
const response = await ai.models.generateContent({
  model: 'gemini-2.5-flash',
  contents: 'Hello, Gemini!',
});
console.log(response.text);  // property, not method

External links

Exercise

Spin up a fresh Node project (npm init -y), install @google/genai, and write a small hello.mjs that constructs a client from env and prints a response from Flash. Confirm Node ≥ 20. Bonus: try the same code in a Bun project — it should work unchanged.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.