Skip to content

Commit 29ccf06

Browse files
thebongyRishit Bansal
andauthored
[AI Gateway]: Add documentation for service account based auth and unified API on Vertex (#25561)
Co-authored-by: Rishit Bansal <[email protected]>
1 parent f7998f0 commit 29ccf06

File tree

1 file changed

+118
-1
lines changed
  • src/content/docs/ai-gateway/usage/providers

1 file changed

+118
-1
lines changed

src/content/docs/ai-gateway/usage/providers/vertex.mdx

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,123 @@ Then you can append the endpoint you want to hit, for example: `/publishers/goog
4343

4444
So your final URL will come together as: `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-vertex-ai/v1/projects/{project_name}/locations/{region}/publishers/google/models/gemini-1.0-pro-001:generateContent`
4545

46-
## Example
46+
## Authenticating with Vertex AI
47+
48+
Authenticating with Vertex AI normally requires generating short-term credentials using the [Google Cloud SDKs](https://cloud.google.com/vertex-ai/docs/authentication) with a complicated setup, but AI Gateway simplifies this for you with multiple options:
49+
50+
### Option 1: Service Account JSON
51+
52+
AI Gateway supports passing a Google service account JSON directly in the `Authorization` header on requests or through AI Gateway's [Bring Your Own Keys](/ai-gateway/configuration/bring-your-own-keys/) feature.
53+
54+
You can [create a service account key](https://cloud.google.com/iam/docs/keys-create-delete) in the Google Cloud Console. Ensure that the service account has the required permissions for the Vertex AI endpoints and models you plan to use.
55+
56+
AI Gateway uses your service account JSON to generate short-term access tokens which are cached and used for consecutive requests, and are automatically refreshed when they expire.
57+
58+
:::note
59+
The service account JSON must include an additional key called `region` with the GCP region code (for example, `us-east1`) you intend to use for your [Vertex AI endpoint](https://cloud.google.com/vertex-ai/docs/reference/rest#service-endpoint). You can also pass the region code `global` to use the global endpoint.
60+
:::
61+
62+
#### Example service account JSON structure
63+
64+
```json
65+
{
66+
"type": "service_account",
67+
"project_id": "your-project-id",
68+
"private_key_id": "your-private-key-id",
69+
"private_key": "-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY\n-----END PRIVATE KEY-----\n",
70+
"client_email": "[email protected]",
71+
"client_id": "your-client-id",
72+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
73+
"token_uri": "https://oauth2.googleapis.com/token",
74+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
75+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/your-service-account%40your-project.iam.gserviceaccount.com",
76+
"region": "us-east1"
77+
}
78+
```
79+
80+
You can pass this JSON in the `Authorization` header or configure it in [Bring Your Own Keys](/ai-gateway/configuration/bring-your-own-keys/).
81+
82+
### Option 2: Direct Access Token
83+
84+
If you are already using the Google Cloud SDKs and generating a short-term access token (for example, with `gcloud auth print-access-token`), you can directly pass this as a Bearer token in the `Authorization` header of the request.
85+
86+
:::note
87+
This option is only supported for the provider-specific endpoint, not for the unified chat completions endpoint.
88+
:::
89+
90+
```bash
91+
curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-vertex-ai/v1/projects/{project_name}/locations/{region}/publishers/google/models/gemini-1.0-pro-001:generateContent" \
92+
-H "Authorization: Bearer ya29.c.b0Aaekm1K..." \
93+
-H 'Content-Type: application/json' \
94+
-d '{
95+
"contents": {
96+
"role": "user",
97+
"parts": [
98+
{
99+
"text": "Tell me more about Cloudflare"
100+
}
101+
]
102+
}
103+
}'
104+
```
105+
106+
## Using Unified Chat Completions API
107+
108+
AI Gateway provides a [Unified API](/ai-gateway/usage/chat-completion/) that works across providers. For Google Vertex AI, you can use the standard chat completions format. Note that the model field includes the provider prefix, so your model string will look like `google-vertex-ai/google/gemini-2.5-pro`.
109+
110+
### Endpoint
111+
112+
```txt
113+
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions
114+
```
115+
116+
### Example with OpenAI SDK
117+
118+
```javascript
119+
import OpenAI from 'openai';
120+
121+
const client = new OpenAI({
122+
apiKey: '{service_account_json}',
123+
baseURL: 'https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat'
124+
});
125+
126+
const response = await client.chat.completions.create({
127+
model: 'google-vertex-ai/google/gemini-2.5-pro',
128+
messages: [
129+
{
130+
role: 'user',
131+
content: 'What is Cloudflare?'
132+
}
133+
]
134+
});
135+
136+
console.log(response.choices[0].message.content);
137+
```
138+
139+
### Example with cURL
140+
141+
```bash
142+
curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions" \
143+
-H "Authorization: Bearer {service_account_json}" \
144+
-H 'Content-Type: application/json' \
145+
-d '{
146+
"model": "google-vertex-ai/google/gemini-2.5-pro",
147+
"messages": [
148+
{
149+
"role": "user",
150+
"content": "What is Cloudflare?"
151+
}
152+
]
153+
}'
154+
```
155+
156+
:::note
157+
See the [Authenticating with Vertex AI](#authenticating-with-vertex-ai) section below for details on the service account JSON structure and authentication options.
158+
:::
159+
160+
## Using Provider-Specific Endpoint
161+
162+
You can also use the provider-specific endpoint to access the full Vertex AI API.
47163

48164
### cURL
49165

@@ -62,3 +178,4 @@ curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-vert
62178
}'
63179

64180
```
181+

0 commit comments

Comments
 (0)