|
| 1 | +--- |
| 2 | +title: Chat Completion Endpoint |
| 3 | +pcx_content_type: configuration |
| 4 | +sidebar: |
| 5 | + order: 5 |
| 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 | +## Key benefits |
| 11 | + |
| 12 | +- Standardization: Provides a unified format compatible with OpenAI's schema, reducing the need for code refactoring. |
| 13 | +- Ease of Development: Switch between different models and providers quickly using a consistent API structure. |
| 14 | + |
| 15 | +## Endpoint URL |
| 16 | + |
| 17 | +```txt |
| 18 | +https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions |
| 19 | +``` |
| 20 | + |
| 21 | +Replace `{account_id}` and `{gateway_id}` with your Cloudflare account and gateway IDs. |
| 22 | + |
| 23 | +## Using the Unified Interface |
| 24 | + |
| 25 | +Switch providers by changing the `model` and `apiKey` parameters. |
| 26 | + |
| 27 | +## Model Parameter Format |
| 28 | + |
| 29 | +Specify the model using `{provider}/{model}` format. For example: |
| 30 | + |
| 31 | +- `openai/gpt-4o-mini` |
| 32 | +- `google-ai-studio/gemini-2.0-flash` |
| 33 | +- `anthropic/claude-3-haiku` |
| 34 | + |
| 35 | +## Example with OpenAI JavaScript SDK |
| 36 | + |
| 37 | +```js |
| 38 | +import OpenAI from "openai"; |
| 39 | +const client = new OpenAI({ |
| 40 | + apiKey: "YOUR_PROVIDER_API_KEY", // Provider API key |
| 41 | + baseURL: |
| 42 | + "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions", |
| 43 | +}); |
| 44 | + |
| 45 | +const response = await client.chat.completions.create({ |
| 46 | + model: "google-ai-studio/gemini-2.0-flash", |
| 47 | + messages: [{ role: "user", content: "What is Cloudflare?" }], |
| 48 | +}); |
| 49 | + |
| 50 | +console.log(response.choices[0].message.content); |
| 51 | +``` |
| 52 | + |
| 53 | +## `curl` Example |
| 54 | + |
| 55 | +```bash |
| 56 | +curl -X POST https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions \ |
| 57 | + --header 'Authorization: Bearer {openai_token}' \ |
| 58 | + --header 'Content-Type: application/json' \ |
| 59 | + --data '{ |
| 60 | + "model": "google-ai-studio/gemini-2.0-flash", |
| 61 | + "messages": [ |
| 62 | + { |
| 63 | + "role": "user", |
| 64 | + "content": "What is Cloudflare?" |
| 65 | + } |
| 66 | + ] |
| 67 | + }' |
| 68 | +``` |
| 69 | + |
| 70 | +## Supported Providers |
| 71 | + |
| 72 | +The OpenAI-compatible endpoint supports models from the following providers: |
| 73 | + |
| 74 | +- [Anthropic](/ai-gateway/providers/anthropic/) |
| 75 | +- [OpenAI](/ai-gateway/providers/openai/) |
| 76 | +- [Groq](/ai-gateway/providers/groq/) |
| 77 | +- [Mistral](/ai-gateway/providers/mistral/) |
| 78 | +- [Cohere](/ai-gateway/providers/cohere/) |
| 79 | +- [Perplexity](/ai-gateway/providers/perplexity/) |
| 80 | +- [Workers AI](/ai-gateway/providers/workersai/) |
| 81 | +- [Google-AI-Studio](/ai-gateway/providers/google-ai-studio/) |
| 82 | +- [Grok](/ai-gateway/providers/grok/) |
| 83 | +- [DeepSeek](/ai-gateway/providers/deepseek/) |
| 84 | +- [Cerebras](/ai-gateway/providers/cerebras/) |
0 commit comments