Paste this into a Worker. Then use https://YOUR_DOMAIN/api/proxy as the Proxy base URL.
export default {
async fetch(request, env) {
const url = new URL(request.url);
const to = url.searchParams.get("to");
if (!to) return new Response("Missing ?to=", { status: 400 });
if (request.method === "OPTIONS") {
return new Response(null, { status: 204, headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,POST,OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization, x-api-key, anthropic-version, anthropic-dangerous-direct-browser-access, xi-api-key",
}});
}
const headers = new Headers(request.headers);
headers.delete("host");
const resp = await fetch(to, {
method: request.method,
headers,
body: ["GET","HEAD"].includes(request.method) ? undefined : await request.arrayBuffer(),
});
const out = new Response(resp.body, resp);
out.headers.set("Access-Control-Allow-Origin", "*");
out.headers.set("Access-Control-Expose-Headers", "*");
return out;
}
}