There are several ways to consume CarbonAware data for your use case. Each approach surfaces the same data for the same call (e.g. the CLI should not give you different data than the WebAPI for the same query). We provide a number of different endpoints to provide the most flexibility to integrate to your environment:
-
You can run the application using the CLI and refer to more documentation here.
-
You can build a container containing the WebAPI and connect via REST requests and refer to more documentation here.
-
You can reference the Carbon Aware C# Library in your projects and make use of its functionalities and features.
-
(Future) You can install the Nuget package and make requests directly. (tracked here)
Each of these has configuration requirements which are detailed below. You can also visit the quickstart.md guide for a step-by-step process for running the CLI locally, deploying the Web API locally or in the cloud, polling the API via HTTP requests or generating and using client libraries (Python example).
For more detailed architecture and design decisions around the Carbon Aware SDK, refer to the Architecture directory.
The Carbon Aware SDK provides a C# Client Library with handlers that replicates the Web API, CLI and SDK functionality. See:
- carbon-aware-library.md for more information about library features.
- packaging.md for details on how to package and consume the library.
- gsf-carbon-aware-library-package.md for instructions on integrating the library in other projects with dependency injection.
Make sure you have installed the following pre-requisites to setup your local environment:
- dotnet core SDK https://dotnet.microsoft.com/en-us/download
- Access to one (or all) of the supported external data APIs
- WattTime account - See instruction on WattTime for details (or use our python samples as described here).
- ElectricityMaps account - See instruction on ElectricityMaps for details (or setup a free trial). Note that the free trial has some restrictions
- ElectricityMapsFree account - See instruction on ElectricityMapsFree for details.
Alternatively, you can also set up your environment using VSCode Remote Containers (Dev Container):
- Docker
- VSCode (it is recommended to work in a Dev Container)
- Remote Containers extension for VSCode
We support multiple data sources for carbon data. At this time, a JSON file, WattTime, ElectricityMaps, and ElectricityMapsFree are supported. To use WattTime data or Electricity Maps data, you'll need to acquire a license from them and set the appropriate configuration information.
You can also visit the selecting-a-date-source.md guide for more information on data sources options, and data-sources.md for detailed architecture decisions around integrating different data providers into the carbon aware SDK.
This project uses the dotnet standard
Microsoft.Extensions.Configuration
mechanism, which allows the user to configure their environment variables in a
unified view while making use of different configuration sources. Review the
link to understand more about the IConfiguration
type.
The WebAPI project uses standard configuration sources provided by ASPNetCore. Please review this link to understand how configuration is loaded and the priority of that configuration.
Please note that configuration is hierarchical. The last configuration source
loaded that contains a configuration value will be the value that's used. This
means that if the same configuration value is found in both appsettings.json
and as an environment variable, the value from the environment variable will be
the value that's applied.
See configuration.md for details about how to configure specific components of the application.
When adding values via environment variables, we recommend that you use the double underscore form, rather than the colon form. Colons won't work in non-windows environment. For example:
DataSources__EmissionsDataSource="WattTime"
Note that double underscores are used to represent dotted notation or child elements that you see in the JSON below. For example, to set proxy information using environment variables, you'd do this:
DataSources__Configurations__WattTime__UseProxy
For local-only settings you can use environment variables, the Secret Manager tool , or an untracked Development appsettings file to override the default project settings.
To use the settings file, rename a copy of the template called
appsettings.Development.json.template
to appsettings.Development.json
and
remove the first line of (invalid) comments. Then update any settings according
to your preferences.
Wherever possible, the projects leverage the default .NET configuration expectations. Thus, they can be configured using any file matching the format:
appsettings.<ENV>.json
. Where<ENV>
is the value of theASPNETCORE_ENVIRONMENT
environment variable. By convention projects tend to use the provided HostEnvironment constantsDevelopment
,Staging
, andProduction
.
You can publish Web API for Carbon Aware SDK with container. These instructions show how to build / run container image with Podman.
Following commands build the container which named to carbon-aware-sdk-webapi
from sources.
$cd src
$podman build -t carbon-aware-sdk-webapi -f CarbonAware.WebApi/src/Dockerfile .
Carbon Aware SDK Web API publishes the service on Port 80, so you need to map it to local port. Following commands maps it to Port 8080.
You also need to configure the SDK with environment variables. They are minimum set when you use WattTime or ElectricityMaps or ElectricityMapsFree as a data source.
$ podman run -it --rm -p 8080:80 \
-e DataSources__ForecastDataSource="WattTime" \
-e DataSources__Configurations__WattTime__Type="WattTime" \
-e DataSources__Configurations__WattTime__Username="wattTimeUsername" \
-e DataSources__Configurations__WattTime__Password="wattTimePassword" \
carbon-aware-sdk-webapi
or
$ podman run -it --rm -p 8080:80 \
-e DataSources__ForecastDataSource="ElectricityMaps" \
-e DataSources__Configurations__ElectricityMaps__Type="ElectricityMaps" \
-e DataSources__Configurations__ElectricityMaps__APITokenHeader="auth-token" \
-e DataSources__Configurations__ElectricityMaps__APIToken="electricityMapsToken" \
carbon-aware-sdk-webapi
or
$ podman run -it --rm -p 8080:80 \
-e DataSources__EmissionsDataSource="ElectricityMapsFree" \
-e DataSources__Configurations__ElectricityMapsFree__Type="ElectricityMapsFree" \
-e DataSources__Configurations__ElectricityMapsFree__token="<YOUR_CO2SIGNAL_TOKEN>" \
carbon-aware-sdk-webapi
When you success to run the container, you can access it via HTTP client.
$ curl -s http://localhost:8080/emissions/forecasts/current?location=westus2 | jq
[
{
"generatedAt": "2022-08-10T14:10:00+00:00",
"optimalDataPoint": {
"location": "GCPD",
"timestamp": "2022-08-10T20:40:00+00:00",
"duration": 5,
"value": 440.4361702590741
},
:
For more information on containerization, refer to the markdown in containerization.md.