How to Build a Fast, Free API Proxy That Just Works (Using Cloudflare Workers)
Tired of hitting CORS errors when trying to fetch data in the browser? Want to keep your API keys safe and stop spinning up full backend servers just to call an endpoint?
You’re not alone. And the good news is: you don’t need a traditional backend to solve these problems.
With Cloudflare Workers, you can create a fast, secure, and completely free API proxy that just works—in minutes. Whether you’re building a side project, hacking together automations, or launching a full-on SaaS, this is a game-changer.
Let’s walk through it step-by-step.
🌐 What Is a Cloudflare Worker (and Why It’s Perfect for Proxies)
Cloudflare Workers are serverless functions that run on Cloudflare’s global edge network. That means:
- No server setup
- Insanely fast performance (close to the user)
- Free tier with generous limits
- Great for simple API calls, proxies, and automations
In short: the perfect place to drop in a fast, secure API proxy.
🔨 When and Why to Use an API Proxy
Here’s when a proxy helps:
- ✅ Bypass CORS issues when making requests from the browser
- ✅ Hide secret API keys from frontend code
- ✅ Modify requests or responses (add headers, reformat JSON)
- ✅ Chain multiple API calls behind one endpoint
- ✅ Add logging, rate limits, or authentication
If you’re building in Replit, Vercel, Netlify, or just don’t want a backend—this is for you.
💪 Step-by-Step: Build Your Proxy in 10 Minutes
🚀 Step 1: Set Up Your Cloudflare Account
- Visit workers.cloudflare.com
- Create a free account
🔧 Step 2: Install Wrangler (CLI Tool)
npm install -g wrangler
wrangler login
🔺 Step 3: Generate a New Project
wrangler init my-api-proxy
Choose “yes” to the default TypeScript/JavaScript template.
🧠 Step 4: Add Proxy Logic (src/index.js
or src/index.ts
)
Replace the contents with this basic proxy example:
export default {
async fetch(request) {
const apiUrl = 'https://api.openai.com/v1/chat/completions';
const body = await request.text();
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${OPENAI_KEY}`,
'Content-Type': 'application/json',
},
body
});
return new Response(await response.text(), {
status: response.status,
headers: { 'Content-Type': 'application/json' },
});
}
}
🔐 Step 5: Secure Your API Key
In your wrangler.toml
, add:
[vars]
OPENAI_KEY = "sk-xxxxxxxxxxxxxxxx"
Cloudflare will inject this as a secure environment variable.
🌎 Step 6: Deploy Your Worker
wrangler deploy
Done! You’ll get a live URL like:
https://my-api-proxy.your-namespace.workers.dev
📁 How to Use the Proxy in Your Frontend
Now that your proxy is live, you can call it like this:
const res = await fetch('https://my-api-proxy.workers.dev', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: "Hello!" }],
})
});
const data = await res.json();
No CORS error. No key leakage. Just clean responses.
🧰 Power Features to Add Later
- ✅ Query param passthrough
- ✅ Auth/token checks
- ✅ Rate limiting (per IP or header)
- ✅ KV storage or caching
- ✅ Support multiple APIs with route matching
You can also handle GET
, PUT
, etc., and validate payloads as needed.
📅 Free Tier Limits to Know
Cloudflare Workers’ free plan includes:
- 100,000 requests/day
- 1,000 scripts per account
- 10ms CPU time/request
That’s more than enough for indie projects, demos, and MVPs.
✅ Conclusion: Start Proxing Like a Pro
With just a few lines of code, you’ve built a:
- Globally distributed API proxy
- That hides secrets
- Beats CORS
- Runs for free
No server required.
Now you can safely call third-party APIs from your frontend, automation tools, or no-code platforms without compromise.
- Vultr vs DigitalOcean: Which Cloud Hosting Platform Is Right for You?
- How to Create a DigitalOcean Droplet (Step-by-Step Guide for Beginners)
- How to Build a Fast, Free API Proxy That Just Works (Using Cloudflare Workers)
- From CORS Errors to Superpowers: Why API Proxies Are a Must for Modern Coders
- Is Your Open Source Project at Risk? How OpenSSF’s Scorecard Exposes Hidden Security Flaws