---
title: Optimize Cloud Costs with Vertex AI & PalM 2 API
description: "Solved: Secure API keys for Vertex AI or PalM 2 hassle-free. Discover effective authentication methods."
image: https://www.c2cglobal.com/hubfs/Imported_Blog_Media/General%20Article%20Graphic-Mar-21-2024-09-03-58-1903-PM-Mar-26-2024-08-35-30-5070-PM.jpg
---

[![c2c logo blue](https://www.c2cglobal.com/hubfs/c2c-logo-blue.svg "c2c logo blue")](https://www.c2cglobal.com/)

[AI and Machine Learning](https://www.c2cglobal.com/articles/tag/ai-and-machine-learning), [Vertex AI](https://www.c2cglobal.com/articles/tag/vertex-ai), [API Management](https://www.c2cglobal.com/articles/tag/api-management)

# How to get API keys to use with Vertex AI or PalM 2 API

By [Petar Ojdrovic](https://www.c2cglobal.com/articles/author/petar-ojdrovic) | August 12, 2021

Hey community! I am looking into replacing some of our OpenAI usage with Vertex / PalM 2 and am wondering how to get the necessary API and use them in production. We would be using these APIs in cloud functions.

 

gcloud auth print-access-token does not work in cloud functions.

I've used service account credentials to initialize clients for other APIs like translate, bigquery, etc., but this endpoint does not accept a service account credential.

Should I use the runtimes service account and get an access token that way? 

```
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" -H "Metadata-Flavor: Google" | jq -r .access_token
```

 

Here's the sample request found in the Vertex AI playground 

```
API_ENDPOINT="us-central1-aiplatform.googleapis.com"PROJECT_ID="home-service-62a33"MODEL_ID="text-bison@001"curl \-X POST \-H "Authorization: Bearer $(gcloud auth print-access-token)" \-H "Content-Type: application/json" \"https://${API_ENDPOINT}/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${MODEL_ID}:predict" -d \$'{  "instances": [    {      "content": ""    }  ],  "parameters": {    "temperature": 0.2,    "maxOutputTokens": 256,    "topP": 0.8,    "topK": 40  }}'
```

 

More broadly - how should we authenticate these API's for use in a cloud function?

 

Thanks in advance!

 

**Best answer by pthiagar**

Generally, once the service account policy binding is setup for the function, the most reliable way to generate tokens would be to do so programmatically using the authentication libraries. 

But, if you have to do this manually, there are many ways to do this too.  For your e.g. you should be able to use Compute metadata server to get id token for a specific audience like this-

curl "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=*AUDIENCE*" \\

     -H "Metadata-Flavor: Google"

Where audience is the URL of the invoked function.  You can then pass the google signed id token in the Authorization Bearer header to the request.  

See docs for more info - [authentication options for invocation](https://cloud.google.com/functions/docs/securing/authenticating).

Hope this helps!

---

## Recent Articles

<https://www.c2cglobal.com/articles/generative-ai-are-you-behind>

[Data Analytics](https://www.c2cglobal.com/articles/tag/data-analytics)

## [Generative AI: Are You Behind?!](https://www.c2cglobal.com/articles/generative-ai-are-you-behind)

Review the latest insights from the AI Readiness Report.

By Bruno Aziza

<https://www.c2cglobal.com/articles/make-gen-ai-work-landscape-slms-vs.-llms-cost-more>

[Industry Solutions](https://www.c2cglobal.com/articles/tag/industry-solutions)

## [Make "Gen AI Work": Landscape, SLMs vs. LLMs, Cost & More...](https://www.c2cglobal.com/articles/make-gen-ai-work-landscape-slms-vs.-llms-cost-more)

Discover the 5 metrics you need to know in order to be an exceptional CEO and Operator.

By Bruno Aziza

<https://www.c2cglobal.com/articles/ai-cheat-sheet>

[Google Cloud Strategy](https://www.c2cglobal.com/articles/tag/google-cloud-strategy)

## [AI Cheat Sheet](https://www.c2cglobal.com/articles/ai-cheat-sheet)

AI is no more and no less the drive to create robots with human minds so they can do everything we do and more. Use this cheat sheet to help decode the space.

By Leah Zitter

## Subscribe to our newsletter

Get the latest from C2C, direct to your inbox.

[Subscribe](https://share.hsforms.com/1znKGo6ySS9uaRyvLQQzxRw4qbyr)

![c2c-logo-white-tagline](https://www.c2cglobal.com/hubfs/c2c-logo-white-tagline.svg "c2c-logo-white-tagline")

C2C is the only peer-to-peer community built to support, connect, and educate Google Cloud customers across regions and industries so they can better harness the power of the cloud—and each other—to solve their biggest challenges and drive innovation.

©2024  C2C Global. All rights reserved. [Privacy Policy](https://www.c2cglobal.com/privacy)

20 N Wacker Drive • Suite 1810 • Chicago, IL • 60606

**Join over 60K members on C2C.**

Let's connect!

- [mailto:hello@c2cglobal.com](mailto:hello@c2cglobal.com)
- <https://m.youtube.com/channel/UCWOli-_qQyZziBdPQ-6p-eQ>
- <https://www.linkedin.com/company/66934702>
- [![X](https://www.c2cglobal.com/hubfs/twitter-x-icon-white.svg)](https://twitter.com/MeetC2C)

![](https://px.ads.linkedin.com/collect/?pid=2634489&fmt=gif)