|
| 1 | +--- |
| 2 | +title: OpenAI Compatibility |
| 3 | +pcx_content_type: reference |
| 4 | +sidebar: |
| 5 | + order: 4 |
| 6 | +--- |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +## Endpoint URL |
| 11 | + |
| 12 | +```txt |
| 13 | +https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions |
| 14 | +``` |
| 15 | + |
| 16 | +Replace `{account_id}` and `{gateway_id}` with your Cloudflare account and gateway IDs. |
| 17 | + |
| 18 | +## Parameters |
| 19 | + |
| 20 | +Switch providers by changing the `model` and `apiKey` parameters. |
| 21 | + |
| 22 | +Specify the model using `{provider}/{model}` format. For example: |
| 23 | + |
| 24 | +- `openai/gpt-4o-mini` |
| 25 | +- `google-ai-studio/gemini-2.0-flash` |
| 26 | +- `anthropic/claude-3-haiku` |
| 27 | + |
| 28 | +## Examples |
| 29 | + |
| 30 | +### OpenAI SDK |
| 31 | + |
| 32 | +```js |
| 33 | +import OpenAI from "openai"; |
| 34 | +const client = new OpenAI({ |
| 35 | + apiKey: "YOUR_PROVIDER_API_KEY", // Provider API key |
| 36 | + baseURL: |
| 37 | + "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions", |
| 38 | +}); |
| 39 | + |
| 40 | +const response = await client.chat.completions.create({ |
| 41 | + model: "google-ai-studio/gemini-2.0-flash", |
| 42 | + messages: [{ role: "user", content: "What is Cloudflare?" }], |
| 43 | +}); |
| 44 | + |
| 45 | +console.log(response.choices[0].message.content); |
| 46 | +``` |
| 47 | + |
| 48 | +### cURL |
| 49 | + |
| 50 | +```bash |
| 51 | +curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \ |
| 52 | + --header 'Authorization: Bearer {openai_token}' \ |
| 53 | + --header 'Content-Type: application/json' \ |
| 54 | + --data '{ |
| 55 | + "model": "google-ai-studio/gemini-2.0-flash", |
| 56 | + "messages": [ |
| 57 | + { |
| 58 | + "role": "user", |
| 59 | + "content": "What is Cloudflare?" |
| 60 | + } |
| 61 | + ] |
| 62 | + }' |
| 63 | +``` |
| 64 | + |
| 65 | +### Universal provider |
| 66 | + |
| 67 | +You can also use this pattern with a [Universal Endpoint](/ai-gateway/universal/). |
| 68 | + |
| 69 | +```ts title="index.ts" |
| 70 | +export interface Env { |
| 71 | + AI: Ai; |
| 72 | +} |
| 73 | + |
| 74 | +export default { |
| 75 | + async fetch(request: Request, env: Env) { |
| 76 | + return env.AI.gateway("default").run({ |
| 77 | + provider: "compat", |
| 78 | + endpoint: "chat/completions", |
| 79 | + headers: { |
| 80 | + authorization: "Bearer ", |
| 81 | + }, |
| 82 | + query: { |
| 83 | + model: "google-ai-studio/gemini-2.0-flash", |
| 84 | + messages: [ |
| 85 | + { |
| 86 | + role: "user", |
| 87 | + content: "What is Cloudflare?", |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + }); |
| 92 | + }, |
| 93 | +}; |
| 94 | +``` |
| 95 | + |
| 96 | +## Supported Providers |
| 97 | + |
| 98 | +The OpenAI-compatible endpoint supports models from the following providers: |
| 99 | + |
| 100 | +- [Anthropic](/ai-gateway/providers/anthropic/) |
| 101 | +- [OpenAI](/ai-gateway/providers/openai/) |
| 102 | +- [Groq](/ai-gateway/providers/groq/) |
| 103 | +- [Mistral](/ai-gateway/providers/mistral/) |
| 104 | +- [Cohere](/ai-gateway/providers/cohere/) |
| 105 | +- [Perplexity](/ai-gateway/providers/perplexity/) |
| 106 | +- [Workers AI](/ai-gateway/providers/workersai/) |
| 107 | +- [Google-AI-Studio](/ai-gateway/providers/google-ai-studio/) |
| 108 | +- [Grok](/ai-gateway/providers/grok/) |
| 109 | +- [DeepSeek](/ai-gateway/providers/deepseek/) |
| 110 | +- [Cerebras](/ai-gateway/providers/cerebras/) |
0 commit comments