A powerful Cloudflare Workers-based AI service aggregator that provides unified API endpoints compatible with both OpenAI and Anthropic formats. Built on top of Cloudflare AI Gateway for enhanced reliability, monitoring, and cost optimization.
- Unified API Interface: Single endpoint supporting both OpenAI and Anthropic API formats
- Multi-Provider Support: Seamlessly integrate with multiple AI providers through Cloudflare AI Gateway
- Streaming Support: Real-time streaming responses for both API formats
- CORS Enabled: Ready for web applications with proper CORS handling
- Type Safety: Full TypeScript implementation with comprehensive type definitions
- Tool/Function Calling: Support for function calling in both OpenAI and Anthropic formats
- Azure OpenAI Integration: Native support for Azure OpenAI deployments
- Request Validation: Robust input validation and error handling
- Image Support: Full support for vision models and image analysis
- BYOK Integration: Secure API key storage using Cloudflare's BYOK feature
Via Cloudflare AI Gateway:
- OpenAI (GPT-4, GPT-3.5, GPT-4V)
- Anthropic (Claude 3.5, Claude 3 Haiku/Sonnet/Opus)
- Google AI Studio (Gemini Pro, Gemini Flash)
- Groq (Mixtral, Llama models)
- Mistral (Mistral Large, Codestral)
- Grok (xAI models)
- DeepSeek (DeepSeek Coder, DeepSeek Chat)
- Cerebras (Llama models)
- Perplexity AI (Sonar models)
- Azure OpenAI
- Cohere
- Workers AI
- Cloudflare Account: Sign up at Cloudflare
- AI Gateway Setup: Follow the AI Gateway Getting Started Guide
- Provider API Keys: Obtain API keys from your chosen AI providers
- Node.js: Version 16 or higher
- pnpm: Package manager (or npm/yarn)
git clone https://github.com/loadchange/apex-ai-proxy.git
cd apex-ai-proxy
pnpm install- Navigate to your Cloudflare Dashboard
- Go to AI → AI Gateway
- Select Create Gateway
- Enter your Gateway name (e.g.,
apex-ai-proxy) - Select Create
- Note your Account ID and Gateway ID from the gateway settings
- In your AI Gateway dashboard, go to Settings
- Enable Authentication to secure your gateway
- Generate a Gateway Token for API access
- Store this token securely - you'll need it for deployment
You have three options for provider authentication:
Store your API keys securely in Cloudflare:
- In your AI Gateway dashboard, go to Provider Keys section
- Click Add API Key
- Select your AI provider from the dropdown
- Enter your API key and optionally provide a description
- Click Save
- Repeat for all providers you want to use
Benefits of BYOK:
- Secure storage with Cloudflare Secrets Store
- Easy key rotation without code changes
- Enhanced security - keys never exposed in requests
- Rate limiting and budget controls per provider
Include provider API keys in request headers (traditional approach):
curl -H "Authorization: Bearer YOUR_PROVIDER_API_KEY" \
-H "cf-aig-authorization: Bearer YOUR_GATEWAY_TOKEN"Use Cloudflare's billing system (where available) without managing individual provider keys.
Edit wrangler.jsonc:
How to find your IDs:
- Account ID: Available in the right sidebar of any Cloudflare dashboard page
- Gateway ID: Found in your AI Gateway settings page URL or dashboard
Create a secret for your gateway token:
# Set your gateway authentication token
wrangler secret put GatewayToken
# Enter your Gateway Token when prompted (from step 2)# Deploy to Cloudflare Workers
pnpm run deployAfter deployment, your worker will be available at:
https://your-worker-name.your-subdomain.workers.dev
Important: This proxy uses the format model#provider (note the # separator):
- OpenAI:
gpt-4#openai,gpt-3.5-turbo#openai - Anthropic:
claude-3-5-haiku-20241022#anthropic,claude-3-5-sonnet-20241022#anthropic - Google:
gemini-2.0-flash#google-ai-studio - Groq:
mixtral-8x7b-32768#groq - Mistral:
mistral-large-latest#mistral - Azure OpenAI:
gpt-4#azure-openai
curl -X POST "https://your-worker.your-subdomain.workers.dev/v1/chat/completions" \
-H "Authorization: Bearer your-gateway-token" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-haiku-20241022#anthropic",
"messages": [
{
"role": "user",
"content": "Hello, world!"
}
],
"stream": true
}'curl -X POST "https://your-worker.your-subdomain.workers.dev/v1/messages" \
-H "Authorization: Bearer your-gateway-token" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-3-5-sonnet-20241022#anthropic",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": "Hello, world!"
}
],
"stream": true
}'Both endpoints support vision models for image analysis:
curl -X POST "https://your-worker.your-subdomain.workers.dev/v1/chat/completions" \
-H "Authorization: Bearer your-gateway-token" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4-vision-preview#openai",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What do you see in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ..."
}
}
]
}
]
}'from openai import OpenAI
client = OpenAI(
api_key="your-gateway-token",
base_url="https://your-worker.your-subdomain.workers.dev/v1"
)
# Use Claude via OpenAI client
response = client.chat.completions.create(
model="claude-3-5-haiku-20241022#anthropic",
messages=[
{"role": "user", "content": "Hello!"}
]
)from anthropic import Anthropic
client = Anthropic(
api_key="your-gateway-token",
base_url="https://your-worker.your-subdomain.workers.dev"
)
response = client.messages.create(
model="claude-3-5-sonnet-20241022#anthropic",
max_tokens=1000,
messages=[
{"role": "user", "content": "Hello!"}
]
)Configure your IDE to use the adapter:
Cursor Settings (Settings → Cursor Settings):
{
"cursor.general.apiKey": "your-gateway-token",
"cursor.general.baseURL": "https://your-worker.your-subdomain.workers.dev/v1",
"cursor.general.model": "claude-3-5-haiku-20241022#anthropic"
}VS Code with Continue Extension:
{
"models": [
{
"title": "Claude 3.5 Haiku",
"provider": "openai",
"model": "claude-3-5-haiku-20241022#anthropic",
"apiKey": "your-gateway-token",
"apiBase": "https://your-worker.your-subdomain.workers.dev/v1"
}
]
}| Variable | Description | Required | Example |
|---|---|---|---|
ACCOUNT_ID |
Your Cloudflare Account ID | ✅ | 1234567890abcdef1234567890abcdef |
GATEWAY_ID |
Your AI Gateway ID | ✅ | my-gateway |
AZURE_RESOURCE |
Azure OpenAI resource name | ❌ | my-azure-openai |
AZURE_API_VERSION |
Azure OpenAI API version | ❌ | 2024-02-01 |
| Secret | Description | Required | How to Set |
|---|---|---|---|
GatewayToken |
Authentication token for API access | ✅ | wrangler secret put GatewayToken |
# Start local development server
pnpm run dev
# Your proxy will be available at http://localhost:8787# Run tests
pnpm run test
# Test with specific provider
curl -X POST "http://localhost:8787/v1/chat/completions" \
-H "Authorization: Bearer test-token" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-3.5-turbo#openai", "messages": [{"role": "user", "content": "test"}]}'# Generate Cloudflare Workers types
pnpm run cf-typegen┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐ │ AI Clients │───▶│ AI Gateway │───▶│ Cloudflare AI │ │ │ │ Adapter │ │ Gateway │ │ • Codex │ │ (Workers) │ │ │ │ • Claude Code │ │ │ │ • Rate Limiting │ │ │ │ • Protocol Conv. │ │ • Monitoring │ │ │ │ • Validation │ │ • Cost Control │ │ │ │ • Error Handling │ │ • BYOK Storage │ │ │ │ • Image Support │ │ • Caching │ └─────────────────┘ └──────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐
│ AI Providers │
│ │
│ • OpenAI │
│ • Anthropic │
│ • Google │
│ • Groq │
│ • Mistral │
│ • Others │
└─────────────────┘
The proxy automatically converts between OpenAI and Anthropic message formats:
- System Messages: Properly handled for each provider's requirements
- Tool Calls: Function calling support across both formats
- Streaming: Real-time streaming with proper Server-Sent Events formatting
- Error Handling: Consistent error responses across providers
- Image Content: Full support for vision models and multi-modal inputs
- Input validation for both API formats
- Model name validation with
model#providerformat - Authentication verification via Gateway Token
- CORS handling for web applications
- Content-type validation and parsing
- Dynamic provider routing based on model specification
- Support for Cloudflare AI Gateway's unified endpoints
- Azure OpenAI integration with deployment management
- Automatic error handling and fallback responses
- Special handling for provider-specific features (e.g., Mistral streaming options)
Leverage Cloudflare AI Gateway's built-in monitoring:
- Request Analytics: Track usage patterns, costs, and performance metrics
- Rate Limiting: Configure per-provider and per-user limits
- Caching: Reduce costs with intelligent response caching
- Logs: Detailed request and response logging with full audit trail
- Cost Tracking: Monitor spending across all providers
- Error Analytics: Track error rates and failure patterns
Access analytics at: https://dash.cloudflare.com/[account-id]/ai/ai-gateway/[gateway-id]
- Use BYOK: Store API keys securely in Cloudflare rather than in code
- Enable Gateway Authentication: Protect your proxy with authentication tokens
- Set Rate Limits: Configure appropriate rate limits to prevent abuse
- Monitor Usage: Regularly review analytics for unusual patterns
- Rotate Keys: Periodically rotate both gateway tokens and provider API keys
- Use HTTPS: Always use HTTPS endpoints in production
- Invalid Model Format: Ensure you're using
model#providerformat (notprovider/model) - Authentication Errors: Verify your Gateway Token is set correctly as a secret
- Provider Errors: Check that your provider API keys are valid and have sufficient credits
- CORS Issues: Ensure your client is sending proper headers for cross-origin requests
Enable debug logging by setting the DEBUG environment variable:
wrangler secret put DEBUG
# Enter "true" when prompted- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License. See the LICENSE file for details.
- Documentation: Cloudflare AI Gateway Docs
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Cloudflare Workers for the serverless platform
- Cloudflare AI Gateway for AI provider management
- All the AI providers for their excellent APIs
{ "$schema": "node_modules/wrangler/config-schema.json", "name": "apex-ai-proxy", "main": "src/index.ts", "compatibility_date": "2024-09-01", "vars": { "ACCOUNT_ID": "your-cloudflare-account-id", "GATEWAY_ID": "your-gateway-id", "AZURE_RESOURCE": "your-azure-resource-name", // Optional: for Azure OpenAI "AZURE_API_VERSION": "2024-02-01" // Optional: for Azure OpenAI } }