Unified API (OpenAI compat)
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}/{gateway_id}/compat/chat/completionsReplace {account_id} and {gateway_id} with your Cloudflare account and gateway IDs.
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
With Key in Request
import OpenAI from "openai";
const client = new OpenAI({ apiKey: "YOUR_PROVIDER_API_KEY", defaultHeaders: { "cf-aig-authorization": `Bearer {cf_api_token}`, }, baseURL: "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",});
// Use different providers by changing the model parameterconst response = await client.chat.completions.create({ model: "google-ai-studio/gemini-2.5-flash", // or "openai/gpt-5-mini", "anthropic/claude-sonnet-4-5" messages: [{ role: "user", content: "Hello, world!" }],});import OpenAI from "openai";
const client = new OpenAI({ apiKey: "YOUR_PROVIDER_API_KEY", baseURL: "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",});
// Use different providers by changing the model parameterconst response = await client.chat.completions.create({ model: "google-ai-studio/gemini-2.5-flash", // or "openai/gpt-5-mini", "anthropic/claude-sonnet-4-5" messages: [{ role: "user", content: "Hello, world!" }],});With Stored Keys (BYOK) / Unified Billing
import OpenAI from "openai";
const client = new OpenAI({ apiKey: "{cf_api_token}", baseURL: "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat",});
// Ensure either your LLM Keys are stored with BYOK// or Unified Billing has creditsconst response = await client.chat.completions.create({ // Use different providers by changing the model parameter model: "google-ai-studio/gemini-2.5-flash", // or "openai/gpt-5-mini" messages: [{ role: "user", content: "Hello, world!" }],});With Key in Request
curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \ --header 'Authorization: Bearer {GOOGLE_GENERATIVE_AI_API_KEY}' \ --header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \ --header 'Content-Type: application/json' \ --data '{ "model": "google-ai-studio/gemini-2.5-flash", "messages": [ { "role": "user", "content": "What is Cloudflare?" } ] }'curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \ --header 'Authorization: Bearer {GOOGLE_GENERATIVE_AI_API_KEY}' \ --header 'Content-Type: application/json' \ --data '{ "model": "google-ai-studio/gemini-2.5-flash", "messages": [ { "role": "user", "content": "What is Cloudflare?" } ] }'With Stored Keys (BYOK) / Unified Billing
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-ai-studio/gemini-2.5-flash", "messages": [ { "role": "user", "content": "What is Cloudflare?" } ] }'The OpenAI-compatible endpoint supports models from the following providers:
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-