API Formats

Google GenAI SDK

Guide to use Google GenAI SDK with cheapkeyai.com: generate content, streaming, chat, vision, function calling, embeddings, and image/TTS.

Google GenAI-compatible is suitable for Gemini SDK, Gemini CLI, and multimodal workflows requiring text, image, audio, or video in the same request.

Installation

npm
npm install @google/genai

Initialize Client

Client
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: process.env.CHEAPKEYAI_API_KEY,
  httpOptions: {
    baseUrl: "https://cheapkeyai.com"
  }
});

const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: "Write a pre-deployment testing checklist."
});

console.log(response.text);

Generate Content

System instruction
const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  config: {
    systemInstruction: "You are a technical assistant, reply in English."
  },
  contents: "Explain cache invalidation."
});
Streaming
const stream = await ai.models.generateContentStream({
  model: "gemini-2.5-flash",
  contents: "Write onboarding documentation for new developers."
});

for await (const chunk of stream) {
  if (chunk.text) process.stdout.write(chunk.text);
}

Multi-turn Chat

Chat session
const chat = ai.chats.create({ model: "gemini-2.5-flash" });

await chat.sendMessage({ message: "I am building docs." });
const answer = await chat.sendMessage({ message: "Suggest SEO structure?" });
console.log(answer.text);

Vision

Image URL
const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: [{
    role: "user",
    parts: [
      { text: "Read the content in the image." },
      { fileData: { fileUri: "https://example.com/image.png", mimeType: "image/png" } }
    ]
  }]
});

Function Calling

Declare functions in `config.tools` to let the model select functions, then execute them in your app and return results to the conversation. Always validate parameters for critical tools.

Commonly Used Endpoints

FeatureUsed ForNotes
models.generateContentText, single-turn chat, multimodalEasiest to integrate.
models.generateContentStreamReturn partial resultsSuitable for real-time UI.
chats.createMulti-turn conversationSDK maintains history in the session.
embeddingsSearch/RAGNormalize input before indexing.
image/TTSImage and voiceCheck supported models and formats.