Gemini Developer Quickstart
TypeScript (Node.js)
Author: MPS Prepared with GPT-5 Thinking
License: Creative Commons Attribution 4.0
A concise TS starter for Gemini with client init, text generation, structured output, and multimodal input.
Gemini Developer Quickstart TypeScript (Node.js) v1.0 2025-09-02 License: CC BY 4.0
Table of Contents
Table of Contents
1. Install & Setup
2. Initialize Client
3. Basic Text Generation
4. JSON/Structured Output (Pattern)
5. Vision Input (Multimodal)
6. Safety & Limits
Gemini Developer Quickstart TypeScript (Node.js) v1.0 2025-09-02 License: CC BY 4.0
1. Install & Setup
Install & Setup
Create a Node.js project and install the official client package for Gemini (TypeScript).
Store your API key in an environment variable (e.g., GEMINI_API_KEY); never hardcode secrets.
This guide uses a minimal example; confirm current package name, model IDs, and endpoints in
official docs.
Gemini Developer Quickstart TypeScript (Node.js) v1.0 2025-09-02 License: CC BY 4.0
2. Initialize Client
Initialize Client
// TypeScript (Node.js)
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro" }); // example model id
Gemini Developer Quickstart TypeScript (Node.js) v1.0 2025-09-02 License: CC BY 4.0
3. Basic Text Generation
Basic Text Generation
const prompt = "List 5 bullet points about product data governance.";
const result = await model.generateContent(prompt);
console.log(result.response.text());
Gemini Developer Quickstart TypeScript (Node.js) v1.0 2025-09-02 License: CC BY 4.0
4. JSON/Structured Output (Pattern)
JSON/Structured Output (Pattern)
Ask the model to return a structured object and validate its shape before using it in your app.
const schemaExample = `Respond with JSON: { "title": string, "bullets": string[] }`;
const r = await model.generateContent(schemaExample);
const text = r.response.text();
const data = JSON.parse(text); // add try/catch and schema validation
Gemini Developer Quickstart TypeScript (Node.js) v1.0 2025-09-02 License: CC BY 4.0
5. Vision Input (Multimodal)
Vision Input (Multimodal)
Send an image together with text instructions. Provide either a public URL or base64 content.
const imagePart = { inlineData: { data: "<base64>", mimeType: "image/png" } };
const r2 = await model.generateContent([
{ text: "Extract any printed price from this label." },
imagePart
]);
console.log(r2.response.text());
Gemini Developer Quickstart TypeScript (Node.js) v1.0 2025-09-02 License: CC BY 4.0
6. Safety & Limits
Safety & Limits
Set timeouts and retries; filter sensitive outputs; log request/response IDs; respect rate
limits and budgets.