The Client class is the primary entry point for interacting with the Gemini API and the Gemini Enterprise Agent Platform API. You can instantiate it using a builder pattern or by relying on environment variables.
Gemini Developer API
Use an API key to access the Gemini Developer backend:
Client client = Client.builder().apiKey("your-api-key").build();
Gemini Enterprise Agent Platform API
To use the Enterprise backend, you must set the enterprise(true) flag. You can authenticate using project/location details or an API key (Express Mode).
Using Project and Location:
Client client = Client.builder()
.project("your-project")
.location("your-location")
.enterprise(true)
.build();
Using API Key (Express Mode):
Client client = Client.builder()
.apiKey("your-api-key")
.enterprise(true)
.build();
Using Environment Variables
You can instantiate a client using new Client() if the following environment variables are set:
For Gemini Developer API:
GOOGLE_API_KEY: Recommended. (GEMINI_API_KEY is legacy and takes lower precedence).
For Gemini Enterprise Agent Platform:
GOOGLE_GENAI_USE_ENTERPRISE=trueGOOGLE_CLOUD_PROJECT: Your project ID.GOOGLE_CLOUD_LOCATION: Your location (e.g., us-central1).GOOGLE_API_KEY: For Express Mode.
import com.google.genai.Client;
// Gemini Developer API
Client client = Client.builder().apiKey("your-api-key").build();
// Gemini Enterprise Agent Platform
Client client = Client.builder()
.project("your-project")
.location("your-location")
.enterprise(true)
.build();