Cloudflare's AI Gateway offers an OpenAI-compatible /chat/completions endpoint, enabling integration with multiple AI providers using a single URL. This feature simplifies the integration process, allowing for seamless switching between different models without significant code modifications.
https://gateway.ai.cloudflare.com/v1/{account_id}/default/compat/chat/completionsReplace {account_id} with your Cloudflare account ID. The default gateway is created automatically on your first request — no setup needed. You can also replace default with a specific gateway ID if you have already created one.
Switch providers by changing the model and apiKey parameters.
Specify the model using {provider}/{model} format. For example:
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?"
}
]
}'The OpenAI-compatible endpoint supports models from the following providers: