跳转到内容
搜索文档

Google AI Studio

最后更新 查看 MarkdownAgent 设置

Google AI Studio 帮助你使用 Google Gemini 模型快速构建。

端点

基础 URL:

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-ai-studio

然后可以追加要访问的端点,例如:v1/models/{model}:{generative_ai_rest_resource}

因此最终 URL 为:https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/google-ai-studio/v1/models/{model}:{generative_ai_rest_resource}

示例

cURL

在请求中包含 API 密钥

curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/google-ai-studio/v1/models/gemini-2.5-flash:generateContent" \
 --header 'content-type: application/json' \
 --header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
 --header 'x-goog-api-key: {google_studio_api_key}' \
 --data '{
      "contents": [
          {
            "role":"user",
            "parts": [
              {"text":"What is Cloudflare?"}
            ]
          }
        ]
      }'
curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/google-ai-studio/v1/models/gemini-2.5-flash:generateContent" \
 --header 'content-type: application/json' \
 --header 'x-goog-api-key: {google_studio_api_key}' \
 --data '{
      "contents": [
          {
            "role":"user",
            "parts": [
              {"text":"What is Cloudflare?"}
            ]
          }
        ]
      }'

使用存储密钥 (BYOK) / Unified Billing

curl "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_name}/google-ai-studio/v1/models/gemini-2.5-flash:generateContent" \
 --header 'content-type: application/json' \
 --header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
 --data '{
      "contents": [
          {
            "role":"user",
            "parts": [
              {"text":"What is Cloudflare?"}
            ]
          }
        ]
      }'

@google/genai

如果你使用 @google/genai 包,可以这样设置端点:

在请求中包含密钥

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: "{google_studio_api_key}",
  httpOptions: {
	  baseUrl: `https://gateway.ai.cloudflare.com/v1/${account_id}/${gateway_name}/google-ai-studio`,
	  headers: {
		  'cf-aig-authorization': 'Bearer {cf_aig_token}',
	  }	
  }
});

const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: "What is Cloudflare?",
});

console.log(response.text);
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: "{google_studio_api_key}",
  httpOptions: {
	  baseUrl: `https://gateway.ai.cloudflare.com/v1/${account_id}/${gateway_name}/google-ai-studio`,
  }
});

const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: "What is Cloudflare?",
});

console.log(response.text);

使用存储密钥 (BYOK) / Unified Billing

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  apiKey: "{cf_aig_token}",
  httpOptions: {
	  baseUrl: `https://gateway.ai.cloudflare.com/v1/${account_id}/${gateway_name}/google-ai-studio`,
  }
});

const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: "What is Cloudflare?",
});

console.log(response.text);

OpenAI 兼容端点

你也可以通过 REST API 使用 OpenAI API schema 访问 Google AI Studio 模型。将请求发送至:

https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1/chat/completions

指定:


{
"model": "google-ai-studio/{model}"
}

这篇文档对您有帮助吗?