Google Gen AI JavaScript SDK for building applications powered by Gemini with content generation, image/video generation, function calling, caching, and real-time live sessions
The GoogleGenAI class is the main entry point for all SDK operations, providing unified access to both the Gemini Developer API and Vertex AI.
Initialize a client instance for accessing Gemini models.
/**
* Main client class for Google Gemini API and Vertex AI
* @param options - Configuration options
*/
class GoogleGenAI {
constructor(options?: GoogleGenAIOptions);
readonly models: Models;
readonly chats: Chats;
readonly live: Live;
readonly batches: Batches;
readonly caches: Caches;
readonly files: Files;
readonly operations: Operations;
readonly authTokens: Tokens;
readonly tunings: Tunings;
readonly fileSearchStores: FileSearchStores;
readonly vertexai: boolean;
}
interface GoogleGenAIOptions {
/** Use Vertex AI API (true) or Gemini API (false, default) */
vertexai?: boolean;
/** Google Cloud project ID (Vertex AI only, Node.js only) */
project?: string;
/** Google Cloud location (Vertex AI only, Node.js only) */
location?: string;
/** API Key (required for Gemini API, especially in browser) */
apiKey?: string;
/** API version to use */
apiVersion?: string;
/** Authentication options from google-auth-library (Node.js only) */
googleAuthOptions?: GoogleAuthOptions;
/** HTTP request customization */
httpOptions?: HttpOptions;
}Usage Examples:
import { GoogleGenAI } from '@google/genai';
// Gemini Developer API (with API key)
const client = new GoogleGenAI({
apiKey: 'YOUR_API_KEY'
});
// Vertex AI (Node.js with Google Cloud authentication)
const vertexClient = new GoogleGenAI({
vertexai: true,
project: 'my-project-id',
location: 'us-central1'
});
// With custom HTTP options
const customClient = new GoogleGenAI({
apiKey: 'YOUR_API_KEY',
httpOptions: {
timeout: 30000,
headers: {
'Custom-Header': 'value'
}
}
});The client exposes specialized modules for different operations.
/** Content generation, image/video generation, embeddings, model management */
readonly models: Models;
/** Multi-turn conversation management */
readonly chats: Chats;
/** Real-time bidirectional communication (experimental) */
readonly live: Live;
/** Batch processing operations */
readonly batches: Batches;
/** Context caching for efficiency */
readonly caches: Caches;
/** File upload and management (Gemini API only) */
readonly files: Files;
/** Long-running operation management */
readonly operations: Operations;
/** Authentication token management (experimental) */
readonly authTokens: Tokens;
/** Model tuning operations (experimental) */
readonly tunings: Tunings;
/** File search store operations */
readonly fileSearchStores: FileSearchStores;
/** Whether using Vertex AI */
readonly vertexai: boolean;interface HttpOptions {
/** Custom base URL */
baseUrl?: string;
/** API version */
apiVersion?: string;
/** Custom headers */
headers?: Record<string, string>;
/** Request timeout in milliseconds */
timeout?: number;
/** Extra body parameters */
extraBody?: Record<string, unknown>;
}
interface GoogleAuthOptions {
// Options from google-auth-library package
// See: https://github.com/googleapis/google-auth-library-nodejs
}In the browser, use API key authentication:
import { GoogleGenAI } from '@google/genai/web';
const client = new GoogleGenAI({
apiKey: 'YOUR_API_KEY'
});In Node.js, you can use either API key or Google Cloud authentication:
import { GoogleGenAI } from '@google/genai/node';
// With API key
const client = new GoogleGenAI({
apiKey: 'YOUR_API_KEY'
});
// With Vertex AI and Application Default Credentials
const vertexClient = new GoogleGenAI({
vertexai: true,
project: 'my-project-id',
location: 'us-central1'
});The SDK supports two Google AI platforms:
Set vertexai: true to use Vertex AI, or omit/set to false for Gemini Developer API.
Install with Tessl CLI
npx tessl i tessl/npm-google--genai