Skip to main content
Cloudflare Workers run at the edge, providing low-latency proxying with minimal configuration. This is the recommended approach if you already use Cloudflare for DNS.

Prerequisites

  • A Cloudflare account
  • A domain managed by Cloudflare (or external DNS pointing to Cloudflare)
  • Access to create Workers in your Cloudflare dashboard

Setup Guide

1

Create a new Worker

  1. Log in to your Cloudflare dashboard
  2. Navigate to Workers & Pages in the sidebar
  3. Click Create applicationCreate Worker
  4. Give your worker a name (e.g., analytics-proxy)
  5. Click Deploy to create the initial worker
2

Add the Worker code

Click Edit code and replace the contents with:
const COMPOSITE_API = 'https://prod.alb.us.api.composite.com';

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

    // Only handle /analytics paths
    if (!url.pathname.startsWith('/analytics')) {
      return new Response('Not Found', { status: 404 });
    }

    // Forward to Composite API with path intact
    const targetUrl = `${COMPOSITE_API}${url.pathname}${url.search}`;

    const modifiedRequest = new Request(targetUrl, {
      method: request.method,
      headers: request.headers,
      body: request.body,
      redirect: 'follow'
    });

    // Add forwarding headers
    const clientIP = request.headers.get('CF-Connecting-IP');
    modifiedRequest.headers.set('X-Forwarded-For', clientIP);
    modifiedRequest.headers.set('X-Forwarded-Proto', 'https');
    modifiedRequest.headers.set('X-Real-IP', clientIP);

    // Forward the request
    const response = await fetch(modifiedRequest);

    // Return response with CORS headers for browser extensions
    const corsHeaders = {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
      'Access-Control-Allow-Headers': 'Content-Type, Authorization'
    };

    // Handle preflight requests
    if (request.method === 'OPTIONS') {
      return new Response(null, { headers: corsHeaders });
    }

    // Clone response and add CORS headers
    const newResponse = new Response(response.body, response);
    Object.entries(corsHeaders).forEach(([key, value]) => {
      newResponse.headers.set(key, value);
    });

    return newResponse;
  }
};
Click Save and deploy.
3

Add a route to your domain

  1. Go to your Worker’s SettingsDomains & Routes
  2. Click AddRoute
  3. Enter the route pattern: yourdomain.com/analytics/*
  4. Select your zone
  5. Click Add route
4

Configure your SDK

Update your extension to use the custom endpoint:
await composite.init({
  apiKey: 'pk_your_api_key',
  apiHost: 'https://yourdomain.com/analytics',
  transport: 'chrome-extension'
});
Update your manifest to only require your domain:
"host_permissions": [
  "https://yourdomain.com/*"
]

Test Your Worker

Before configuring your SDK, verify the Worker is routing requests correctly:
  1. Open your browser and navigate to:
    https://yourdomain.com/analytics/health
    
  2. You should see a response from the Composite API (or a valid error response, not a Cloudflare error page)
  3. Check the Network tab in DevTools to confirm:
    • The request reaches your Worker
    • The response headers include your CORS headers
    • No redirect loops occur
You can also test using the Workers preview URL before adding a route:
https://analytics-proxy.your-account.workers.dev/analytics/health

Resolve DNS Conflicts

If your Worker route isn’t handling requests, you may have conflicting DNS records.
1

Check for existing records

  1. Go to your Cloudflare dashboard → DNSRecords
  2. Look for any A, AAAA, or CNAME records for your root domain or the path you’re using
2

Delete or disable conflicting records

If you have existing records that might intercept traffic before it reaches your Worker:
  1. Delete the conflicting record, or
  2. Disable the Proxy toggle (orange cloud → grey cloud) to let the Worker handle routing
Only delete records you’re sure aren’t needed. If your domain serves other content, be careful not to break existing functionality.
3

Verify the route takes precedence

After clearing conflicts, test your Worker again. Routes with explicit path patterns (/analytics/*) should now receive traffic.
DNS changes typically propagate within minutes, but can take up to 48 hours in rare cases.

Troubleshooting

Ensure your route pattern matches your URL structure: yourdomain.com/analytics/*Check the Worker is deployed and the route is active in Workers & PagesYour WorkerSettingsDomains & Routes.
Verify the Worker includes the CORS headers in the response. The example code above handles this, but if you’ve customized it, ensure Access-Control-Allow-Origin is set.For Chrome extensions, you may also need to add your extension’s origin to the allow list.
Check Cloudflare’s dashboard for Worker errors:
  1. Go to Workers & PagesYour WorkerLogs
  2. Look for any exceptions or failed requests
Ensure prod.alb.us.api.composite.com is accessible from Cloudflare’s network.

What’s Next?