0
# Client Initialization
1
2
The `GoogleGenAI` class is the main entry point for all SDK operations, providing unified access to both the Gemini Developer API and Vertex AI.
3
4
## Capabilities
5
6
### GoogleGenAI Constructor
7
8
Initialize a client instance for accessing Gemini models.
9
10
```typescript { .api }
11
/**
12
* Main client class for Google Gemini API and Vertex AI
13
* @param options - Configuration options
14
*/
15
class GoogleGenAI {
16
constructor(options?: GoogleGenAIOptions);
17
18
readonly models: Models;
19
readonly chats: Chats;
20
readonly live: Live;
21
readonly batches: Batches;
22
readonly caches: Caches;
23
readonly files: Files;
24
readonly operations: Operations;
25
readonly authTokens: Tokens;
26
readonly tunings: Tunings;
27
readonly fileSearchStores: FileSearchStores;
28
readonly vertexai: boolean;
29
}
30
31
interface GoogleGenAIOptions {
32
/** Use Vertex AI API (true) or Gemini API (false, default) */
33
vertexai?: boolean;
34
/** Google Cloud project ID (Vertex AI only, Node.js only) */
35
project?: string;
36
/** Google Cloud location (Vertex AI only, Node.js only) */
37
location?: string;
38
/** API Key (required for Gemini API, especially in browser) */
39
apiKey?: string;
40
/** API version to use */
41
apiVersion?: string;
42
/** Authentication options from google-auth-library (Node.js only) */
43
googleAuthOptions?: GoogleAuthOptions;
44
/** HTTP request customization */
45
httpOptions?: HttpOptions;
46
}
47
```
48
49
**Usage Examples:**
50
51
```typescript
52
import { GoogleGenAI } from '@google/genai';
53
54
// Gemini Developer API (with API key)
55
const client = new GoogleGenAI({
56
apiKey: 'YOUR_API_KEY'
57
});
58
59
// Vertex AI (Node.js with Google Cloud authentication)
60
const vertexClient = new GoogleGenAI({
61
vertexai: true,
62
project: 'my-project-id',
63
location: 'us-central1'
64
});
65
66
// With custom HTTP options
67
const customClient = new GoogleGenAI({
68
apiKey: 'YOUR_API_KEY',
69
httpOptions: {
70
timeout: 30000,
71
headers: {
72
'Custom-Header': 'value'
73
}
74
}
75
});
76
```
77
78
### Module Properties
79
80
The client exposes specialized modules for different operations.
81
82
```typescript { .api }
83
/** Content generation, image/video generation, embeddings, model management */
84
readonly models: Models;
85
86
/** Multi-turn conversation management */
87
readonly chats: Chats;
88
89
/** Real-time bidirectional communication (experimental) */
90
readonly live: Live;
91
92
/** Batch processing operations */
93
readonly batches: Batches;
94
95
/** Context caching for efficiency */
96
readonly caches: Caches;
97
98
/** File upload and management (Gemini API only) */
99
readonly files: Files;
100
101
/** Long-running operation management */
102
readonly operations: Operations;
103
104
/** Authentication token management (experimental) */
105
readonly authTokens: Tokens;
106
107
/** Model tuning operations (experimental) */
108
readonly tunings: Tunings;
109
110
/** File search store operations */
111
readonly fileSearchStores: FileSearchStores;
112
113
/** Whether using Vertex AI */
114
readonly vertexai: boolean;
115
```
116
117
## Types
118
119
```typescript { .api }
120
interface HttpOptions {
121
/** Custom base URL */
122
baseUrl?: string;
123
/** API version */
124
apiVersion?: string;
125
/** Custom headers */
126
headers?: Record<string, string>;
127
/** Request timeout in milliseconds */
128
timeout?: number;
129
/** Extra body parameters */
130
extraBody?: Record<string, unknown>;
131
}
132
133
interface GoogleAuthOptions {
134
// Options from google-auth-library package
135
// See: https://github.com/googleapis/google-auth-library-nodejs
136
}
137
```
138
139
## Platform-Specific Usage
140
141
### Browser Environment
142
143
In the browser, use API key authentication:
144
145
```typescript
146
import { GoogleGenAI } from '@google/genai/web';
147
148
const client = new GoogleGenAI({
149
apiKey: 'YOUR_API_KEY'
150
});
151
```
152
153
### Node.js Environment
154
155
In Node.js, you can use either API key or Google Cloud authentication:
156
157
```typescript
158
import { GoogleGenAI } from '@google/genai/node';
159
160
// With API key
161
const client = new GoogleGenAI({
162
apiKey: 'YOUR_API_KEY'
163
});
164
165
// With Vertex AI and Application Default Credentials
166
const vertexClient = new GoogleGenAI({
167
vertexai: true,
168
project: 'my-project-id',
169
location: 'us-central1'
170
});
171
```
172
173
## API Selection
174
175
The SDK supports two Google AI platforms:
176
177
- **Gemini Developer API**: Free tier available, API key authentication, suitable for development and small-scale applications
178
- **Vertex AI**: Enterprise-grade, Google Cloud project required, IAM authentication, suitable for production at scale
179
180
Set `vertexai: true` to use Vertex AI, or omit/set to `false` for Gemini Developer API.
181