Skip to content

Commit da435ac

Browse files
author
Rishit Bansal
committed
[AI Gateway]: Add documentation for service account based auth and unified API on Vertex
1 parent 2294143 commit da435ac

File tree

1 file changed

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

1 file changed

+120
-1
lines changed

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

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,59 @@ 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+
## Using Unified Chat Completions API
47+
48+
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`.
49+
50+
### Endpoint
51+
52+
```txt
53+
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions
54+
```
55+
56+
### Example with OpenAI SDK
57+
58+
```javascript
59+
import OpenAI from 'openai';
60+
61+
const client = new OpenAI({
62+
apiKey: '{service_account_json}',
63+
baseURL: 'https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat'
64+
});
65+
66+
const response = await client.chat.completions.create({
67+
model: 'google-vertex-ai/google/gemini-2.5-pro',
68+
messages: [
69+
{
70+
role: 'user',
71+
content: 'What is Cloudflare?'
72+
}
73+
]
74+
});
75+
76+
console.log(response.choices[0].message.content);
77+
```
78+
79+
### Example with cURL
80+
81+
```bash
82+
curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions" \
83+
-H "Authorization: Bearer {service_account_json}" \
84+
-H 'Content-Type: application/json' \
85+
-d '{
86+
"model": "google-vertex-ai/google/gemini-2.5-pro",
87+
"messages": [
88+
{
89+
"role": "user",
90+
"content": "What is Cloudflare?"
91+
}
92+
]
93+
}'
94+
```
95+
96+
## Using Provider-Specific Endpoint
97+
98+
You can also use the provider-specific endpoint to access the full Vertex AI API.
4799

48100
### cURL
49101

@@ -62,3 +114,70 @@ curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-vert
62114
}'
63115

64116
```
117+
118+
## Authenticating with Vertex AI
119+
120+
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:
121+
122+
### Option 1: Service Account JSON
123+
124+
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.
125+
126+
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.
127+
128+
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.
129+
130+
:::note
131+
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.
132+
:::
133+
134+
```bash
135+
curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions" \
136+
-H "Authorization: Bearer $(echo '{
137+
"type": "service_account",
138+
"project_id": "your-project-id",
139+
"private_key_id": "your-private-key-id",
140+
"private_key": "-----BEGIN PRIVATE KEY-----\nYOUR_PRIVATE_KEY\n-----END PRIVATE KEY-----\n",
141+
"client_email": "[email protected]",
142+
"client_id": "your-client-id",
143+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
144+
"token_uri": "https://oauth2.googleapis.com/token",
145+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
146+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/your-service-account%40your-project.iam.gserviceaccount.com",
147+
"region": "us-east1"
148+
}' | tr -d '\n')" \
149+
-H 'Content-Type: application/json' \
150+
-d '{
151+
"model": "google-vertex-ai/google/gemini-2.5-pro",
152+
"messages": [
153+
{
154+
"role": "user",
155+
"content": "What is Cloudflare?"
156+
}
157+
]
158+
}'
159+
```
160+
161+
### Option 2: Direct Access Token
162+
163+
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.
164+
165+
:::note
166+
This option is only supported for the provider-specific endpoint, not for the unified chat completions endpoint.
167+
:::
168+
169+
```bash
170+
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" \
171+
-H "Authorization: Bearer ya29.c.b0Aaekm1K..." \
172+
-H 'Content-Type: application/json' \
173+
-d '{
174+
"contents": {
175+
"role": "user",
176+
"parts": [
177+
{
178+
"text": "Tell me more about Cloudflare"
179+
}
180+
]
181+
}
182+
}'
183+
```

0 commit comments

Comments
 (0)