Nadhebe
tutorials

Gemini API Examples for JavaScript & Node.js (2026 Developer Guide)

TypeScript and Node.js code examples using @google/genai for streaming chat sessions, structured Zod JSON outputs, and function calling.

Nadhebe Editorial Team Nadhebe Editorial Team
· · 2 min read
GPU Lab Verified
Node.js JavaScript SDK code blueprint illustration of Gemini API
On this page

Google’s @google/genai npm library brings native TypeScript support, automatic streaming iterators, and function calling capabilities to Node.js backends and web applications.


Installation

npm install @google/genai zod

1. Asynchronous Text Completion

import { GoogleGenAI } from '@google/genai';

const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

async function run() {
  const response = await ai.models.generateContent({
    model: 'gemini-2.0-flash',
    contents: 'Write a TypeScript interface for a user profile.',
  });

  console.log(response.text);
}

run();

2. Multi-Turn Conversational Chat

const chat = ai.chats.create({
  model: 'gemini-2.0-flash',
  config: {
    systemInstruction: 'You are a helpful senior TypeScript architect.',
  },
});

let response = await chat.sendMessage({ message: 'Hi, what is Astro SSR?' });
console.log(response.text);

response = await chat.sendMessage({ message: 'How do islands work in Astro?' });
console.log(response.text);

Multi-Turn Conversational Chat Session Diagram


3. Function Calling (Tool Integration)

Give Gemini the ability to execute JavaScript functions:

JavaScript Function Calling and Tool Execution Workflow

const fetchWeather = {
  name: 'getWeather',
  description: 'Get current weather temperature for a city',
  parameters: {
    type: 'OBJECT',
    properties: {
      location: { type: 'STRING' },
    },
    required: ['location'],
  },
};

const response = await ai.models.generateContent({
  model: 'gemini-2.0-flash',
  contents: 'What is the temperature in San Francisco?',
  config: {
    tools: [{ functionDeclarations: [fetchWeather] }],
  },
});

if (response.functionCalls) {
  console.log('Tool Invoked:', response.functionCalls[0]);
}

Summary

  • Use @google/genai for full TypeScript auto-completion.
  • Maintain chat history effortlessly with ai.chats.create().
Nadhebe Editorial Team

Nadhebe Editorial Team

The collective editorial desk, technical writers, and hardware validation engineers at Nadhebe. All publications undergo multi-stage peer review and physical GPU lab validation.

Includes Free AI Starter Kit

The Weekly AI Engineering Briefing

Join AI engineers building with Claude, MCP, Gemini, and open-source models. Received by developers, researchers, and technical founders.