Unified API (OpenAI compat)

Last updated Oct 2, 2026 Deprecated for single-model calls

For standard single-model chat completions, this endpoint is deprecated. Use the REST API instead, which provides OpenAI-compatible endpoints at api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/v1/chat/completions. The /compat/chat/completions endpoint will continue to work for existing integrations.

Dynamic routing

Dynamic routes (dynamic/{route}) can be invoked through this /compat/chat/completions endpoint, and existing integrations continue to work. For new integrations, use the REST API at /ai/v1/chat/completions or the AI binding instead. See Using a dynamic route for examples.

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.

Endpoint URL

https://gateway.ai.cloudflare.com/v1/{account_id}/default/compat/chat/completions

Replace {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.

Parameters

Switch providers by changing the model and apiKey parameters.

Specify the model using {provider}/{model} format. For example:

  • openai/gpt-5-mini
  • google-ai-studio/gemini-2.5-flash
  • anthropic/claude-sonnet-4-5

Examples

Make a request to

Unified API (OpenAI compat) - 图1 OpenAI

using

OpenAI JS SDK

with

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-6-luna",
    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-opus-5-5",
    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-4-argon",
    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.7",
    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-6-luna",
    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-opus-5-5",
    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-4-argon",
    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.7",
    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 { createAI } from "agents/models/ai-sdk";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const { text } = await generateText({
  model: ai("openai/gpt-6-luna"),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const { text } = await generateText({
  model: ai("anthropic/claude-opus-5.5"),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const { text } = await generateText({
  model: ai("google/gemini-4-argon"),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const { text } = await generateText({
  model: ai("xai/grok-4.7"),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const { text } = await generateText({
  model: ai("dynamic/customer-support"),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const { text } = await generateText({
  model: ai("@cf/meta/llama-3.3-70b-instruct-fp8-fast"),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const openai = createOpenAI({ apiKey: "{API_KEY}" });

const { text } = await generateText({
  model: ai(openai("gpt-6-luna"), { vendorKey: "request" }),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const openai = createOpenAI({ apiKey: "{API_KEY}" });

const { text } = await generateText({
  model: ai(openai("gpt-6-luna"), { vendorKey: "request" }),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { createAnthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const anthropic = createAnthropic({ apiKey: "{API_KEY}" });

const { text } = await generateText({
  model: ai(anthropic("claude-opus-5-5"), { vendorKey: "request" }),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { createAnthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const anthropic = createAnthropic({ apiKey: "{API_KEY}" });

const { text } = await generateText({
  model: ai(anthropic("claude-opus-5-5"), { vendorKey: "request" }),
  prompt: "What is Cloudflare?",
});
import { createAI } from "agents/models/ai-sdk";
import { createGoogleGenerativeAI } from "@ai-sdk/google";
import { generateText } from "ai";

const ai = createAI({
  accountId: "{CLOUDFLARE_ACCOUNT_ID}",
  apiToken: "{CF_API_TOKEN}",
  gateway: "{GATEWAY_NAME}",
});

const google = createGoogleGenerativeAI({ apiKey: "{API_KEY}" });

const { text } = await generateText({
  model: ai(google("gemini-4-argon"), { vendorKey: "request" }),
  prompt: "What is Cloudflare?",
});

```javascript import { createAI } from "agents/models/ai-sdk"; import { createGoogleGenerativeAI } from "@ai-sdk/google"; import { generateText } from "ai";