Switch Language
Toggle Theme

OpenAI Blocked in China? Set Up Workers Proxy for Free in 5 Minutes (Complete Code Included)

Introduction

Last week, I wanted to build a ChatGPT application for fun. After finishing the frontend code, I excitedly called the API, only to get a connection timeout. Tried several times with no luck, then realized—OpenAI is completely blocked in China.

To be honest, being blocked by the network feels really frustrating. I tried proxy services from Taobao but worried they weren’t reliable—what if my API Key got leaked? I also considered buying a VPS to set up my own proxy, but the cost was at least several dozen dollars a month, plus the hassle of server configuration and maintenance. Just thinking about it gave me a headache.

Then I discovered Cloudflare Workers. Completely free and takes only 5 minutes to set up. I’ve been using it for over two months now, and it’s amazing—not only stable but often faster than many paid proxies. This article shares the complete setup process with ready-to-use code.

Why Choose Cloudflare Workers?

Zero Cost, Perfect for Individual Developers

The Workers free plan gives you 100K requests per day and 1,000 requests per minute. You might wonder—can free be any good? I thought the same initially. But after using it, I found that for personal development, learning, or small projects, this quota is more than enough.

Let’s do the math: Assuming each request takes 2 seconds on average, working 8 hours straight non-stop, that’s about 2,000+ requests. With a 100K quota, you’d need to use it continuously for several days to run out.

No Server Needed, Hassle-Free

Traditional solutions require buying a VPS, installing Nginx for reverse proxy, and worrying about server downtime. Workers needs none of that—Cloudflare handles all the infrastructure. You just write a few lines of code.

Plus, Workers runs on Cloudflare’s global CDN network, theoretically faster than your self-hosted single server. After all, Cloudflare has nodes in over 300 cities worldwide.

Built-in API Key Protection

This is particularly important. If you call OpenAI API directly from the frontend, your key will definitely be exposed in the browser—anyone opening developer tools can see it. With Workers as a middleware, the frontend only calls your Worker URL, while the actual API Key stays safely in Cloudflare’s environment variables.

2025 New Benefits

By the way, in August 2025, Cloudflare partnered with OpenAI to integrate OpenAI’s open-source models directly into Workers AI. This means besides proxying the original API, you can also directly use Cloudflare’s provided models with 10,000 Neurons of free daily quota.

Preparation

The preparation is super simple. You need:

Accounts and Resources:

  • Cloudflare account (free registration, takes a few minutes)
  • OpenAI or Claude API Key (you should already have this)
  • Domain (optional, Workers provides a free .workers.dev subdomain)

Technical Requirements:

  • Basic JavaScript knowledge (just need to understand fetch requests)
  • Understanding of HTTP

Time Cost:

  • First-time setup: 5-10 minutes
  • Once familiar: 3 minutes

Hands-on: Set Up OpenAI Proxy in 5 Minutes

Step 1: Create a Worker

Log into Cloudflare console, find “Workers & Pages” in the left menu. Click “Create Application,” then select “Create Worker.”

Cloudflare will automatically give your Worker a random name (like aged-shadow-1234). You can change it to something like “openai-proxy.” Click “Deploy.”

Now you have a running Worker, though it doesn’t do anything yet.

Step 2: Write the Code

Click “Edit Code” to enter the code editor and paste this code:

export default {
  async fetch(request, env) {
    const url = new URL(request.url);

    // Replace domain with OpenAI's API address
    url.hostname = 'api.openai.com';

    // Create new request
    const newRequest = new Request(url, {
      method: request.method,
      headers: request.headers,
      body: request.body
    });

    // Forward request and return response
    const response = await fetch(newRequest);

    // Handle CORS cross-origin issues
    const newResponse = new Response(response.body, response);
    newResponse.headers.set('Access-Control-Allow-Origin', '*');
    newResponse.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    newResponse.headers.set('Access-Control-Allow-Headers', 'Content-Type, Authorization');

    return newResponse;
  }
};

