5  API

API Access Guide on TaiPI Data Repository

Quickly download datasets hosted on the TaiPI Data Repository using command-line utilities or programmatically.

Below are examples of how to fetch data using Wget, cURL, and Python — with and without authentication.


🔑 Authentication (API Key)

Some datasets may require authentication. You’ll need an API key.

  • Obtain your API key from this link.
  • Use it in the Authorization header when downloading.

Wget Instructions

To bulk download a dataset directly:

wget -O <output_filename>.tar.gz https://taipidata.org/api/download/<DATASET_ID>.tar.gz 

cURL Instructions

To bulk download a dataset with cURL:

curl -L -o <output_filename>.tar.gz https://taipidata.org/api/download/<DATASET_ID>.tar.gz

Filtering datasets based on spatial parameters

1) Set variables

DATASET_ID="<DATASET_ID>"
API_KEY="<YOUR_API_KEY>"
BBOX="-99.1068962554186,78.30928844783944,-73.22652777579931,84.24406980383603"
BASE_URL="https://taipidata.ncu.edu.tw/api/datasets"

2) Fetch JSON to a file

wget --header="api-key: ${API_KEY}" -O "response.json" "${BASE_URL}?bbox=${BBOX}&dataset_id=${DATASET_ID}"

Or

curl -H "api-key: ${API_KEY}" -o "response.json" "${BASE_URL}?bbox=${BBOX}&dataset_id=${DATASET_ID}"

3) Inspect / print item details (optional)

jq -r --arg id "$DATASET_ID" '  .[$id][]  | "Name: \(.name // "N/A")\nBounding Box: \(.bbox // "N/A")\nCoordinates: \(.coordinates // "N/A")\nData URL: \(.data // "N/A")\nThumbnail URL: \(.thumbnail // "N/A")\n----------------------------------------"' response.json

4) Download each data URL

jq -r --arg id "$DATASET_ID" '.[ $id ][] | .data' response.json | while read -r url; do
    [ -z "$url" ] && continue
    file="${url##*/}"
    echo "Downloading: $file"
    wget --header="api-key: ${API_KEY}" "$url" -O "$file"
done

Python Instructions

Example usage:

# python code
import requests
import json
import urllib.request
dataset_id = "<DATASET_ID>"
base_url = "https://taipidata.ncu.edu.tw/api/datasets?"
api = "<YOUR_API_KEY>"

params = {
    "bbox": "-99.1068962554186,78.30928844783944,-73.22652777579931,84.24406980383603", # example bbox
    "dataset_id": dataset_id,
}

headers = {
    "api-key": api
}

response = requests.get(base_url, params=params, headers=headers)

if response.status_code == 200:
    result = dict(response.json()) 
    
    for item in result[dataset_id]:
        print("Name:", item.get("name", "N/A"))
        print("Bounding Box:", item.get("bbox", "N/A"))
        print("Coordinates:", item.get("coordinates", "N/A"))
        print("Data URL:", item.get("data", "N/A"))
        print("Thumbnail URL:", item.get("thumbnail", "N/A"))
        url = item.get("data", "N/A")
        output_file = url.split('/')[-1]
        urllib.request.urlretrieve(url, output_file)
        print(f"File downloaded successfully and saved as {output_file}")
        print("-" * 40)
else:
    print(f"Error: {response.status_code}")
    print(response.text)

✅ Replace <DATASET_ID> with the actual dataset ID.

✅ Replace <YOUR_API_KEY> with your personal API key from this link.

✅ Replace <BBOX> with the actual bbox.

✅ Change <output_filename> to your preferred filename.

Contact Support

For technical assistance or to report issues with the API, please contact at: ilhamadi@ncu.edu.tw