Skip to content

Commit 160a0c3

Browse files
author
Prabhjit Singh Dhillon
committed
Add support for Application Default Credentials (ADC)
1 parent 705d448 commit 160a0c3

File tree

11 files changed

+793
-45
lines changed

11 files changed

+793
-45
lines changed

.changeset/new-adc-support.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@google/generative-ai": minor
3+
---
4+
5+
Added support for Application Default Credentials (ADC). This enables more secure authentication when running on Google Cloud environments like GKE or GCE, without the need to manage API keys.
6+
7+
To use ADC:
8+
1. Install the google-auth-library package
9+
2. Initialize the SDK with `new GoogleGenerativeAI(undefined, { useAdc: true })`
10+
3. Set up ADC through gcloud CLI or service account credentials
11+
12+
See documentation for more details.

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ seamlessly across text, images, and code.
2828
See the [Node.js quickstart](https://ai.google.dev/tutorials/node_quickstart)
2929
for complete code.
3030

31+
### Using API Key Authentication
32+
3133
1. Install the SDK package
3234

3335
```js
3436
npm install @google/generative-ai
3537
```
3638

37-
1. Initialize the model
39+
2. Initialize the model
3840

3941
```js
4042
const { GoogleGenerativeAI } = require("@google/generative-ai");
@@ -44,7 +46,7 @@ const genAI = new GoogleGenerativeAI(process.env.API_KEY);
4446
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
4547
```
4648

47-
1. Run a prompt
49+
3. Run a prompt
4850

4951
```js
5052
const prompt = "Does this look store-bought or homemade?";
@@ -59,6 +61,30 @@ const result = await model.generateContent([prompt, image]);
5961
console.log(result.response.text());
6062
```
6163

64+
### Using Application Default Credentials (ADC)
65+
66+
For improved security when running on Google Cloud environments (GKE, GCE, etc.) or for local development, you can use Application Default Credentials (ADC) instead of API keys:
67+
68+
69+
70+
1. Initialize with ADC
71+
72+
```js
73+
const { GoogleGenerativeAI } = require("@google/generative-ai");
74+
75+
// Initialize with ADC (no API key needed)
76+
const genAI = new GoogleGenerativeAI(undefined, { useAdc: true });
77+
78+
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
79+
```
80+
81+
3. Set up your credentials
82+
- For local development: `gcloud auth application-default login`
83+
- For GKE/GCE: Use service account credentials
84+
- For more details, see [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials)
85+
86+
A complete sample is available in the [samples/adc_auth.js](./samples/adc_auth.js) file.
87+
6288
## Try out a sample app
6389

6490
This repository contains sample Node and web apps demonstrating how the SDK can

0 commit comments

Comments
 (0)