agentleFS
shali10shali10/awesome-agent-skills/skills/cloudflare-worker-api-proxy/SKILL.md

cloudflare-worker-api-proxy

Skill1 starsChanged 2 months agoMITView on GitHub

Use when deploying or maintaining a zero-cost OpenAI/Anthropic-compatible API reverse proxy on Cloudflare Workers with custom auth, streaming, and rate-limiting.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Broken SSE streaming: Never read await request.text() or await response.text() when streaming (stream: true).

    line 81

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Zero-Cost Serverless OpenAI API Proxy on Cloudflare Workers

Overview

A high-performance, edge-native API proxy deployed on Cloudflare Workers. It enables zero-cost routing, token authentication, custom rate-limiting, and header rewriting for OpenAI, Claude, and Google Gemini API endpoints, bypassing regional network blocks with global edge caching and SSE streaming support.

When to Use & When NOT to Use

When to Use

  • Bypassing geographic IP restrictions on AI provider APIs (e.g. OpenAI / Anthropic).
  • Adding custom Bearer token authentication in front of self-hosted or shared LLM gateways.
  • Injecting custom user agents or security headers into upstream requests.

When NOT to Use

  • Heavy video/audio encoding or streaming workloads exceeding Cloudflare Worker CPU limits (50ms free / 30s paid).
  • Caching multi-gigabyte files (use Cloudflare R2 instead).

Production Worker Implementation

export default {
  async fetch(request, env) {
    const url = new URL(request.url);
    
    // 1. CORS Preflight
    if (request.method === "OPTIONS") {
      return new Response(null, {
        headers: {
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
          "Access-Control-Allow-Headers": "Content-Type, Authorization",
        }
      });
    }

    // 2. Upstream Target Configuration
    const UPSTREAM_TARGET = env.UPSTREAM_URL || "https://api.openai.com";
    const targetUrl = new URL(url.pathname + url.search, UPSTREAM_TARGET);

    // 3. Clone & Modify Headers
    const headers = new Headers(request.headers);
    headers.set("Host", targetUrl.hostname);
    if (env.INJECT_USER_AGENT) {
      headers.set("User-Agent", env.INJECT_USER_AGENT);
    }

    // 4. Proxy Request with Streaming
    const response = await fetch(targetUrl.toString(), {
      method: request.method,
      headers: headers,
      body: request.body,
      redirect: "follow"
    });

    // 5. Return Response with CORS
    const newHeaders = new Headers(response.headers);
    newHeaders.set("Access-Control-Allow-Origin": "*");
    return new Response(response.body, {
      status: response.status,
      statusText: response.statusText,
      headers: newHeaders
    });
  }
};

Common Pitfalls

  1. Broken SSE streaming: Never read await request.text() or await response.text() when streaming (stream: true); pipe response.body directly to preserve real-time tokens.
  2. Missing Host header rewrite: Upstream services behind Cloudflare will throw 403 / 1000 errors if the incoming Host header is not rewritten to match the target hostname.

Verification Checklist

  • Worker responds to GET /v1/models with HTTP 200/401 from upstream.
  • SSE streaming test via curl -N delivers chunks without buffering.
  • CORS headers are present on both OPTIONS preflight and normal responses.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.