We’ve recently released an update to our carbon.txt python library to allow developers to create carbon.txt files programmatically and integrate carbon.txt support in their own applications. Read on to find out how to make use of it, and to see how we integrated it to help verified hosting providers create a carbon.txt for their organization with a single click.
Carbon.txt is our proposed standard for a single recognisable location on any web domain – e.g. /carbon.txt – for public sustainability data relating to that company. Our vision is to help companies make their sustainability claims more standardised, transparent and discoverable.
We’ve created a publicly accessible builder and validator for carbon.txt files on carbontxt.org, using our open-source carbon.txt python library as a basis. To date these tools have provided manual methods for creating files.
As our project matures, we’ve turned our attention to exploring how to support programmatic creation of carbon.txt files. This is an important use case for making it easy for other developers to integrate carbon.txt in their own applications, content management systems and web frameworks, and therefore make it easier to adopt the ideas of carbon.txt into new contexts.
We’re pleased to have extended the aforementioned carbon.txt python library to provide new functionality to programmatically create a carbon.txt file from directly inside other applications: documentation here.
Using the library
If you’re a python developer who wants to use this functionality in your own applications, install the carbon-txt package using pip, then use the build_carbontxt_file function to build a carbon.txt file object from your data:
from carbon_txt import build_carbontxt_file
data = {
"org": {
"disclosures": [{"url": "https://www.example.com", "doc_type": "web-page"}]
},
"upstream": {
"services": [
{"domain": "example.com", "service_type": "virtual-private-servers"}
]
},
}
file = build_carbontxt_file(data)The build_carbontxt_file function gives you a carbon text file object, which you can output as a string, or save to a file.
file.to_toml()
#=> string with contents of TOML file
file.save_toml("/tmp/carbon.txt")
#=> saves the TOML file to the given pathYou can read all the details about how it works on our carbon.txt python library documentation site.
If you’re a developer who’s interested in integrating carbon.txt, and especially if you’re the developer of a content management system or web framework, we’d love to hear from you! Please get in touch – let us know how you want to use carbon.txt and how we might be able to help. We’d also be interested to hear what other languages and technologies we should be using. For any feedback and ideas, open an issue on our carbon.txt git repository.
A case study: building carbon.txt files for Green Web verified hosting providers.
As a result of our work last year to use carbon.txt to link individual domains to verified providers in our platform, we saw an opportunity to make this process simpler for our registered providers.
Our existing implementation required providers to create and publish a carbon.txt file which collated all their public sustainability disclosures – however, in many cases, we already had data about these disclosures on file, provided as part of our verification process. Wouldn’t it be better, we thought, to use the information already provided to automatically create a carbon.txt file for each provider, which they could simply upload to their domain, rather than requiring them to re-enter the same information they’d already provided to us.
This would solve a problem we encountered while carrying out user research with providers earlier in the year: a friction was that making sustainability disclosures with carbon.txt required a mix of skills and knowledge typically distributed among different members of a team. While a sustainability specialist or communications person might be the best placed to identify and gather the particular information that a carbon.txt file should disclose, they would need to rely on help from a developer, webmaster or system administrator to create the carbon.txt file and upload it. By using the python library to create a carbon.txt from the sustainability disclosures we’d already gathered for the provider, uploading the carbon.txt file became a simple task which can be carried out by anyone with sufficient technical knowledge and access.
The result is now live on our platform. If you’re a verified provider with us and have provided public disclosures as part of the verification process, as you go through the carbon.txt setup process, you will now be given the option to directly download a pre-populated carbon.txt file for your domain.

Appreciations
Finally, we’d like to thank Thibaud Colas, of Torchbox and Wagtail, for providing us with valuable input and code review on the new features inside our carbon.txt python library.
