To use Gemini on Vertex AI, authenticate by using a Google Cloud API key or 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 you're a new or existing Google Cloud user.
Select your user type:
Create a Google Cloud API key
If you already are a standard Google Cloud user with billing enabled, 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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles.
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 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 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 API_KEY=YOUR_API_KEY
- Apply the changes