X Tutup
Skip to content
Cloudflare Docs

Getting started

Last reviewed: over 1 year ago

In this guide, you will learn how to set up and use your first AI Gateway.

Get your account ID and authentication token

Before making requests, you need two things:

  1. Your Account ID — find it in the Cloudflare dashboard.
  2. A Cloudflare API tokencreate an API token with AI Gateway - Read and AI Gateway - Edit permissions. The example below also uses Workers AI, so add Workers AI - Read as well.

Send your first request

Run the following command to make your first request through AI Gateway:

Terminal window
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' \
--data '{
"model": "workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast",
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}'

Create a gateway manually

You can also create gateways manually with a custom name and configuration through the dashboard or API.

Go to AI Gateway
  1. Log into the Cloudflare dashboard and select your account.
  2. Go to AI > AI Gateway.
  3. Select Create Gateway.
  4. Enter your Gateway name. Note: Gateway name has a 64 character limit.
  5. Select Create.

Provider authentication

Authenticate with your upstream AI provider using one of the following options:

  • Unified Billing: Use the AI Gateway billing to pay for and authenticate your inference requests. Refer to Unified Billing.
  • BYOK (Store Keys): Store your own provider API Keys with Cloudflare, and AI Gateway will include them at runtime. Refer to BYOK.
  • Request headers: Include your provider API Key in the request headers as you normally would (for example, Authorization: Bearer <OPENAI_API_KEY>).

Integration options

Unified API Endpoint

OpenAI Compatible Recommended

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:

Make a request to
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-5.2",
messages: [{ role: "user", content: "Hello, world!" }],
});

Refer to Unified API to learn more about OpenAI compatibility.

Provider-specific endpoints

For direct integration with specific AI providers, use dedicated endpoints that maintain the original provider's API schema while adding AI Gateway features.

https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/{provider}

Available providers:

Next steps

X Tutup