CO2.js comes with a handy function which lets you check if one domain or an array of domains is served from a green web host. Under the hood, this function uses the Greencheck API to return results.
In this tutorial, you will install CO2.js in a Node environment. Then, you will check the green hosting status of one or more domains.
You can follow along with this tutorial in your local development environment, or by using the button below to launch the project in Gitpod.
If you are following along this tutorial locally, you will need to have the following setup on your machine:
If you are following along using the Gitpod starter template, you can skip this section.
Otherwise, create a new folder locally called co2js-node
and navigate into that folder. Then, initialise NPM.
mkdir co2js-node
cd co2js-node
npm init -y
Next, create an hosting.js
file, and open it in your code editor of choice. We will write the code for this tutorial inside the hosting.js
file.
Inside your project folder, run the following command to install CO2.js as a dependency.
npm install @tgwf/co2
In your project’s hosting.js
file, add the following code to include the CO2.js hosting module in your code.
const { hosting } = require("@tgwf/co2");
The hosting module includes a check()
function. We will be using this to perform our green hosting checks.
To check if a single domain is green hosted, you can pass the following parameters into the check
function:
string
the website domain you want to check for green hosting.object
Optional since v0.15.0 An object of domain check options. It may contain the following keys
boolean
indicate where you want to receive the full JSON payload from the API, or a simpler true/false result.string
representing the app, site, or organisation that is making the request.When checking a single domain, you can choose to receive the result as:
boolean
value (true
for green hosted, false
for not), orobject
containing the full JSON payload the is returned from the Green Web Dataset.The default behaviour of the check
function for a single domain in CO2.js is to return a boolean (true/false) response whenever a domain is queried. You can also explicitly set this by setting verbose: false
in the options that are passed into the function.
Adding the code below to the hosting.js
file allows us to check if the domain google.com
is served from a green web host and return a boolean response.
const options = {
verbose: false,
userAgentIdentifier: "myGreenApp",
};
hosting.check("google.com", options).then((result) => {
console.log(result);
});
Running the code above returns the following result:
node hosting.js
# Output:
# true
Alternately, you can choose to return a verbose response when checking a domain. This will return all the information available for that domain's hosting provider in the Green Web Dataset. The response will be returned as a JSON object. To do this, you will need to set verbose: true
in the options that are passed into the function.
Adding the code below to the hosting.js
file allows us to check if the domain google.com
is served from a green web host and return a boolean response.
const options = {
verbose: true,
userAgentIdentifier: "myGreenApp",
};
hosting.check("google.com", options).then((result) => {
console.log(result);
});
Running the code above returns the following result:
node hosting.js
# Output:
# {
# url: 'google.com',
# hosted_by: 'Google Inc.',
# hosted_by_website: 'https://www.google.com',
# partner: null,
# green: true,
# hosted_by_id: 595,
# modified: '2024-05-02T06:01:59',
# supporting_documents: [
# {
# id: 108,
# title: 'Sustainability at Google',
# link: 'https://sustainability.google'
# },
# {
# id: 139,
# title: 'Independent verification of Google 2020 Reporting',
# link: 'https://s3.nl-ams.scw.cloud/tgwf-web-app-live/uploads/Google_Cloud_-_3degrees_cloud_services_review_statement_final.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=SCWT1WBAW6NZ5SW5GYJ8%2F20240502%2Fnl-ams%2Fs3%2Faws4_request&X-Amz-Date=20240502T071323Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=1bc5b6f5bbe461b13fb694a7787fede307226386891d0b87ac1914fa95a27684'
# },
# ... results truncated for readability
# ]
# }
To check if more than one domain is green hosted, you can pass the following parameters into the check
function:
array
an array of strings representing website domain you want to check for green hosting.object
Optional since v0.15.0 An object of domain check options. It may contain the following keys
boolean
indicate where you want to receive the full JSON payload from the API, or a simpler true/false result.string
representing the app, site, or organisation that is making the request.When checking a multiple domains, you can choose to receive the result as:
array
of strings corresponding to any green domains found by the check, orobject
containing the full JSON payload the is returned from the Green Web Dataset.The default behaviour of the check
function for multiple domains in CO2.js is to return an array of strings corresponding to the queried domains that were identified as green in the Green Web Dataset. You can also explicitly set this by setting verbose: false
in the options that are passed into the function.
Adding the code below to the hosting.js
file allows us to check if the domains google.com
and pchome.com
are served from a green web host and return a boolean response.
const options = {
verbose: false,
userAgentIdentifier: "myGreenApp",
};
hosting.check(["google.com", "pchome.com"], options).then((result) => {
console.log(result);
});
Running the code above returns the following result:
node hosting.js
# Output:
# ["google.com"]
Alternately, you can choose to return a verbose response when checking multiple domains. This will return all the information available for those domain's hosting provider in the Green Web Dataset. The response will be returned as a JSON object. To do this, you will need to set verbose: true
in the options that are passed into the function.
Adding the code below to the hosting.js
file allows us to check if the domain google.com
is served from a green web host and return a boolean response.
const options = {
verbose: true,
userAgentIdentifier: "myGreenApp",
};
hosting.check(["google.com", "pchome.com"], options).then((result) => {
console.log(result);
});
Running the code above returns the following result:
node hosting.js
# Output:
# {
# "google.com": {
# url: 'google.com',
# hosted_by: 'Google Inc.',
# hosted_by_website: 'https://www.google.com',
# partner: null,
# green: true,
# hosted_by_id: 595,
# modified: '2024-05-02T06:01:59',
# supporting_documents: [
# {
# id: 108,
# title: 'Sustainability at Google',
# link: 'https://sustainability.google'
# },
# {
# id: 139,
# title: 'Independent verification of Google 2020 Reporting',
# link: 'https://s3.nl-ams.scw.cloud/tgwf-web-app-live/uploads/Google_Cloud_-_3degrees_cloud_services_review_statement_final.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=SCWT1WBAW6NZ5SW5GYJ8%2F20240502%2Fnl-ams%2Fs3%2Faws4_request&X-Amz-Date=20240502T071323Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=1bc5b6f5bbe461b13fb694a7787fede307226386891d0b87ac1914fa95a27684'
# },
# ... results truncated for readability
# ]
# },
# "pchome.com": {
# url: "pchome.com",
# hosted_by: null,
# hosted_by_website: null,
# partner: null,
# green: false,
# hosted_by_id: null,
# modified: "2024-05-02T07:16:10.504512"
# },
# }
You now know how to use CO2.js to check one or more domains for green hosting.
The domain/s passed to the check()
function must not include any protocol, port, or path information.
climateaction.tech
Acceptedhttps://climateaction.tech
Incorrectclimateaction.tech/events
IncorrectWhen a green hosting check is performed for a domain, we will return either green: true
(it's a green host) or green: false
(not a green host).
If you're expecting a green result, but not getting it, there are a few reasons why this might be happening. We've covered these in our FAQ - Why does my website show up as grey in the Green Web Checker?.