Here’s what this code does:

  1. Receives requests from the frontend
  2. Replaces the request domain with api.openai.com
  3. Forwards the modified request to OpenAI
  4. Returns OpenAI’s response back to the frontend unchanged
  5. Also handles CORS (cross-origin issues)

Click “Save and Deploy.”

Step 3: Test It

After deployment, you’ll see a Worker URL like https://openai-proxy.yourname.workers.dev.

Test it with curl (replace YOUR_API_KEY with your OpenAI key):

curl https://openai-proxy.yourname.workers.dev/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

If you see a normal response from OpenAI, congratulations—you’ve succeeded!

Advanced: Support Multiple AI Services

Claude API Proxy

Claude’s API structure differs slightly from OpenAI’s, mainly in the request headers. Modify the code to support Claude:

export default {
  async fetch(request, env) {
    const url = new URL(request.url);

    // Determine which service based on path
    if (url.pathname.startsWith('/claude')) {
      // Remove /claude prefix, forward to Anthropic
      url.pathname = url.pathname.replace('/claude', '');
      url.hostname = 'api.anthropic.com';
    } else {
      // Default is OpenAI
      url.hostname = 'api.openai.com';
    }

    const newRequest = new Request(url, {
      method: request.method,
      headers: request.headers,
      body: request.body
    });

    const response = await fetch(newRequest);
    const newResponse = new Response(response.body, response);
    newResponse.headers.set('Access-Control-Allow-Origin', '*');

    return newResponse;
  }
};

Now accessing /claude/v1/messages will forward to the Claude API.

Gemini API Proxy

Google’s Gemini API endpoint is generativelanguage.googleapis.com. Just add another condition:

if (url.pathname.startsWith('/gemini')) {
  url.pathname = url.pathname.replace('/gemini', '');
  url.hostname = 'generativelanguage.googleapis.com';
}

Now one Worker can proxy three AI services.

Security Best Practices

Don’t Hardcode API Keys

You might see some tutorials putting API Keys directly in Worker code—don’t do this! Code is stored in plain text and might accidentally get shared.

The correct approach is using environment variables. In Worker settings, find “Variables and Secrets” and add an environment variable:

  • Name: OPENAI_API_KEY
  • Value: Your API Key
  • Type: Select “Secret” (encrypted storage)

Then use it in code like this:

export default {
  async fetch(request, env) {
    // Read API Key from environment variables
    const apiKey = env.OPENAI_API_KEY;

    // Modify request headers, add API Key
    const headers = new Headers(request.headers);
    headers.set('Authorization', `Bearer ${apiKey}`);

    // Rest of the code same as before...
  }
};

This way, the frontend doesn’t need to pass the API Key—much safer.

Add Custom Authentication Token

If you’re worried about someone discovering and abusing your Worker URL, add a simple authentication layer:

export default {
  async fetch(request, env) {
    // Check custom Token
    const authToken = request.headers.get('X-Custom-Auth');
    if (authToken !== env.MY_SECRET_TOKEN) {
      return new Response('Unauthorized', { status: 401 });
    }

    // Authentication passed, continue processing request...
  }
};

Set MY_SECRET_TOKEN in environment variables, and include this custom header when the frontend calls.

Monitor Usage

The Cloudflare console has an Analytics tab showing daily request volume, error rates, and other data. Check it regularly to catch any issues before exceeding the free quota.

You can also set up alerts: In “Notifications,” create a rule to email you when request volume approaches 100K.

Common Issues and Solutions

Slow Requests or Timeouts

If responses are particularly slow, the Worker might be assigned to a less-than-ideal node.

Solution: Bind a custom domain. Cloudflare optimizes routing based on your domain’s DNS configuration, usually faster than the free .workers.dev domain.

In Worker settings, select “Triggers” → “Add Custom Domain,” enter your domain (like api.yourdomain.com), and follow the prompts to add DNS records.

403 or 401 Errors

This usually indicates API Key issues:

  1. Check if the environment variable Key name matches what’s in the code
  2. Confirm the API Key is valid and has credit
  3. Check if OpenAI/Claude has regional restrictions (though Workers are globally distributed, some nodes might be identified)

