SDKs overview

Carbon Interface provides Software Development Kits (SDKs) and client libraries to facilitate integration with its RESTful API. These tools are designed to streamline the process of making API calls for carbon emission calculations, handling authentication, request formatting, and response parsing. Official SDKs are maintained by Carbon Interface, with community contributions expanding support to additional programming languages and platforms. The API itself follows REST principles, utilizing standard HTTP methods and JSON payloads for data exchange, consistent with widely adopted API design patterns Google Cloud REST API comparisons.

The core functionality of these SDKs revolves around accessing various Carbon Interface endpoints, such as those for calculating emissions from flight segments, shipping activities, and electricity consumption. Developers can use these SDKs to programmatically estimate the carbon footprint of diverse activities, integrating this data into applications, dashboards, or reporting tools. The SDKs abstract away the underlying HTTP requests, allowing developers to interact with the API using native language constructs.

Official SDKs by language

Carbon Interface maintains official SDKs for several popular programming languages, ensuring direct support and compatibility with their API. These SDKs are developed to provide idiomatic access to the API's features, including authentication and error handling. The primary languages with official SDK support include Python, Ruby, Node.js, Go, and PHP. Each SDK typically includes client classes or functions that map to the API's resources and actions, simplifying the process of sending data and receiving emission estimates.

The table below outlines the official SDKs, their respective package managers, and standard installation commands. Developers can refer to the Carbon Interface client libraries documentation for detailed setup instructions and usage examples for each language.

Language Package Manager / Package Install Command Maturity
Python pip install carboninterface pip install carboninterface Official, Stable
Ruby gem install carboninterface gem install carboninterface Official, Stable
Node.js npm install carboninterface npm install carboninterface Official, Stable
Go go get github.com/carboninterface/carboninterface-go go get github.com/carboninterface/carboninterface-go Official, Stable
PHP composer require carboninterface/carboninterface-php composer require carboninterface/carboninterface-php Official, Stable

Installation

Installation of Carbon Interface SDKs typically involves using the standard package manager for the target programming language. Each SDK is published to its respective ecosystem's package registry, ensuring a consistent and familiar installation process for developers. Prior to installation, developers should ensure they have the appropriate language runtime and package manager installed on their system. For example, Python SDKs require pip, while Node.js SDKs use npm or yarn. The installation process is generally a single command executed in the project's terminal.

Python

To install the Python SDK, use pip:

pip install carboninterface

Ruby

For Ruby projects, the SDK is installed via gem:

gem install carboninterface

Node.js

Node.js developers can install the SDK using npm or yarn:

npm install carboninterface
# or
yarn add carboninterface

Go

Go modules are used to install the Go SDK:

go get github.com/carboninterface/carboninterface-go

PHP

PHP projects typically use Composer for dependency management:

composer require carboninterface/carboninterface-php

After installation, the SDK can be imported and utilized in the respective programming environment. Developers should consult the Carbon Interface client libraries documentation for any specific environment configurations or setup steps beyond basic installation.

Quickstart example

The following quickstart example demonstrates how to use the Carbon Interface Python SDK to estimate the carbon emissions for a hypothetical flight. This example assumes that the carboninterface Python package has been installed and that an API key is available. API keys are required for authentication with the Carbon Interface API and should be kept secure, often managed via environment variables Twilio environment variable guide or secure configuration systems.

import os
from carboninterface.client import CarbonInterfaceClient

# Retrieve your Carbon Interface API key from an environment variable
# It's recommended to store API keys securely, not directly in code.
api_key = os.environ.get("CARBON_INTERFACE_API_KEY")

if not api_key:
    raise ValueError("CARBON_INTERFACE_API_KEY environment variable not set.")

client = CarbonInterfaceClient(api_key=api_key)

try:
    # Define the flight details for emission estimation
    flight_estimate_data = {
        "type": "flight",
        "segments": [
            {
                "departure_airport": "sfo",
                "destination_airport": "nyc"
            },
            {
                "departure_airport": "nyc",
                "destination_airport": "sfo"
            }
        ]
    }

    # Make the API call to get the flight carbon estimate
    estimate = client.get_flight_estimate(flight_estimate_data)

    # Print the estimated carbon emissions
    if estimate and estimate.get("data") and estimate["data"].get("attributes"):
        attributes = estimate["data"]["attributes"]
        print(f"Flight Carbon Estimate (g CO2e): {attributes['carbon_g']}")
        print(f"Flight Carbon Estimate (kg CO2e): {attributes['carbon_kg']}")
        print(f"Flight Carbon Estimate (lb CO2e): {attributes['carbon_lb']}")
        print(f"Flight Carbon Estimate (mt CO2e): {attributes['carbon_mt']}")
    else:
        print("Could not retrieve flight estimate data.")

except Exception as e:
    print(f"An error occurred: {e}")

This Python snippet initializes the CarbonInterfaceClient with an API key, constructs a dictionary representing the flight segments, and then calls the get_flight_estimate method. The response, containing the estimated carbon emissions in various units (grams, kilograms, pounds, metric tons of CO2e), is then printed to the console. Similar patterns apply to other programming languages, with specific method names and data structures aligning with the respective SDK's design.

Community libraries

While Carbon Interface provides official SDKs for several mainstream languages, the developer community also contributes libraries for other programming environments. These community-maintained libraries extend the reach of the Carbon Interface API to languages not officially supported, offering developers more flexibility in their technology choices. Community libraries are typically open-source projects hosted on platforms like GitHub, allowing for peer review, contributions, and community support.

Developers considering community libraries should review their documentation, recent activity, and contributor base to assess their reliability and ongoing support. Examples of community-driven efforts include client libraries for languages such as Rust and Java, which fill gaps where official SDKs do not yet exist. These libraries often follow the same API conventions as the official SDKs, providing similar functionality for making API requests and handling responses.

Before integrating a community library, it is advisable to check the Carbon Interface client libraries page for the most up-to-date information on recommended and supported SDKs. The primary Carbon Interface documentation also offers cURL examples for all API endpoints, which can serve as a basis for building custom client implementations in any language without an existing SDK.