diff --git a/src/_includes/partials/carbon-txt-endpoint.njk b/src/_includes/partials/carbon-txt-endpoint.njk new file mode 100644 index 0000000..48b40d2 --- /dev/null +++ b/src/_includes/partials/carbon-txt-endpoint.njk @@ -0,0 +1,7 @@ +## Endpoint + +A `POST` request should be sent to `https://carbon-txt-api.greenweb.org/api/validate/{{endpoint}}`. + +## Authentication + +The request header should include `x-api-key: my-api-key`, where `my-api-key` is your Green Web Foundation API key. [Learn more about API authentication](/api/carbon-txt/authentication). diff --git a/src/_includes/partials/carbon-txt-overview.njk b/src/_includes/partials/carbon-txt-overview.njk new file mode 100644 index 0000000..e5221ff --- /dev/null +++ b/src/_includes/partials/carbon-txt-overview.njk @@ -0,0 +1,10 @@ +[carbon.txt](https://carbontxt.org) makes sustainability data easier to discover and use on the web. Carbon.txt is a single, discoverable location on any web domain for public, machine-readable, sustainability data relating to that company. + +It’s a web-first, *connect not collect* style approach, of most benefit to those interested in scraping the structured data companies have to publish according to national laws. Designed to be extended by default, we see carbon.txt becoming essential infrastructure for sustainability data services crunching available numbers and sharing the stories it can tell. + +## Why carbon.txt? + +1. **Discovery of sustainability data continues to be a problem.** Our research with Wikirate made it abundantly clear that without new, webby approaches, sustainability data will continue to be hard to find or out of date. +1. **Changes in the law mean that lots of firms will need to publish all kinds of sustainability data that previously they didn’t have to.** The law literally says they need to publish this data online, free of charge for the public to see, and there are significant GDPR-scale fees for non-compliance. +1. **New standards mean that this data will be comparable, machine-readable, and likely across different parts of the world.** This could make verifying claims and identifying greenwashing easier, if it’s done right. +1. **Carbon.txt is our open-source project to make this sustainability information easier to discover.** Carbon.txt is a spec that defines predictable, consistent places on any website to publish sustainability data so that both humans and machines can find it. diff --git a/src/_includes/partials/carbon-txt-results.njk b/src/_includes/partials/carbon-txt-results.njk new file mode 100644 index 0000000..f491c35 --- /dev/null +++ b/src/_includes/partials/carbon-txt-results.njk @@ -0,0 +1,46 @@ +## Understanding the validator response. + +The validator will return a JSON response with either success or failure results. + +### Success + +{{ successCodeBlock | safe}} + + +### Failure + +Below is an example of an error returned due to incorrect `doc_type`. + +```json +{ + "success": false, + "errors": [ + { + "type": "literal_error", + "loc": [ + "org", + "disclosures", + 0, + "doc_type" + ], + "msg": "Input should be 'web-page', 'annual-report', 'sustainability-page', 'certificate', 'csrd-report', 'ai-model-card' or 'other'", + "input": "report", + "ctx": { + "expected": "'web-page', 'annual-report', 'sustainability-page', 'certificate', 'csrd-report', 'ai-model-card' or 'other'" + }, + "url": "https://errors.pydantic.dev/2.13/v/literal_error" + } + ], + "logs": [ + "Attempting to validate contents of version=\"0.5\"\nlast_updated=2026-06-01\n\n[", + "Carbon.txt file parsed as valid TOML.", + "Validation failed.", + "Validation error: 1 validation error for CarbonTxtFile\norg.disclosures.0.doc_type\n Input should be 'web-page', 'annual-report', 'sustainability-page', 'certificate', 'csrd-report', 'ai-model-card' or 'other' [type=literal_error, input_value='report', input_type=str]\n For further information visit https://errors.pydantic.dev/2.13/v/literal_error" + ] +} +``` + +The object has the following fields: + - The `success` field returns `false`, indicating carbon.txt content was not succesfully parsed. + - The detials of errors found are serialized inside the `errors` array. + - An array of `logs` which detail the carbon.txt lookup and validation process carried out, for debugging purposes. diff --git a/src/docs/api/carbon-txt/authentication.md b/src/docs/api/carbon-txt/authentication.md new file mode 100644 index 0000000..21c1664 --- /dev/null +++ b/src/docs/api/carbon-txt/authentication.md @@ -0,0 +1,20 @@ +--- +libraryName: Carbon.txt validator API +title: Authentication +description: Use this API to lookup and validate carbon.txt files. +hasTabs: true +eleventyNavigation: + key: carbon-txt-api-auth + title: Authentication + order: 2 +--- + +# Authentication + +To use the carbon.txt validator API, you will need to authenticate using a Green Web Foundation API key, available by logging into the [Green Web Portal](https://admin.thegreenwebfoundation.org). Register or log into an existing account, then navigate to the API keys section in the main navigation - you will be asked to provide some information on your planned use case, and to accept our privacy policy before creating your first key. You may create a maximum of 3 API keys by default. + +Once you have created a key, you can use it to authenticate your requests to the carbon.txt validation API by passing the `X-Api-Key` header with your requests. By default, requests are rate limited to 2 per second per API key. + +If you need more keys, or a raised rate limit, please get in touch with our support team and we'll be happy to help. + +[Interactive OpenAPI documentation](https://carbon-txt-api.greenweb.org/api/docs) for the API is also available, and has full details of all request and response parameters. diff --git a/src/docs/api/carbon-txt/carbon-txt.json b/src/docs/api/carbon-txt/carbon-txt.json new file mode 100644 index 0000000..4412abf --- /dev/null +++ b/src/docs/api/carbon-txt/carbon-txt.json @@ -0,0 +1,9 @@ +{ + "tags": ["carbon-txt"], + "name": "Carbon.txt validator API", + "permalink": "{% if not overridePermalink %}{% generateDocsPath page.filePathStem %}/index.html{% else %}{{ overridePermalink }}{% endif %}", + "layout": "layouts/doc.njk", + "repoVersion": "1", + "repo": "carbon-txt-validator", + "repository": "https://github.com/thegreenwebfoundation/carbon.-txt-validator" +} diff --git a/src/docs/api/carbon-txt/check-by-content.md b/src/docs/api/carbon-txt/check-by-content.md new file mode 100644 index 0000000..39284ba --- /dev/null +++ b/src/docs/api/carbon-txt/check-by-content.md @@ -0,0 +1,111 @@ +--- +libraryName: Carbon.txt validator API +title: Check by file content +description: Use this API to validate the contents of a carbon.txt file. +hasTabs: true +eleventyNavigation: + key: carbon-txt-api-check-by-content + title: Check by file content + order: 5 +--- + +# Validating carbon.txt file content + +To validate a carbon.txt file by uploading contents directly, make a `POST` request to the `/api/validate/file` endpoint, passing a JSON-encoded body with the carbon.txt file contents encoded as the `text_contents` parameter. + +{% set endpoint = 'file' %} +{% include 'partials/carbon-txt-endpoint.njk' %} + +## Payload + +The request should include a JSON-encoded body with the carbon.txt file contents encoded as the `text_contents` parameter. + + +
    +
  1. JavaScript
  2. +
  3. cURL
  4. +
