Energy Efficient Homes Save More- Year after Year
Tell us more about what you’d like to achieve
/**
* Watt Footprint - Brent Crude price proxy
*
* WHAT THIS DOES
* Your website widget calls this Worker. This Worker calls OilPriceAPI
* with your secret key, then sends back just the price data. The key
* never touches the browser, so it can't be copied out of your page source.
*
* SETUP (about 10 minutes, no card needed for the free tier)
* 1. Get a free API key at https://www.oilpriceapi.com
* 2. Go to https://dash.cloudflare.com, sign up free, click Workers & Pages
* 3. Create a new Worker, paste this whole file in, replacing the default code
* 4. In the Worker settings, add an environment variable/secret:
* name: OIL_API_KEY
* value: (your key from step 1)
* 5. Deploy. You'll get a URL like:
* https://oil-price-proxy.YOUR-SUBDOMAIN.workers.dev
* 6. Put that URL into the PROXY_URL constant near the top of the widget code
* 7. In the CORS line below, replace the placeholder with your real website domain
*/
const ALLOWED_ORIGIN = "https://www.wattfootprint.ie"; // change to your real domain
export default {
async fetch(request, env) {
// Only allow requests from your own site
const origin = request.headers.get("Origin") || "";
const corsHeaders = {
"Access-Control-Allow-Origin": ALLOWED_ORIGIN,
"Access-Control-Allow-Methods": "GET",
};
if (request.method === "OPTIONS") {
return new Response(null, { headers: corsHeaders });
}
try {
const apiResponse = await fetch(
"https://api.oilpriceapi.com/v1/prices/latest?by_code=BRENT_CRUDE_USD",
{
headers: {
Authorization: `Token ${env.OIL_API_KEY}`,
"Content-Type": "application/json",
},
}
);
if (!apiResponse.ok) {
return new Response(
JSON.stringify({ error: "Could not fetch oil price" }),
{ status: 502, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
const data = await apiResponse.json();
return new Response(JSON.stringify(data), {
headers: { ...corsHeaders, "Content-Type": "application/json" },
});
} catch (err) {
return new Response(
JSON.stringify({ error: "Proxy error" }),
{ status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
);
}
},
};
Email: info@wattfootprint.com
Phone: (01) 912 5490