Cloudflare 的 AI Gateway 提供与 OpenAI 兼容的 /chat/completions 端点,使你能够使用单个 URL 与多个 AI 提供商集成。此功能简化了集成流程,允许在不同模型之间无缝切换,而无需大幅修改代码。
https://gateway.ai.cloudflare.com/v1/{account_id}/default/compat/chat/completions将 {account_id} 替换为你的 Cloudflare 账户 ID。default gateway 会在你的首次请求时自动创建 — 无需额外设置。如果你已创建 gateway,也可以将 default 替换为特定的 gateway ID。
通过更改 model 和 apiKey 参数来切换提供商。
使用 {provider}/{model} 格式指定模型。例如:
openai/gpt-5-minigoogle-ai-studio/gemini-2.5-flashanthropic/claude-sonnet-4-5
Make a request to
OpenAIusing with
OpenAI JS SDK
Stored Key (BYOK)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{cf_api_token}",
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "openai/gpt-5.2",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{cf_api_token}",
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "anthropic/claude-4-5-sonnet",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{cf_api_token}",
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "google/gemini-2.5-pro",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{cf_api_token}",
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "grok/grok-4",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{cf_api_token}",
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "dynamic/customer-support",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{cf_api_token}",
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{openai_api_token}",
defaultHeaders: {
// if gateway is authenticated
"cf-aig-authorization": `Bearer {cf_api_token}`,
},
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "openai/gpt-5.2",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{anthropic_api_token}",
defaultHeaders: {
// if gateway is authenticated
"cf-aig-authorization": `Bearer {cf_api_token}`,
},
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "anthropic/claude-4-5-sonnet",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{google_api_token}",
defaultHeaders: {
// if gateway is authenticated
"cf-aig-authorization": `Bearer {cf_api_token}`,
},
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "google/gemini-2.5-pro",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{grok_api_token}",
defaultHeaders: {
// if gateway is authenticated
"cf-aig-authorization": `Bearer {cf_api_token}`,
},
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "grok/grok-4",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{dynamic_api_token}",
defaultHeaders: {
// if gateway is authenticated
"cf-aig-authorization": `Bearer {cf_api_token}`,
},
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "dynamic/customer-support",
messages: [{ role: "user", content: "Hello, world!" }],
});import OpenAI from "openai";
const client = new OpenAI({
apiKey: "{workers-ai_api_token}",
defaultHeaders: {
// if gateway is authenticated
"cf-aig-authorization": `Bearer {cf_api_token}`,
},
baseURL:
"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",
});
const response = await client.chat.completions.create({
model: "workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast",
messages: [{ role: "user", content: "Hello, world!" }],
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified();
const { text } = await generateText({
model: aigateway(unified('openai/gpt-5.2')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified();
const { text } = await generateText({
model: aigateway(unified('anthropic/claude-4-5-sonnet')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified();
const { text } = await generateText({
model: aigateway(unified('google/gemini-2.5-pro')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified();
const { text } = await generateText({
model: aigateway(unified('grok/grok-4')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified();
const { text } = await generateText({
model: aigateway(unified('dynamic/customer-support')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified();
const { text } = await generateText({
model: aigateway(unified('workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(unified('openai/gpt-5.2')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(unified('anthropic/claude-4-5-sonnet')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(unified('google/gemini-2.5-pro')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(unified('grok/grok-4')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(unified('dynamic/customer-support')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(unified('workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createOpenAI } from 'ai-gateway-provider/providers/openai';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const openai = createOpenAI();
const { text } = await generateText({
model: aigateway(openai.chat('gpt-5.2')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createAnthropic } from 'ai-gateway-provider/providers/anthropic';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const anthropic = createAnthropic();
const { text } = await generateText({
model: aigateway(anthropic('claude-4-5-sonnet')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createGoogle } from 'ai-gateway-provider/providers/google';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const google = createGoogle();
const { text } = await generateText({
model: aigateway(google('gemini-2.5-pro')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createXai } from 'ai-gateway-provider/providers/xai';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const xai = createXai();
const { text } = await generateText({
model: aigateway(xai('grok-4')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified();
const { text } = await generateText({
model: aigateway(unified('customer-support')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified();
const { text } = await generateText({
model: aigateway(unified('@cf/meta/llama-3.3-70b-instruct-fp8-fast')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createOpenAI } from 'ai-gateway-provider/providers/openai';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const openai = createOpenAI({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(openai.chat('gpt-5.2')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createAnthropic } from 'ai-gateway-provider/providers/anthropic';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const anthropic = createAnthropic({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(anthropic('claude-4-5-sonnet')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createGoogle } from 'ai-gateway-provider/providers/google';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const google = createGoogle({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(google('gemini-2.5-pro')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createXai } from 'ai-gateway-provider/providers/xai';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const xai = createXai({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(xai('grok-4')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(unified('customer-support')),
prompt: 'What is Cloudflare?',
});import { createAiGateway } from 'ai-gateway-provider';
import { createUnified } from 'ai-gateway-provider/providers/unified';
import { generateText } from "ai";
const aigateway = createAiGateway({
accountId: "{CLOUDFLARE_ACCOUNT_ID}",
gateway: '{GATEWAY_NAME}',
apiKey: '{CF_AIG_TOKEN}',
});
const unified = createUnified({ apiKey: '{API_KEY}' });
const { text } = await generateText({
model: aigateway(unified('@cf/meta/llama-3.3-70b-instruct-fp8-fast')),
prompt: 'What is Cloudflare?',
});curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Content-Type: application/json' \
--data '{
"model": "openai/gpt-5.2",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Content-Type: application/json' \
--data '{
"model": "anthropic/claude-4-5-sonnet",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Content-Type: application/json' \
--data '{
"model": "google/gemini-2.5-pro",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Content-Type: application/json' \
--data '{
"model": "grok/grok-4",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Content-Type: application/json' \
--data '{
"model": "dynamic/customer-support",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Content-Type: application/json' \
--data '{
"model": "workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Authorization: Bearer {openai_api_token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "openai/gpt-5.2",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Authorization: Bearer {anthropic_api_token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "anthropic/claude-4-5-sonnet",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Authorization: Bearer {google_api_token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "google/gemini-2.5-pro",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Authorization: Bearer {grok_api_token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "grok/grok-4",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Authorization: Bearer {dynamic_api_token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "dynamic/customer-support",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \
--header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \
--header 'Authorization: Bearer {workers-ai_api_token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'OpenAI 兼容端点支持以下提供商的模型: