Fetch
Use these code examples for bypassing CORS errors in Fetch requests with Corsfix CORS proxy.
GET Request
Section titled “GET Request”fetch("https://proxy.corsfix.com/?https://api.example.com/data");Cached Response
Section titled “Cached Response”fetch("https://proxy.corsfix.com/?https://api.example.com/data", { headers: { "x-corsfix-cache": "15m", },});POST Request
Section titled “POST Request”fetch("https://proxy.corsfix.com/?https://api.example.com/data", { method: "POST", body: JSON.stringify({ data: "mydata", }),});Using Secrets
Section titled “Using Secrets”fetch( "https://proxy.corsfix.com/?" + "https://api.example.com/data?key={{SECRET_KEY}}", { method: "POST", headers: { Authorization: "Bearer {{SECRET_TOKEN}}", }, body: JSON.stringify({ data: "mydata", }), });Header Override
Section titled “Header Override”fetch("https://proxy.corsfix.com/?https://api.example.com/data", { headers: { "x-corsfix-headers": JSON.stringify({ "User-Agent": "MyAgent/1.0", }), },});If you would rather not build the proxy URL yourself, the Corsfix SDK wraps fetch for you. It takes the same arguments as fetch, with Corsfix features in the corsfix option.
import corsfix from "corsfix";
corsfix.fetch("https://api.example.com/data", { corsfix: { cache: "15m" },});