Debugging tip: Add some logging in the code:

console.log('API Key:', env.OPENAI_API_KEY ? 'Set' : 'Not set');

Then check real-time logs in the Worker’s “Logs” tab.

What If Free Quota Isn’t Enough

If you find 100K really isn’t enough (like for commercial projects), consider the paid plan:

  • Workers paid plan: $5/month, includes 10 million requests
  • Overage: $0.50 per 1 million requests

Honestly, for small to medium applications, this is more cost-effective than buying your own VPS. Plus you don’t worry about server maintenance—the time saved is worth more.

Optimization tips:

  1. Add frontend caching—don’t repeat identical requests
  2. Use batch interfaces (if the API supports it) to reduce request count
  3. Use mock data during development instead of always calling the real API

Conclusion

After all that, the Workers proxy solution has three core advantages:

  • Zero Cost: Free quota is more than enough for individual development
  • Zero Barrier: 5-minute configuration, less than 30 lines of code
  • Zero Risk: API Key stored securely, no leaks

This solution is particularly suitable for personal learning, demo development, and small projects. If you’re also looking for stable AI API access, definitely try Workers.

Go set one up now! Bookmark this article and come back if you run into issues. If you encounter other pitfalls during setup, share them in the comments—I’d love to know what else can be optimized.

By the way, the open-source projects mentioned in the article are all excellent, especially chatgptProxyAPI and worker-openai-proxy. The code is very clear—check them out on GitHub.

What solution are you currently using to access AI APIs? Let’s chat in the comments!

FAQ

How do I create a Cloudflare Workers proxy for OpenAI?
Creating a Workers proxy takes 5 minutes: 1) Log into Cloudflare, go to Workers & Pages, click Create Worker, 2) Replace the default code with ~50 lines of proxy code that replaces the request hostname with 'api.openai.com' and handles CORS, 3) Click Deploy to get your permanent .workers.dev URL. Test with curl using your OpenAI API key - if you see a normal response, you're done. The free plan includes 100K requests/day which is sufficient for personal development.
Is it safe to use Cloudflare Workers as an API proxy?
Yes, when implemented correctly it's very safe. Never hardcode API keys in your Worker code - instead use Cloudflare's environment variables with 'Secret' type for encrypted storage. The frontend only calls your Worker URL while actual API keys stay secure in Cloudflare's environment. For additional security, add custom authentication tokens via X-Custom-Auth headers. Workers run on Cloudflare's infrastructure which is highly secure, and you can monitor usage via Analytics to detect any abuse.
Can one Worker proxy multiple AI services like OpenAI, Claude, and Gemini?
Yes, modify the code to check the request path and route accordingly. For example, use url.hostname = 'api.openai.com' for default paths, 'api.anthropic.com' for paths starting with /claude, and 'generativelanguage.googleapis.com' for paths starting with /gemini. This way a single Worker can proxy all three services - just change the path in your frontend calls to switch between providers.
What if I exceed the 100K daily request free quota?
Workers automatically switches to paid mode when you exceed the free quota. The paid plan costs $5/month for 10 million requests, with overage at $0.50 per 1 million requests - still very cost-effective compared to buying your own VPS. To optimize quota usage: 1) Add frontend caching to avoid duplicate requests, 2) Use batch interfaces where supported, 3) Use mock data during development instead of always calling real APIs. You can also set up alerts in Cloudflare Notifications to warn you when approaching the 100K limit.
Why are my Worker proxy requests slow or timing out?
Slow responses usually mean your Worker is on a suboptimal node. Solution: Bind a custom domain to your Worker - Cloudflare optimizes routing based on your domain's DNS configuration, typically faster than the free .workers.dev domain. Go to Worker Settings → Triggers → Add Custom Domain, enter your domain (like api.yourdomain.com), and follow prompts to add DNS records. Also check that your API key is valid and has credit, and review the Worker Logs tab for any error messages.

7 min read · Published on: Dec 1, 2025 · Modified on: Jan 22, 2026

Comments

Sign in with GitHub to leave a comment

Related Posts