Skip to main content

Python wrapper for the carbem library - retrieve carbon emission data from cloud providers

Project description

Carbem

A Rust library for retrieving carbon emission values from cloud providers.

Overview

Carbem provides a unified interface for querying carbon emission data from various cloud service providers. This library helps developers build more environmentally conscious applications by making it easy to access and analyze the carbon footprint of cloud infrastructure.

Features

  • 🌍 Multi-provider support: Unified API for different cloud providers
  • Async/await: Built with modern async Rust for high performance
  • 🔒 Type-safe: Leverages Rust's type system for reliable carbon data handling
  • 🚀 Easy to use: Simple and intuitive API design
  • 🐍 FFI Ready: JSON-based API perfect for Python/TypeScript bindings
  • 🔧 Flexible Filtering: Filter by regions, services, and resources

Installation

Add this to your Cargo.toml:

[dependencies]
carbem = "0.2.0"

Quick Start

For standalone Rust applications, use the builder pattern with environment variables:

use carbem::{CarbemClient, EmissionQuery, TimePeriod};
use chrono::{Utc, Duration};

#[tokio::main]
async fn main() -> carbem::Result<()> {
    // Configure client from environment variables
    let client = CarbemClient::new()
        .with_azure_from_env()?;
    
    // Create a query
    let query = EmissionQuery {
        provider: "azure".to_string(),
        regions: vec!["subscription-id".to_string()],
        time_period: TimePeriod {
            start: Utc::now() - Duration::days(30),
            end: Utc::now(),
        },
        services: Some(vec!["compute".to_string(), "storage".to_string()]),
        resources: None,
    };
    
    let emissions = client.query_emissions(&query).await?;
    
    for emission in emissions {
        println!("Service: {}, Emissions: {} kg CO2eq", 
                 emission.service.unwrap_or_default(),
                 emission.emissions_kg_co2eq);
    }
    
    Ok(())
}

Create a .env file in your project root:

# Azure Carbon Emissions Configuration
CARBEM_AZURE_ACCESS_TOKEN=your_azure_bearer_token_here
# OR alternatively use:
# AZURE_TOKEN=your_azure_bearer_token_here

Configuration Parameters

Environment Variables (for Standalone Rust)

  • CARBEM_AZURE_ACCESS_TOKEN: Azure access token
  • AZURE_TOKEN: Alternative Azure access token variable

Azure Configuration (AzureConfig)

The Azure provider requires minimal configuration:

use carbem::AzureConfig;

let config = AzureConfig {
    access_token: "your-bearer-token".to_string(),
};

Object-Oriented API (Advanced Usage)

use carbem::{CarbemClient, AzureConfig, EmissionQuery, TimePeriod};
use chrono::{Utc, Duration};

#[tokio::main]
async fn main() -> carbem::Result<()> {
    // Create a client and configure Azure provider
    let config = AzureConfig {
        access_token: "your-bearer-token".to_string(),
    };
    
    let client = CarbemClient::new()
        .with_azure(config)?;
    
    // Query carbon emissions for the last 30 days
    let query = EmissionQuery {
        provider: "azure".to_string(),
        regions: vec!["subscription-id".to_string()], // Use your subscription IDs
        time_period: TimePeriod {
            start: Utc::now() - Duration::days(30),
            end: Utc::now(),
        },
        services: None,
        resources: None,
    };
    
    let emissions = client.query_emissions(&query).await?;
    
    for emission in emissions {
        println!("Date: {}, Region: {}, Emissions: {} kg CO2eq", 
                 emission.time_period.start.format("%Y-%m-%d"),
                 emission.region, 
                 emission.emissions_kg_co2eq);
    }
    
    Ok(())
}

Supported Providers

Microsoft Azure ✅

 Google Cloud Platform

Google Cloud Platform is not supported at the moment (October 11th 2025). Data are available only after exporting them to BigQuery as discussed in this page. Thus, one will need to make a query to the BigQuery API, which makes a standard implementation not possible at the moment.

Amazon Web Services (AWS)

AWS is not supported at the moment (October 11th 2025). Data are available in S3 buckets as discussed in this page. An endpoint existed but was discontinued on July 23rd 2025 (ref).

Roadmap

  • Core library infrastructure
  • Azure Carbon Emission Reports API integration
  • Amazon Web Services (AWS)
  • Google Cloud Platform (GCP)
  • Additional providers planned

Testing

The library includes a comprehensive test suite:

# Run all tests
cargo test

# Run specific Azure provider tests
cargo test providers::azure

# Run with output
cargo test -- --nocapture

Test coverage includes:

  • Provider creation and configuration
  • Query conversion and validation
  • Date parsing and time period handling
  • Data conversion from Azure API responses
  • Error handling for invalid configurations

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

This project is licensed under Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)

Acknowledgments

This project aims to support sustainability efforts in cloud computing by making carbon emission data more accessible to developers and organizations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

carbem_python-0.2.0.tar.gz (33.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

carbem_python-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

File details

Details for the file carbem_python-0.2.0.tar.gz.

File metadata

  • Download URL: carbem_python-0.2.0.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.6

File hashes

Hashes for carbem_python-0.2.0.tar.gz
Algorithm Hash digest
SHA256 291109975598d2d6006eff16d639cbd59c0929b30c87e23da0d5d70afe08a661
MD5 86f61df90aa2ef97c43053b08f23cacd
BLAKE2b-256 a7c1dd39f5d52b426c775b0960e25b18d1df1e491c5f7307c966d8c52dc6db11

See more details on using hashes here.

File details

Details for the file carbem_python-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for carbem_python-0.2.0-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 482f45dc5cdae869a28aeee1d563fb565164bcea65d384b01d77ca7993a47c38
MD5 a8eac9156186e0d653f1c700d261c5e1
BLAKE2b-256 fdce539044788141a7b96f163cf4001b3b1ae35a562e8f46b9e4884e08044d37

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page