Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/ai-gateway/configuration.md
1# Configuration & Setup23## Creating a Gateway45### Dashboard6AI > AI Gateway > Create Gateway > Configure (auth, caching, rate limiting, logging)78### API9```bash10curl -X POST https://api.cloudflare.com/client/v4/accounts/{account_id}/ai-gateway/gateways \11-H "Authorization: Bearer $CF_API_TOKEN" -H "Content-Type: application/json" \12-d '{"id":"my-gateway","cache_ttl":3600,"rate_limiting_interval":60,"rate_limiting_limit":100,"collect_logs":true}'13```1415**Naming:** lowercase alphanumeric + hyphens (e.g., `prod-api`, `dev-chat`)1617## Wrangler Integration1819```toml20[ai]21binding = "AI"2223[[ai.gateway]]24id = "my-gateway"25```2627```bash28wrangler secret put CF_API_TOKEN29wrangler secret put OPENAI_API_KEY # If not using BYOK30```3132## Authentication3334### Gateway Auth (protects gateway access)35```typescript36const client = new OpenAI({37baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`,38defaultHeaders: { 'cf-aig-authorization': `Bearer ${cfToken}` }39});40```4142### Provider Auth Options4344**1. Unified Billing (keyless)** - pay through Cloudflare, no provider key:45```typescript46const client = new OpenAI({47baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`,48defaultHeaders: { 'cf-aig-authorization': `Bearer ${cfToken}` }49});50```51Supports: OpenAI, Anthropic, Google AI Studio5253**2. BYOK** - store keys in dashboard (Provider Keys > Add), no key in code5455**3. Request Headers** - pass provider key per request:56```typescript57const client = new OpenAI({58apiKey: process.env.OPENAI_API_KEY,59baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/openai`,60defaultHeaders: { 'cf-aig-authorization': `Bearer ${cfToken}` }61});62```6364## API Token Permissions6566- **Gateway management:** AI Gateway - Read + Edit67- **Gateway access:** AI Gateway - Read (minimum)6869## Gateway Management API7071```bash72# List73curl https://api.cloudflare.com/client/v4/accounts/{account_id}/ai-gateway/gateways \74-H "Authorization: Bearer $CF_API_TOKEN"7576# Get77curl .../gateways/{gateway_id}7879# Update80curl -X PUT .../gateways/{gateway_id} \81-d '{"cache_ttl":7200,"rate_limiting_limit":200}'8283# Delete84curl -X DELETE .../gateways/{gateway_id}85```8687## Getting IDs8889- **Account ID:** Dashboard > Overview > Copy90- **Gateway ID:** AI Gateway > Gateway name column9192## Python Example9394```python95from openai import OpenAI96import os9798client = OpenAI(99api_key=os.environ.get("OPENAI_API_KEY"),100base_url=f"https://gateway.ai.cloudflare.com/v1/{os.environ['CF_ACCOUNT_ID']}/{os.environ['GATEWAY_ID']}/openai",101default_headers={"cf-aig-authorization": f"Bearer {os.environ['CF_API_TOKEN']}"}102)103```104105## Best Practices1061071. **Always authenticate gateways in production**1082. **Use BYOK or unified billing** - secrets out of code1093. **Environment-specific gateways** - separate dev/staging/prod1104. **Set rate limits** - prevent runaway costs1115. **Enable logging** - track usage, debug issues112