YOU ARE CURRENTLY USING THE STAGING ENVIRONMENT

API - Version 0 (stable)

You can communicate with the Cool Farm Tool (CFT) using HTTP, allowing applications or scripts you write to:

  • fetch input and emissions data stored in the CFT for visualisation or further analysis,

  • create and modify product footprints and farm settings (e.g. to create an alternative data-entry interface, or to export input data from an existing application for processing in the CFT),

  • send input data, and receive immediate emissions estimates.

Warning

This documentation is for the stable version of the Cool Farm Tool API, which will receive support (bug-fixes, but not new features) until around December 2020; please ensure you are planning to upgrade your client code to API - Version 1 (current) by then.

Contents:

Quick start

  • See Authentication for information on how to obtain your API keys.

  • This example uses Python, but similar results could be achieved in other languages.

Ensure that the Requests library is installed (pip install requests), then save the following as retrieve.py:

from decimal import Decimal
import json

import requests

from settings import *

r = requests.get(API_URL + 'farm/', headers={
    'x-api-authorization': API_KEY,
    'x-api-app-authorization': APP_KEY,
    'x-requested-with': 'XMLHttpRequest'
})
try:
    farm = json.loads(r.content)
except ValueError, e:
    print r.content
    raise

print "loaded farm '%s' (%d products)" % (farm['name'], len(farm['products']))

for product in farm['products']:
    print ' * %s (@%s) ' % (product['name'], product['slug'])
    r_p = requests.get(API_URL + 'product/%s/' % product['slug'], headers={
        'x-api-authorization': API_KEY,
        'x-api-app-authorization': APP_KEY
    })
    try:
        product = json.loads(r_p.content)
    except ValueError, e:
        print r_p.content
        raise
    emissions = product['summary']['emissions_product']
    print '   total: %.2f %s' % (Decimal(emissions[0]), emissions[1])

Edit API_KEY and APP_KEY as appropriate, then run:

$ python retrieve.py
loaded farm 'test farm' (1 products)
 * potato_2014 (@F23EF302)
   total: -43531.35 kg CO2e