Skip to content

Support Application Default Credentials #289 #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/new-adc-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"@google/generative-ai": minor
---

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.

To use ADC:
1. Install the google-auth-library package
2. Initialize the SDK with `new GoogleGenerativeAI(undefined, { useAdc: true })`
3. Set up ADC through gcloud CLI or service account credentials

See documentation for more details.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ seamlessly across text, images, and code.
See the [Node.js quickstart](https://ai.google.dev/tutorials/node_quickstart)
for complete code.

### Using API Key Authentication

1. Install the SDK package

```js
npm install @google/generative-ai
```

1. Initialize the model
2. Initialize the model

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

1. Run a prompt
3. Run a prompt

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

### Using Application Default Credentials (ADC)

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:



1. Initialize with ADC

```js
const { GoogleGenerativeAI } = require("@google/generative-ai");

// Initialize with ADC (no API key needed)
const genAI = new GoogleGenerativeAI(undefined, { useAdc: true });

const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
```

3. Set up your credentials
- For local development: `gcloud auth application-default login`
- For GKE/GCE: Use service account credentials
- For more details, see [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials)

A complete sample is available in the [samples/adc_auth.js](./samples/adc_auth.js) file.

## Try out a sample app

This repository contains sample Node and web apps demonstrating how the SDK can
Expand Down
Loading