SDKs overview
The UK Carbon Intensity API offers programmatic access to real-time and forecasted carbon intensity data across various regions in Great Britain. This data indicates the carbon dioxide emissions per unit of electricity consumed, providing insights for optimizing energy usage and developing environmentally conscious applications. The API does not require authentication, simplifying integration for developers. While official SDKs are provided for common languages, the API's RESTful nature allows for direct HTTP requests from any programming environment, as documented in the UK Carbon Intensity data API reference. The availability of both official and community-driven libraries aims to streamline development workflows, abstracting the underlying HTTP requests and data parsing.
Developers frequently utilize this data for applications ranging from smart home energy management systems to large-scale industrial energy optimization, and for academic research into energy consumption patterns and environmental impact. The API's straightforward design, as noted in the official API documentation, contributes to a accessible developer experience.
Official SDKs by language
The UK Carbon Intensity API primarily supports development through direct API calls, but it also provides official client libraries to facilitate integration. These libraries are designed to handle the HTTP requests and JSON parsing, allowing developers to focus on application logic. The primary languages with official support are Python and JavaScript, reflecting common choices for data-driven and web-based applications.
| Language | Package Name | Maturity | Description |
|---|---|---|---|
| Python | carbonintensity |
Stable | A Python client for accessing real-time and forecasted UK carbon intensity data. It provides methods for fetching intensity, generation mix, and regional data. |
| JavaScript | carbon-intensity-api |
Stable | A JavaScript client library suitable for Node.js and browser environments, designed to fetch carbon intensity and generation data from the API. |
Installation
Installing the official SDKs for UK Carbon Intensity typically involves using standard package managers for Python and JavaScript. The following sections detail the common installation methods.
Python
For Python projects, the carbonintensity library can be installed using pip, the Python package installer. It is recommended to install libraries within a virtual environment to manage dependencies effectively.
pip install carbonintensity
After installation, the library can be imported into Python scripts to begin making API requests. For detailed usage instructions and available methods, refer to the UK Carbon Intensity API documentation.
JavaScript
For JavaScript projects, whether for Node.js backend services or frontend web applications, the carbon-intensity-api package can be installed via npm (Node Package Manager) or yarn.
Using npm:
npm install carbon-intensity-api
Using yarn:
yarn add carbon-intensity-api
Once installed, the library can be imported using ES modules or CommonJS syntax, depending on the project configuration. The Mozilla Developer Network's JavaScript modules guide provides further information on module systems.
Quickstart example
The following examples demonstrate how to retrieve the current carbon intensity for Great Britain using the official Python and JavaScript SDKs. These snippets illustrate basic API interaction and data retrieval.
Python quickstart
This Python example fetches the current carbon intensity for the entire Great Britain region.
import carbonintensity
# Initialize the client
api = carbonintensity.CarbonIntensity()
# Get current carbon intensity for Great Britain
try:
current_intensity = api.get_current_intensity()
if current_intensity:
print(f"Current Carbon Intensity for GB: {current_intensity['data'][0]['intensity']['actual']} gCO2/kWh")
print(f"Forecast: {current_intensity['data'][0]['intensity']['forecast']} gCO2/kWh")
print(f"Index: {current_intensity['data'][0]['intensity']['index']}")
else:
print("Could not retrieve current intensity.")
except Exception as e:
print(f"An error occurred: {e}")
JavaScript quickstart (Node.js)
This Node.js example uses the JavaScript SDK to retrieve the current carbon intensity data.
const { CarbonIntensityAPI } = require('carbon-intensity-api');
async function getCurrentCarbonIntensity() {
try {
const api = new CarbonIntensityAPI();
const data = await api.getIntensity();
if (data && data.data && data.data.length > 0) {
const current = data.data[0];
console.log(`Current Carbon Intensity for GB: ${current.intensity.actual} gCO2/kWh`);
console.log(`Forecast: ${current.intensity.forecast} gCO2/kWh`);
console.log(`Index: ${current.intensity.index}`);
} else {
console.log('No carbon intensity data available.');
}
} catch (error) {
console.error('Error fetching carbon intensity:', error);
}
}
getCurrentCarbonIntensity();
Community libraries
Beyond the officially supported SDKs, the open-source community has developed various libraries and integrations that leverage the UK Carbon Intensity API. These community contributions often extend functionality, provide wrappers for additional languages, or integrate the data into specific platforms or frameworks. While not officially maintained, they can offer valuable tools for developers working in diverse environments.
Community libraries may include:
- Go/Golang wrappers: Projects that provide idiomatic Go clients for the API, often found on platforms like GitHub. These typically use Go's standard HTTP client to fetch and parse JSON responses.
- PHP clients: Libraries designed for PHP applications, allowing server-side integration of carbon intensity data into web applications built with frameworks like Laravel or Symfony.
- Rust crates: Community-driven Rust libraries that provide safe and efficient bindings to the API, appealing to developers focused on performance and reliability.
- Home automation integrations: Scripts or plugins for systems like Home Assistant or OpenHAB that use carbon intensity data to automate energy-consuming devices during periods of lower carbon intensity.
- Data visualization tools: Projects that consume the API data and provide ready-made dashboards or components for visualizing carbon intensity trends.
Developers seeking community resources are advised to search public code repositories and forums for relevant projects. When using community-maintained libraries, it is important to review their documentation, licensing, and active maintenance status. Projects like those found on Google's Open Source initiatives page highlight the collaborative nature of such development.
The UK Carbon Intensity API's open and unauthenticated nature encourages such community development, as developers can freely experiment and build tools without needing to manage API keys or complex authentication flows. This fosters a broader ecosystem of applications and services that benefit from real-time environmental data.