This package calculates the carbon emission by the network traffic from a given URL.
NPM
npm i website-carbon-calculator --save
YARN
yarn add website-carbon-calculator
2. Get you Google PageSpeed API Key here.
import { WebsiteCarbonCalculator, WebsiteCarbonCalculatorError } from 'website-carbon-calculator';
try {
const websiteCarbonCalculator = new WebsiteCarbonCalculator({pagespeedApiKey: '...'});
const result = websiteCarbonCalculator.calculateByURL('https://yourwebsite.com');
// {
// url: 'yourwebsite.com',
// bytesTransferred: 123456,
// isGreenHost: true,
// co2PerPageview: 0.1234567,
// }
} catch(error) {
if(error instanceof WebsiteCarbonCalculatorError){
console.warn(error.message);
}
// Do something else...
}
Check how the calc works here.
Help to maintain this project and become a sponsor on Github Sponsors, Ko-fi, or Buy Me A Coffee! 🎉 You can get your company logo, link & name on this file. It's also rendered on package page in npmjs.com and yarnpkg.com sites too! 🚀
See CONTRIBUTING.
Ricardo Dantas - @ricardodantas
This package based on the carbon emission calculator code available on the Website Carbon API by Wholegrain Digital.
MIT, see LICENSE