To use Gemini on Vertex AI, you need to authenticate by using a Google Cloud API key or by using application default credentials. We recommend using an API key for testing and using application default credentials for production. This page shows you how to get a Google Cloud API key based on whether or not you already have a Google Cloud account.
Select whether you have a Google Cloud account:
Create a Google Cloud API key
If you already have a Google Cloud account, use the following instructions to get a standard Google Cloud API key. Alternatively, you can use application default credentials instead of using an API key.
Before you begin
Select a project, enable billing, enable the Vertex AI API
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Enable service account API key creation
- Open IAM & Admin > Organization policies.
- In the list of policies, filter for policies called iam.managed.disableServiceAccountApiKeyCreation.
- Click Actions > Edit policy.
- Under Policy source, select Override parent's policy, then click Add a rule.
- Under Enforcement, select Off.
- Click Done.
- Click Set policy. In the dialog that pops up, click Set policy again.
Create a new service account
- Open IAM & Admin > Service Accounts.
- Click Create service account.
- Configure the service account as follows:
- Service account name:
vertex-ai-runner
- Service account ID:
vertexairunner
- Service account name:
- Click Create and continue.
- Under Permissions, click Select a role and select Vertex AI Platform Express User from the menu.
- Click Continue.
- Click Done.
Create an API key
- Open APIs & Services > Credentials.
- Click Create credentials > API key.
- Configure the API key as follows:
- Name:
vertexaiapikey
- Authenticate API calls through a service account: Selected.
- Name:
- Click Select service account.
- Select the service account you created in the previous step and click Select.
- Click Create.
Make your first API request
After getting an API key, learn how to use your API key to make your first request in the API quickstart.
Optional: Set up your API key locally
For initial testing, you can hard code an API key, but this should only be temporary since it is not secure. The rest of this section goes through how to set up your API key locally as an environment variable with different operating systems.
Click to expand instructions
Linux/macOS
-
Run the following command to see which command-line shell you are using:
echo $SHELL
The output is similar to the following:
/bin/bash
-
Add a shell export variable for your API key, by doing one of the following:
-
If the output of the previous step is
/bin/bash
:-
Open
.bashrc
:touch ~/.bashrc open ~/.bashrc
-
Add the following line to
.bashrc
:export GEMINI_API_KEY=YOUR_API_KEY
-
Save the file, then run the following to apply the changes:
source ~/.bashrc
-
-
If the output of the previous step is
/bin/zsh
:-
Open
.zshrc
:touch ~/.zshrc open ~/.zshrc
-
Add the following line to
.zshrc
:export GEMINI_API_KEY= YOUR_API_KEY
-
Save the file, then run the following to apply the changes:
source ~/.zshrc
-
-
Windows
- Search for "Environment Variables" in the system settings
- Edit either "User variables" (for current user) or "System variables" (for all users - use with caution).
- Create the variable and add
export GEMINI_API_KEY=YOUR_API_KEY
- Apply the changes