+ +
+{% set code %} +fetch("https://carbon-txt-api.greenweb.org/api/validate/file", { + method: "POST", + headers: { + "X-Api-Key": gwf_xxxxxxx.xxxxxxxxxxxxxxxxx, + }, + body: JSON.stringify({ text_contents: version="0.5" + last_updated=2026-06-01 + + [org] + disclosures = [ + { doc_type="web-page", url="https://example.com" }, + ] + }) +}) +{% endset %} + +{% codeSnippet code, 'js' %} + +
+ +
+{% set code %} +curl -X POST \ + -H "Content-Type: application/json" \ + -H "X-Api-Key: gwf_xxxxxxx.xxxxxxxxxxxxxxxxx" \ + --data "{\"text_contents\": \"version='0.5'\n[org]\ndisclosures=[{ doc_type='web-page', url='https://example.com'}]\" }" \ + https://carbon-txt-api.greenweb.org/api/validate/file +{% endset %} + +{% codeSnippet code, 'curl' %} + +
+ +
+ +{% set successCodeBlock %} +```json +{ + "success": true, + "data": { + "version": "0.5", + "last_updated": null, + "upstream": null, + "org": { + "disclosures": [ + { + "doc_type": "web-page", + "url": "https://example.com", + "domain": null, + "valid_until": null, + "title": null + } + ] + } + }, + "logs": [ + "Attempting to validate contents of version='0.5'\n[org]\ndisclosures=[{ doc_t", + "Carbon.txt file parsed as valid TOML.", + "Parsed TOML was recognised as valid Carbon.txt file with syntax version 0.5.\n", + "ai-model-card_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_ai_model_card: Document type web-page seen. Doing nothing", + "csrd_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_csrd_document: Document type web-page seen. Doing nothing", + "ai-model-card_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_ai_model_card: Document type web-page seen. Doing nothing", + "csrd_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_csrd_document: Document type web-page seen. Doing nothing" + ], + "document_data": {} +} +``` + + +The object has the following fields: + - The `success` field returns `true`, indicating carbon.txt content was succesfully parsed. + - The contents of the carbon.txt file, serialized as JSON, inside the `data` field. + - An array of `logs` which detail the carbon.txt lookup and validation process carried out, for debugging purposes. + - A `document_data` object, containing any data parsed from linked documents by installed [carbon.txt plugins](https://carbon-txt-validator.readthedocs.io/en/latest/plugins.html). Currently the API provides plugins to parse [CSRD reports](https://finance.ec.europa.eu/financial-markets/company-reporting-and-auditing/company-reporting/corporate-sustainability-reporting_en) and [AI model cards](https://huggingface.co/docs/hub/model-cards). +{% endset %} + +{% include 'partials/carbon-txt-results.njk' %} diff --git a/src/docs/api/carbon-txt/check-by-domain.md b/src/docs/api/carbon-txt/check-by-domain.md new file mode 100644 index 0000000..c7fc348 --- /dev/null +++ b/src/docs/api/carbon-txt/check-by-domain.md @@ -0,0 +1,116 @@ +--- +libraryName: Carbon.txt validator API +title: Check by website domain +description: Use this API to validate the contents of a carbon.txt file hosted at a website domain. +hasTabs: true +eleventyNavigation: + key: carbon-txt-api-check-by-domain + title: Check by website domain + order: 4 +--- + +# Validating carbon.txt URL + +To validate a carbon.txt file by hosted at a public domain, make a `POST` request to the `/api/validate/domain` endpoint, passing a JSON-encoded body with the carbon.txt file contents encoded as the `domain` parameter. + +{% set endpoint = 'domain' %} +{% include 'partials/carbon-txt-endpoint.njk' %} + +## Payload + +The request should include a JSON-encoded body with the carbon.txt file contents encoded as the `domain` parameter. + + +
    +
  1. JavaScript
  2. +
  3. cURL
  4. +
+ +
+{% set code %} +fetch("https://carbon-txt-api.greenweb.org/api/validate/domain", { + method: "POST", + headers: { + "X-Api-Key": gwf_xxxxxxx.xxxxxxxxxxxxxxxxx, + }, + body: JSON.stringify({ domain: example.com }) +}) +{% endset %} + +{% codeSnippet code, 'js' %} + +
+ +
+{% set code %} +curl -X POST \ + -H "Content-Type: application/json" \ + -H "X-Api-Key: gwf_xxxxxxx.xxxxxxxxxxxxxxxxx" \ + --data "{\"domain\": \"example.com/\" }" \ + https://carbon-txt-api.greenweb.org/api/validate/domain +{% endset %} + +{% codeSnippet code, 'curl' %} + +
+
+ +
+
+
+

Important

+

Please note that the carbon.txt validator does not follow redirects. It will return a failure result if it encounters a redirect, and will flag this error in the logs.

+
+
+
+ + +{% set successCodeBlock %} +```json +{ + "success": true, + "url": "https://example.com/carbon.txt", + "data": { + "version": "0.5", + "last_updated": null, + "upstream": null, + "org": { + "disclosures": [ + { + "doc_type": "web-page", + "url": "https://example.com", + "domain": null, + "valid_until": null, + "title": null + } + ] + } + }, + "logs": [ + "Attempting to validate contents of version='0.5'\n[org]\ndisclosures=[{ doc_t", + "Carbon.txt file parsed as valid TOML.", + "Parsed TOML was recognised as valid Carbon.txt file with syntax version 0.5.\n", + "ai-model-card_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_ai_model_card: Document type web-page seen. Doing nothing", + "csrd_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_csrd_document: Document type web-page seen. Doing nothing", + "ai-model-card_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_ai_model_card: Document type web-page seen. Doing nothing", + "csrd_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_csrd_document: Document type web-page seen. Doing nothing" + ], + "document_data": {} +} +``` + + +The object has the following fields: + - The `success` field returns `true`, indicating a carbon.txt file was found and succesfully parsed. + - A `url` field will be returned with the canonical url of the file found. + - The contents of the carbon.txt file, serialized as JSON, inside the `data` field. + - An array of `logs` which detail the carbon.txt lookup and validation process carried out, for debugging purposes. + - A `document_data` object, containing any data parsed from linked documents by installed [carbon.txt plugins](https://carbon-txt-validator.readthedocs.io/en/latest/plugins.html). Currently the API provides plugins to parse [CSRD reports](https://finance.ec.europa.eu/financial-markets/company-reporting-and-auditing/company-reporting/corporate-sustainability-reporting_en) and [AI model cards](https://huggingface.co/docs/hub/model-cards). + - If you requested a domain which delegates its carbon.txt file to another domain using a DNS TXT record or HTTP header, the `delegation_method` field will indicate the delegation method followed. +{% endset %} + +{% include 'partials/carbon-txt-results.njk' %} diff --git a/src/docs/api/carbon-txt/check-by-url.md b/src/docs/api/carbon-txt/check-by-url.md new file mode 100644 index 0000000..07c61ea --- /dev/null +++ b/src/docs/api/carbon-txt/check-by-url.md @@ -0,0 +1,115 @@ +--- +libraryName: Carbon.txt validator API +title: Check by carbon.txt URL +description: Use this API to validate the contents of a carbon.txt file hosted at a fixed URL. +hasTabs: true +eleventyNavigation: + key: carbon-txt-api-check-by-url + title: Check by carbon.txt URL + order: 4 +--- + +# Validating carbon.txt URL + +To validate a carbon.txt file by hosted at a public URL, make a `POST` request to the `/api/validate/url` endpoint, passing a JSON-encoded body with the carbon.txt file contents encoded as the `url` parameter. + +{% set endpoint = 'url' %} +{% include 'partials/carbon-txt-endpoint.njk' %} + +## Payload + +The request should include a JSON-encoded body with the carbon.txt file contents encoded as the `url` parameter. + + +
    +
  1. JavaScript
  2. +
  3. cURL
  4. +
+ +
+{% set code %} +fetch("https://carbon-txt-api.greenweb.org/api/validate/url", { + method: "POST", + headers: { + "X-Api-Key": gwf_xxxxxxx.xxxxxxxxxxxxxxxxx, + }, + body: JSON.stringify({ url: https://example.com/carbon.txt }) +}) +{% endset %} + +{% codeSnippet code, 'js' %} + +
+ +
+{% set code %} +curl -X POST \ + -H "Content-Type: application/json" \ + -H "X-Api-Key: gwf_xxxxxxx.xxxxxxxxxxxxxxxxx" \ + --data "{\"url\": \"https://example.com/carbon.txt\" }" \ + https://carbon-txt-api.greenweb.org/api/validate/url +{% endset %} + +{% codeSnippet code, 'curl' %} + +
+ +
+ +
+
+
+

Important

+

Please note that the carbon.txt validator does not follow redirects. It will return a failure result if it encounters a redirect, and will flag this error in the logs.

+
+
+
+ +{% set successCodeBlock %} +```json +{ + "success": true, + "url": "https://example.com/carbon.txt", + "data": { + "version": "0.5", + "last_updated": null, + "upstream": null, + "org": { + "disclosures": [ + { + "doc_type": "web-page", + "url": "https://example.com", + "domain": null, + "valid_until": null, + "title": null + } + ] + } + }, + "logs": [ + "Attempting to validate contents of version='0.5'\n[org]\ndisclosures=[{ doc_t", + "Carbon.txt file parsed as valid TOML.", + "Parsed TOML was recognised as valid Carbon.txt file with syntax version 0.5.\n", + "ai-model-card_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_ai_model_card: Document type web-page seen. Doing nothing", + "csrd_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_csrd_document: Document type web-page seen. Doing nothing", + "ai-model-card_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_ai_model_card: Document type web-page seen. Doing nothing", + "csrd_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_csrd_document: Document type web-page seen. Doing nothing" + ], + "document_data": {} +} +``` + + +The object has the following fields: + - The `success` field returns `true`, indicating a carbon.txt file was found and succesfully parsed. + - A `url` field will be returned with the canonical url of the file found. + - The contents of the carbon.txt file, serialized as JSON, inside the `data` field. + - An array of `logs` which detail the carbon.txt lookup and validation process carried out, for debugging purposes. + - A `document_data` object, containing any data parsed from linked documents by installed [carbon.txt plugins](https://carbon-txt-validator.readthedocs.io/en/latest/plugins.html). Currently the API provides plugins to parse [CSRD reports](https://finance.ec.europa.eu/financial-markets/company-reporting-and-auditing/company-reporting/corporate-sustainability-reporting_en) and [AI model cards](https://huggingface.co/docs/hub/model-cards). +{% endset %} + +{% include 'partials/carbon-txt-results.njk' %} diff --git a/src/docs/api/carbon-txt/overview.md b/src/docs/api/carbon-txt/overview.md new file mode 100644 index 0000000..c7abaf8 --- /dev/null +++ b/src/docs/api/carbon-txt/overview.md @@ -0,0 +1,98 @@ +--- +tags: apiMain +libraryName: Carbon.txt validator API +title: Overview +description: Use this API to lookup and validate carbon.txt files. +hasTabs: true +eleventyNavigation: + key: carbon-txt-api-overview + title: Overview + order: 1 +--- + +# Carbon.txt validator API + +The carbon.txt validator API allows you to validate the syntax and content of carbon.txt files, and to look up the carbon.txt file for a given domain. + +{% include 'partials/carbon-txt-overview.njk' %} + +## These docs + +The documentation in this section is aimed at developers looking to use the carbon.txt validator API provided by the Green Web Foundation to discover, validate, and parse carbon.txt files and their contents. General information about carbon.txt for a non-technical audience is available on the [carbon.txt website](https://carbontxt.org). + +This documentation covers: + +- [Authenticating](/api/carbon-txt-/authentication) requests made to the carbon.txt validator API +- Using the carbon.txt validator API to discover and parse carbon.txt files on a [given web domain](/api/carbon-txt-/check-by-domain) +- Using the carbon.txt validator API to discover and parse carbon.txt files at a [specific public URL](/api/carbon-txt-/check-by-url) +- Using the carbon.txt validator API to parse [carbon.txt content directly](/api/carbon-txt-/check-by-content) + +## Other carbon.txt Tools + +Below are a collection of tools that you can use to get started creating a carbon.txt file for your organisation, or to check the contents of an existing carbon.txt file. + +
+
+
+

+ + + +Quickstart guide?

+

Learn how to create a carbon.txt file for your organisation, upload it to your domain, and check that it is valid.

+
+
+
+ Go to guide +
+
+ + + +## Keep updated + +To follow the carbon.txt project, you can: + +Follow us on LinkedIn +Subscribe to our montlhy newsletter diff --git a/src/docs/carbon-txt/overview.md b/src/docs/carbon-txt/overview.md index fbb71d7..340c56d 100644 --- a/src/docs/carbon-txt/overview.md +++ b/src/docs/carbon-txt/overview.md @@ -11,20 +11,11 @@ eleventyNavigation: # Carbon.txt - Overview -carbon.txt makes sustainability data easier to discover and use on the web. Carbon.txt is a single, discoverable location on any web domain for public, machine-readable, sustainability data relating to that company. - -It’s a web-first, *connect not collect* style approach, of most benefit to those interested in scraping the structured data companies have to publish according to national laws. Designed to be extended by default, we see carbon.txt becoming essential infrastructure for sustainability data services crunching available numbers and sharing the stories it can tell. - -## Why carbon.txt? - -1. **Discovery of sustainability data continues to be a problem.** Our research with Wikirate made it abundantly clear that without new, webby approaches, sustainability data will continue to be hard to find or out of date. -1. **Changes in the law mean that lots of firms will need to publish all kinds of sustainability data that previously they didn’t have to.** The law literally says they need to publish this data online, free of charge for the public to see, and there are significant GDPR-scale fees for non-compliance. -1. **New standards mean that this data will be comparable, machine-readable, and likely across different parts of the world.** This could make verifying claims and identifying greenwashing easier, if it’s done right. -1. **Carbon.txt is our open-source project to make this sustainability information easier to discover.** Carbon.txt is a spec that defines predictable, consistent places on any website to publish sustainability data so that both humans and machines can find it. +{% include 'partials/carbon-txt-overview.njk' %} ## These docs -The documentation on this website is aimed at developers looking to build additional tooling that extends carbon.txt - primarily through plugins and other extensions to the [carbon.txt validator](https://github.com/thegreenwebfoundation/carbon-txt-validator). General information about carbon.txt for a non-technical audience is available on the carbon.txt website. +The documentation in this section is aimed at developers looking to build additional tooling that extends carbon.txt - primarily through plugins and other extensions to the [carbon.txt validator](https://github.com/thegreenwebfoundation/carbon-txt-validator). General information about carbon.txt for a non-technical audience is available on the [carbon.txt website](https://carbontxt.org). ## Other carbon.txt Tools @@ -76,10 +67,21 @@ Below are a collection of tools that you can use to get started creating a carbo -
  • +
  • -

    Syntax reference

    - Learn about all the details of the carbon.txt format from our syntax specification. +

    Validation API

    + Use our HTTP API to look up and validate carbon.txt files. + +
    +
  • +
  • +
    +
    +

    Syntax reference

    + Learn about all the details of the carbon.txt format from our syntax specification. +
    diff --git a/src/docs/co2js/tutorials/check-carbon-txt.md b/src/docs/co2js/tutorials/check-carbon-txt.md new file mode 100644 index 0000000..de01438 --- /dev/null +++ b/src/docs/co2js/tutorials/check-carbon-txt.md @@ -0,0 +1,162 @@ +--- +title: "Check a domain for carbon.txt" +description: "In this tutorial, you will install CO2.js in a Node environment. Then, you will check a domain to see if they are hosting a valid carbon.txt file." +eleventyNavigation: + key: check-carbon-txt + title: "Check a domain for carbon.txt" + # parent: overview + sectionTitle: Tutorials + order: 14 +--- + +# {{ title }} + +## Overview + +CO2.js comes with a handy function which lets you check if a web domain hosts a valid carbon.txt file. Under the hood, this function uses the [Greencheck API](/api/carbon-txt/overview/) to return results. You can learn more about the carbon.txt project at [https://carbontxt.org](https://carbontxt.org). + +In this tutorial, you will install CO2.js in a Node environment. Then, you will check a domain to see if it hosts a valid carbon.txt file. + +## Before starting + +You can follow along with this tutorial in your local development environment, or by using the button below to launch the project in Gitpod. + +[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/thegreenwebfoundation/gitpod-node-starter) + +### Local development + +If you are following along this tutorial locally, you will need to have the following setup on your machine: + +- Node version 14 or later +- NPM version 6 or later + +## Learning goals + +- How to install CO2.js using NPM +- How to lookup if a domain hosts a carbon.txt file + +## Setting up + +If you are following along using the Gitpod starter template, you can skip this section. + +Otherwise, create a new folder locally called `co2js-carbon-txt` and navigate into that folder. Then, initialise NPM. + +```bash +mkdir co2js-carbon-txt +cd co2js-carbon-txt +npm init -y +``` + +Next, create an `check.js` file, and open it in your code editor of choice. We will write the code for this tutorial inside the `check.js` file. + +## Installing CO2.js + +Inside your project folder, run the following command to install CO2.js as a dependency. + +```bash +npm install @tgwf/co2 +``` + +## Initialise CO2.js + +In your project’s `check.js` file, add the following code to include the CO2.js carbon-txt module's `check()` function in your code. + +```js +const { check } = require("@tgwf/co2/carbon-txt"); +``` + +## Lookup a domain to find a carbon.txt file + +To check if a single domain is green hosted, you can pass the following parameters into the `check` function: + +| Parameter | Type | Required | Description | +| --- | --- | --- | --- | +| domain | `string` | Required | the website domain you want to check for green hosting. | +| options | `object` | Required | An object of domain check options. | + +The `options` object may contain the following keys: + +| Key | Type | Required | Description | +| --- | --- | --- | --- | +| apiKey | `string` | Required | A valid Green Web Foundation API key. You can get one from the [Green Web Portal](http://admin.thegreenwebfoundation.org/provider-portal). | +| verbose | `boolean` | | indicate where you want to receive the full JSON payload from the API (including logs), or just the core data. Default: `false` | +| userAgentIdentifier | `string` | | representing the app, site, or organisation that is making the request. | +| customValidator | `string` | | representing the URL of the carbon.txt validator endpoint (if you do not want to use the Green Web Foundation carbon.txt API endpoint). | + +Adding the code below to the `check.js` file allows us to check if the domain `google.com` for a valid carbon.txt file and return the results of the parsed file if found. + +```js +const options = { + apiKey: "my-api-key", + verbose: false +}; +check("google.com", options).then((result) => { + console.log(result); +}); +``` + +Running the code above returns the following result: + +```bash +node check.js + +# Output: +# { +# success: false, +# errors: [ +# 'UnreachableCarbonTxtFile: Unable to find a valid carbon.txt file at the domain google.com' +# ] +# } +``` + +If you want to get the full logs from the carbon.txt validator, you can change set `verbose: true` in the options. + +{% set successCodeBlock %} +```json +{ + "success": true, + "url": "https://example.com/carbon.txt", + "data": { + "version": "0.5", + "last_updated": null, + "upstream": null, + "org": { + "disclosures": [ + { + "doc_type": "web-page", + "url": "https://example.com", + "domain": null, + "valid_until": null, + "title": null + } + ] + } + }, + "logs": [ + "Attempting to validate contents of version='0.5'\n[org]\ndisclosures=[{ doc_t", + "Carbon.txt file parsed as valid TOML.", + "Parsed TOML was recognised as valid Carbon.txt file with syntax version 0.5.\n", + "ai-model-card_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_ai_model_card: Document type web-page seen. Doing nothing", + "csrd_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_csrd_document: Document type web-page seen. Doing nothing", + "ai-model-card_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_ai_model_card: Document type web-page seen. Doing nothing", + "csrd_greenweb: Processing supporting document: https://example.com for None", + "carbon_txt.process_csrd_document: Document type web-page seen. Doing nothing" + ], + "document_data": {} +} +``` + + +The object has the following fields: + - The `success` field returns `true`, indicating a carbon.txt file was found and succesfully parsed. + - A `url` field will be returned with the canonical url of the file found. + - The contents of the carbon.txt file, serialized as JSON, inside the `data` field. + - An array of `logs` which detail the carbon.txt lookup and validation process carried out, for debugging purposes. + - A `document_data` object, containing any data parsed from linked documents by installed [carbon.txt plugins](https://carbon-txt-validator.readthedocs.io/en/latest/plugins.html). Currently the API provides plugins to parse [CSRD reports](https://finance.ec.europa.eu/financial-markets/company-reporting-and-auditing/company-reporting/corporate-sustainability-reporting_en) and [AI model cards](https://huggingface.co/docs/hub/model-cards). + - If you requested a domain which delegates its carbon.txt file to another domain using a DNS TXT record or HTTP header, the `delegation_method` field will indicate the delegation method followed. +{% endset %} + +{% include 'partials/carbon-txt-results.njk' %}