Examples

Learn why and how to use the API

The Cooler API is a set of REST APIs for

  • generating and managing carbon footprints of products and services

  • totaling multiple footprints

  • marking transactions to be neutralized later

  • generating and displaying statistics

Travel Agency Example

Let's say you're a developer for a travel agency and are using your own custom-made online reservation system. The travel agency wants potential customers to know they're neutralizing the carbon emissions created by their trips, so you'll need to do a few things to achieve this:

  • generate the current carbon footprint of the trip

  • display the footprint to prospective customers

  • mark the trip for neutralization once it's purchased,

  • include the footprint and neutralization status in a later email.

First, let's get the carbon footprint of your trip. We'll use sample data for this example.

// Sample data to be sent in the API request
let sampleData = {
  "automaticallyNeutralize": true,
  "items": [
    {
      "price": 2349,
      "sku": "Eluthera",
      "currency": "USD",
      "manufacturer": "none",
      "description": "10-day trip to one of the Bahamas outer-most islands, featuring an open mic night hosted by Lenny Kravitz's band",
      "title": "vacation",
      "category": "tour operator",
      "zip": "90210",
      "isUsed": false
    }
  ]
};

// Asynchronous function to call the Cooler API
async function callCoolerAPI() {
  try {
    // API call to Cooler
    const response = await fetch('https://api.cooler.dev/v1/footprint/products', {
      method: 'POST',
      headers: {
        "Content-Type": "application/json",
        "COOLER-API-KEY": "cooler_test_1877606a-bcf7-4edb-aaf4-f3b3eaf2e3ca" // API key for authorization
      },
      body: JSON.stringify({
        sampleData,
        automaticallyNeutralize: false,
        processSyncronously: true
      })
    });

    // Parse the JSON response
    const data = await response.json();

    // Assuming the response has a 'footprint' property
    const footprint = data.items[0].footprint;

    // Display the footprint in an alert popup
    alert("Your trip will generate " + footprint + " of CO2");

  } catch (error) {
    console.error('Error calling the Cooler API:', error); // Handle any errors that occur during the API call
  }
}

// Call the API function
callCoolerAPI();

That's it! You get the idea. Let's look at the response data:

//the footprint endpoint returns data in the following format. 
//note there can be many items in the items array. 
{
    "id": "803f577a-d664-43af-8f73-c28f03542583",
    "items": [
        {
            "id": "31a97edf-046d-4644-8c3c-baf7c275ad0d",
            "submission": {
                "id": "0c64aeb7-1148-4c16-b728-1e44e0afae3f",
                "zip": "90210",
                "sku": "Eluthera",
                "currency": "USD",
                "manufacturer": "none",
                "title": "vacation",
                "price": 2349,
                "category": "tour operator",
                "description": null,
                "isUsed": false
            },
            "footprint": {
                "id": "3ca38ed1-28ea-4056-a49f-1c0785e69bc4",
                "currency": "USD",
                "expiresAt": "2024-07-24T15:28:40.764Z",
                "carbonFootprint": 1282.55,
                "carbonFootprintSavings": null
            }
        }
    ],
    "dateCreated": "2024-07-17T15:28:40.666Z",
    "dateUpdated": "2024-07-17T15:28:40.666Z"
}

What did we do? - submitted a transaction to the API with our API key and sample data to the footprint endpoint, then displayed part of the API's response. - marked the transaction to be neutralized by setting automaticallyNeutralize to true. That's it!

Last updated