-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[AI Gateway] Add Workers AI REST API docs and migrate examples to new… #30828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: production
Are you sure you want to change the base?
Changes from all commits
b9e9fa9
d7d0208
08760cc
e2a3e1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,29 +9,26 @@ products: | |
| - ai-gateway | ||
| --- | ||
|
|
||
| Using an Authenticated Gateway in AI Gateway adds security by requiring a valid authorization token for each request. This feature is especially useful when storing logs, as it prevents unauthorized access and protects against invalid requests that can inflate log storage usage and make it harder to find the data you need. With Authenticated Gateway enabled, only requests with the correct token are processed. | ||
| AI Gateway requires a valid Cloudflare API token for each request. This prevents unauthorized access and protects against invalid requests that can inflate log storage usage. | ||
|
|
||
| :::note | ||
| We recommend enabling Authenticated Gateway when opting to store logs with AI Gateway. | ||
|
|
||
| If Authenticated Gateway is enabled but a request does not include the required `cf-aig-authorization` header, the request will fail. This setting ensures that only verified requests pass through the gateway. To bypass the need for the `cf-aig-authorization` header, make sure to disable Authenticated Gateway. | ||
| ::: | ||
| When using the [REST API](/ai-gateway/usage/rest-api/), pass your Cloudflare API token in the standard `Authorization` header. When using [provider-native endpoints](/ai-gateway/usage/providers/) at `gateway.ai.cloudflare.com`, use the `cf-aig-authorization` header instead. | ||
|
|
||
| ## Setting up Authenticated Gateway using the Dashboard | ||
| ## Setting up Authenticated Gateway using the dashboard | ||
|
|
||
| 1. Go to the Settings for the specific gateway you want to enable authentication for. | ||
| 2. Select **Create authentication token** to generate a custom token with the required `Run` permissions. Be sure to securely save this token, as it will not be displayed again. | ||
| 3. Include the `cf-aig-authorization` header with your API token in each request for this gateway. | ||
| 3. Include the API token in each request: | ||
| - If using the REST API (`/ai/run`), include your Cloudflare API token in the standard `Authorization` header. | ||
| - If using [provider-native endpoints](/ai-gateway/usage/providers/) at `gateway.ai.cloudflare.com`, use the `cf-aig-authorization` header. | ||
| 4. Return to the settings page and toggle on Authenticated Gateway. | ||
|
|
||
| ## Example requests with OpenAI | ||
| ## Example requests | ||
|
|
||
| ```bash | ||
| curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions \ | ||
| --header 'cf-aig-authorization: Bearer {CF_AIG_TOKEN}' \ | ||
| --header 'Authorization: Bearer OPENAI_TOKEN' \ | ||
| --header 'Content-Type: application/json' \ | ||
| --data '{"model": "gpt-5-mini", "messages": [{"role": "user", "content": "What is Cloudflare?"}]}' | ||
| curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \ | ||
|
mchenco marked this conversation as resolved.
|
||
| --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ | ||
| --header "Content-Type: application/json" \ | ||
| --data '{"model": "openai/gpt-4.1-mini", "messages": [{"role": "user", "content": "What is Cloudflare?"}]}' | ||
| ``` | ||
|
|
||
| Using the OpenAI SDK: | ||
|
|
@@ -40,24 +37,24 @@ Using the OpenAI SDK: | |
| import OpenAI from "openai"; | ||
|
|
||
| const openai = new OpenAI({ | ||
| apiKey: process.env.OPENAI_API_KEY, | ||
| baseURL: "https://gateway.ai.cloudflare.com/v1/account-id/gateway/openai", | ||
| defaultHeaders: { | ||
| "cf-aig-authorization": `Bearer {token}`, | ||
| }, | ||
| apiKey: CLOUDFLARE_API_TOKEN, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it not process.env.CLOUDFLARE_API_TOKEN? (idk) |
||
| baseURL: `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/ai/v1`, | ||
| }); | ||
|
|
||
| const response = await openai.chat.completions.create({ | ||
| model: "openai/gpt-4.1-mini", | ||
| messages: [{ role: "user", content: "What is Cloudflare?" }], | ||
| }); | ||
| ``` | ||
|
|
||
| ## Example requests with the Vercel AI SDK | ||
| Using the Vercel AI SDK: | ||
|
|
||
| ```javascript | ||
| import { createOpenAI } from "@ai-sdk/openai"; | ||
|
|
||
| const openai = createOpenAI({ | ||
| baseURL: "https://gateway.ai.cloudflare.com/v1/account-id/gateway/openai", | ||
| headers: { | ||
| "cf-aig-authorization": `Bearer {token}`, | ||
| }, | ||
| apiKey: CLOUDFLARE_API_TOKEN, | ||
| baseURL: `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/ai/v1`, | ||
| }); | ||
| ``` | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,9 +16,7 @@ import { | |
| Render, | ||
| TabItem, | ||
| Tabs, | ||
| Badge, | ||
| } from "~/components"; | ||
| import CodeSnippets from "~/components/ai-gateway/code-examples.astro"; | ||
|
|
||
| In this guide, you will learn how to set up and use your first AI Gateway. | ||
|
|
||
|
|
@@ -34,11 +32,12 @@ Before making requests, you need two things: | |
| Run the following command to make your first request through AI Gateway: | ||
|
|
||
| ```bash | ||
| curl -X POST https://gateway.ai.cloudflare.com/v1/$CLOUDFLARE_ACCOUNT_ID/default/compat/chat/completions \ | ||
| --header "cf-aig-authorization: Bearer $CLOUDFLARE_API_TOKEN" \ | ||
| --header 'Content-Type: application/json' \ | ||
| curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \ | ||
| --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ | ||
| --header "Content-Type: application/json" \ | ||
| --data '{ | ||
| "model": "workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast", | ||
| "model": "moonshotai/kimi-k2.6", | ||
| "provider": "cloudflare", | ||
| "messages": [ | ||
| { | ||
| "role": "user", | ||
|
|
@@ -49,7 +48,7 @@ curl -X POST https://gateway.ai.cloudflare.com/v1/$CLOUDFLARE_ACCOUNT_ID/default | |
| ``` | ||
|
|
||
| :::note | ||
| AI Gateway automatically creates a gateway for you on the first request. The gateway is created with [authentication](/ai-gateway/configuration/authentication/) turned on, so the `cf-aig-authorization` header is required for all requests. For more details on how the default gateway works, refer to [Default gateway](/ai-gateway/configuration/manage-gateway/#default-gateway). | ||
| You do not need to create a gateway before sending requests. When no gateway ID is specified, AI Gateway uses `default` as the gateway ID and automatically creates it on the first authenticated request. To use a specific gateway, add the `cf-aig-gateway-id` header to your request. For more details, refer to [Default gateway](/ai-gateway/configuration/manage-gateway/#default-gateway). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This note says the default gateway is auto-created on the first authenticated request, but |
||
| ::: | ||
|
|
||
| <Details header="Create a gateway manually"> | ||
|
|
@@ -70,31 +69,21 @@ Authenticate with your upstream AI provider using one of the following options: | |
|
|
||
| ## Integration options | ||
|
|
||
| ### Unified API Endpoint | ||
| ### REST API | ||
|
|
||
| <Badge text="OpenAI Compatible" variant="tip" size="small" /><Badge text="Recommended" variant="success" size="small" /> | ||
| <br /> | ||
| <br /> | ||
|
|
||
| The easiest way to get started with AI Gateway is through our OpenAI-compatible `/chat/completions` endpoint. This allows you to use existing OpenAI SDKs and tools with minimal code changes while gaining access to multiple AI providers. | ||
|
|
||
| ` | ||
| https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions | ||
| ` | ||
|
|
||
| **Key benefits:** | ||
|
|
||
| - Drop-in replacement for OpenAI API, works with existing OpenAI SDKs and other OpenAI compliant clients | ||
| - Switch between providers by changing the `model` parameter | ||
| - Dynamic Routing - Define complex routing scenarios requiring conditional logic, conduct A/B tests, set rate / budget limits, etc | ||
|
|
||
|
|
||
| #### Example: | ||
|
|
||
| <CodeSnippets forceAPI="unified" /> | ||
| Call any model — whether hosted on Cloudflare or by a third-party provider — through the same Cloudflare API. No provider SDKs or API keys needed — authentication and billing are handled through your Cloudflare account. Three endpoints are available: `/ai/run` for all modalities, `/ai/v1/chat/completions` for OpenAI SDK compatibility, and `/ai/v1/responses` for agentic workflows. | ||
|
|
||
| ```bash | ||
| curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \ | ||
| --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ | ||
| --header "Content-Type: application/json" \ | ||
| --data '{ | ||
| "model": "openai/gpt-4.1-mini", | ||
| "messages": [{"role": "user", "content": "What is Cloudflare?"}] | ||
| }' | ||
| ``` | ||
|
|
||
| Refer to [Unified API](/ai-gateway/usage/chat-completion/) to learn more about OpenAI compatibility. | ||
| Refer to [REST API](/ai-gateway/usage/rest-api/) for details and examples. | ||
|
|
||
| ### Provider-specific endpoints | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the unified Cloudflare REST API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO qualifiers like "unified" only makes sense to people who knew that it was not unified before? but if you're looking at this fresh, it's just the REST API. And if you check the Workers AI docs seeing that it's the same just makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i do think we should add a Deprecation note
about how gateway.ai.cloudflare.com will still work, but we suggest everyone migrate to the new REST API endpoint?