Skip to content

Example programmatic API use

Here's some examples of how you might access and use the API from a programming environment such as Python, R, or bash.

First, generate a session token:

import requests
import json
import getpass  

url = "https://api.looc-b.farm"  

Make sure you have your access token available:

token = getpass.getpass()

Make a request for a region of interest. Here we'll use the monitoring endpoint to assess habitat condition since 2012 where we implemented a (fabricated!) tree planting event in 2015:

feature = {
    "type": "FeatureCollection",
    "features": [
        {
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Polygon",
            "coordinates": [
            [
                [
                147.62603759765625,
                -42.59454359788449
                ],
                [
                147.62603759765625,
                -42.604652418230486
                ],
                [
                147.689208984375,
                -42.72280375732726
                ],
                [
                147.80868530273438,
                -42.666280705649264
                ],
                [
                147.73590087890625,
                -42.58443313755392
                ],
                [
                147.62603759765625,
                -42.59454359788449
                ]
            ]
            ]
        }
        }
    ]
 }

endpoint = 'monitoring'

params = {"polygon": json.dumps(feature), 
          "mode": 'monitoring',
          "start_year": 2012, 
          "end_year": 2020,
          "action_year": 2015,
          "dataset": ["habitat-condition"]
         }

header = {'Authorization': 'Bearer {}'.format(token)}
response = requests.post(f'{url}/{endpoint}?', 
                        json = params, 
                        headers=header)
print(response.status_code)

All going well, you will be able to extract the API response objects from response.

data = response.json()
data.keys()

data is now a nested dictionary containing the returned objects. See the API overview for details of what the response contains, and the example notebook for examples of these.

library(httr)
library(jsonlite)
library(getPass)

url = "https://api.looc-b.farm"  

Make sure you have your access token available:

token = getPass::getPass()

Make a request for a region of interest. Here we'll use the monitoring endpoint to assess habitat condition since 2012 where we implemented a (fabricated!) tree planting event in 2015:

feature = '{
    "type": "FeatureCollection",
    "features": [
        {
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Polygon",
            "coordinates": [
            [
                [
                147.62603759765625,
                -42.59454359788449
                ],
                [
                147.62603759765625,
                -42.604652418230486
                ],
                [
                147.689208984375,
                -42.72280375732726
                ],
                [
                147.80868530273438,
                -42.666280705649264
                ],
                [
                147.73590087890625,
                -42.58443313755392
                ],
                [
                147.62603759765625,
                -42.59454359788449
                ]
            ]
            ]
        }
        }
    ]
 }'

endpoint = 'monitoring'

params = list("polygon": feature, 
              "mode": 'monitoring',
              "start_year": 2012, 
              "end_year": 2020,
              "action_year": 2015,
              "dataset" = list("habitat-condition"))

auth = paste("Bearer", token)

response = GET(paste0(url, '/', endpoint, '?'),
               add_headers("Content-Type" = "application/json",
                            Accept="application/+json",
                            "Authorization" = auth),
                body = toJSON(params), 
                encode = 'json')
print(response$status_code)

All going well, you will be able to extract the API response objects from response.

response_data = content(response, 'parsed')
str(response_data)

Make sure you have your access token available:

read -sp 'Enter token: ' TOKEN

Make a request for a region of interest. Here we'll use the monitoring endpoint to assess habitat condition since 2012 where we implemented a (fabricated!) tree planting event in 2015. This example requires jq:

FEATURE='{
        "type": "FeatureCollection",
        "features": [
            {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                [
                    [
                    147.62603759765625,
                    -42.59454359788449
                    ],
                    [
                    147.62603759765625,
                    -42.604652418230486
                    ],
                    [
                    147.689208984375,
                    -42.72280375732726
                    ],
                    [
                    147.80868530273438,
                    -42.666280705649264
                    ],
                    [
                    147.73590087890625,
                    -42.58443313755392
                    ],
                    [
                    147.62603759765625,
                    -42.59454359788449
                    ]
                ]
                ]
            }
            }
        ]
}'

MODE="monitoring"
START_YEAR=2012
END_YEAR=2020
ACTION_YEAR=2016
ACTION_TYPE="None"

BODY=$(jq --null-input \
--arg polygon "$FEATURE" \
--arg mode "$MODE" \
--arg start_year "$START_YEAR" \
--arg end_year "$END_YEAR" \
--arg action_year "$ACTION_YEAR" \
--arg action_type "$ACTION_TYPE" \
'{"polygon": $polygon, "mode": $mode, "start_year": $start_year, "end_year": $end_year, "action_year": $action_year, "action_type": $action_type, "dataset": ["habitat-condition"]}')

AUTH="Authorization: Bearer $TOKEN"  

RESPONSE=$(curl -X 'POST' \
         https://api.looc-b.farm/monitoring \
         -H 'accept: application/json' \
         -H "$AUTH" \
         -H 'Content-Type: application/json' \
         -d "${BODY}")

All going well, you will be able to extract the API response objects from RESPONSE.

# E.g.: to extract the assessment area
echo $RESPONSE | jq .assessment_area