The browser cannot always call another site's API directly (CORS), and an API key must never be in page code. Use a Pages Function / Worker in between. The page then calls only its own /api/download endpoint.
// functions/api/download.js
export async function onRequestPost({ request, env }) {
const { url, downloadMode, videoQuality, audioFormat, audioBitrate, filenameStyle } = await request.json();
const r = await fetch(env.COBALT_URL + "/", {
method: "POST",
headers: { "Accept": "application/json", "Content-Type": "application/json",
...(env.COBALT_KEY ? { "Authorization": "Api-Key " + env.COBALT_KEY } : {}) },
body: JSON.stringify({ url, downloadMode, videoQuality, audioFormat, audioBitrate, filenameStyle })
});
return new Response(await r.text(), { headers: { "Content-Type": "application/json" } });
}Store COBALT_URL and the key in Cloudflare Dashboard → Settings → Variables / Secrets.