API Documentation

Introduction

IRIU DataHub provides a comprehensive REST API based on CKAN's Action API. This API allows you to programmatically access all public datasets, metadata, organizations, and groups.

Base URL: https://donnees.iriu.ca/api/3/action/

Quick Start - Download All Datasets

Python Example

import requests
import json

BASE_URL = "https://donnees.iriu.ca/api/3/action"

# 1. Get list of all datasets
response = requests.get(f"{BASE_URL}/package_list")
datasets = response.json()["result"]
print(f"Found {len(datasets)} datasets")

# 2. Get metadata for a specific dataset
dataset_id = datasets[0]  # First dataset
response = requests.get(f"{BASE_URL}/package_show", params={"id": dataset_id})
metadata = response.json()["result"]

print(f"Title: {metadata['title']}")
print(f"Description: {metadata.get('notes', 'N/A')}")
print(f"Organization: {metadata['organization']['title']}")

# 3. Download resources (data files)
for resource in metadata["resources"]:
    print(f"Resource: {resource['name']}")
    print(f"Format: {resource['format']}")
    print(f"URL: {resource['url']}")

Download All Metadata to JSON

import requests
import json

BASE_URL = "https://donnees.iriu.ca/api/3/action"

# Get all datasets with full metadata
response = requests.get(f"{BASE_URL}/package_search", params={"rows": 10000})
all_datasets = response.json()["result"]["results"]

# Save to file
with open("iriu_datasets_metadata.json", "w", encoding="utf-8") as f:
    json.dump(all_datasets, f, ensure_ascii=False, indent=2)

print(f"Saved {len(all_datasets)} datasets to iriu_datasets_metadata.json")

Common API Endpoints

Action Endpoint Description
List all datasets /api/3/action/package_list Returns list of all dataset IDs
Search datasets /api/3/action/package_search?q=climate Search datasets by keyword
Get dataset details /api/3/action/package_show?id={id} Get full metadata for a dataset
List organizations /api/3/action/organization_list Returns list of all organizations
Get organization details /api/3/action/organization_show?id={id} Get organization info and datasets
List groups /api/3/action/group_list Returns list of thematic groups
Get group details /api/3/action/group_show?id={id} Get group info and datasets
List tags /api/3/action/tag_list Returns list of all tags
Site statistics /api/3/action/site_read Get site statistics

Advanced Search

Search Parameters

# Search with filters
/api/3/action/package_search?q=transport&rows=100&start=0

# Filter by organization
/api/3/action/package_search?fq=organization:ville-de-montreal

# Filter by tag
/api/3/action/package_search?fq=tags:climate

# Filter by format
/api/3/action/package_search?fq=res_format:CSV

# Combine filters
/api/3/action/package_search?q=eau&fq=organization:ville-de-quebec+tags:environment

Python - Advanced Search Example

import requests

BASE_URL = "https://donnees.iriu.ca/api/3/action"

# Search for climate-related datasets with CSV files
params = {
    "q": "climat OR climate OR température",
    "fq": "res_format:CSV",
    "rows": 100,
    "start": 0
}

response = requests.get(f"{BASE_URL}/package_search", params=params)
results = response.json()["result"]

print(f"Found {results['count']} datasets")
for dataset in results["results"]:
    print(f"- {dataset['title']}")

DataStore API

For datasets with DataStore enabled, you can query the data directly:

# Get data from a resource
/api/3/action/datastore_search?resource_id={resource_id}

# With filters and limits
/api/3/action/datastore_search?resource_id={id}&limit=100&offset=0

# SQL query (if enabled)
/api/3/action/datastore_search_sql?sql=SELECT * FROM "{resource_id}" LIMIT 10

Python - Download DataStore Data

import requests
import pandas as pd

BASE_URL = "https://donnees.iriu.ca/api/3/action"

# Get resource data from DataStore
resource_id = "your-resource-id-here"
response = requests.get(
    f"{BASE_URL}/datastore_search",
    params={"resource_id": resource_id, "limit": 1000}
)

data = response.json()["result"]
records = data["records"]

# Convert to pandas DataFrame
df = pd.DataFrame(records)
print(df.head())

# Save to CSV
df.to_csv("dataset.csv", index=False)

Complete Download Script

Here's a complete Python script to download all datasets and their resources:

"""
IRIU DataHub - Complete Dataset Downloader
Downloads all datasets metadata and resource files
"""

import requests
import json
import os
from pathlib import Path
from tqdm import tqdm  # pip install tqdm

BASE_URL = "https://donnees.iriu.ca/api/3/action"
OUTPUT_DIR = Path("iriu_data")

def get_all_datasets():
    """Fetch all datasets with full metadata"""
    response = requests.get(f"{BASE_URL}/package_search", params={"rows": 10000})
    return response.json()["result"]["results"]

def download_resource(url, filepath):
    """Download a resource file"""
    try:
        response = requests.get(url, stream=True, timeout=30)
        response.raise_for_status()
        with open(filepath, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)
        return True
    except Exception as e:
        print(f"Error downloading {url}: {e}")
        return False

def main():
    # Create output directory
    OUTPUT_DIR.mkdir(exist_ok=True)

    # Get all datasets
    print("Fetching all datasets...")
    datasets = get_all_datasets()
    print(f"Found {len(datasets)} datasets")

    # Save metadata
    metadata_file = OUTPUT_DIR / "all_metadata.json"
    with open(metadata_file, "w", encoding="utf-8") as f:
        json.dump(datasets, f, ensure_ascii=False, indent=2)
    print(f"Saved metadata to {metadata_file}")

    # Download resources (optional - can be large!)
    download_files = input("Download all resource files? (y/n): ").lower() == 'y'

    if download_files:
        resources_dir = OUTPUT_DIR / "resources"
        resources_dir.mkdir(exist_ok=True)

        for dataset in tqdm(datasets, desc="Downloading"):
            dataset_dir = resources_dir / dataset["name"]
            dataset_dir.mkdir(exist_ok=True)

            for resource in dataset.get("resources", []):
                if resource.get("url"):
                    ext = resource.get("format", "dat").lower()
                    filename = f"{resource['id']}.{ext}"
                    filepath = dataset_dir / filename

                    if not filepath.exists():
                        download_resource(resource["url"], filepath)

    print("Done!")

if __name__ == "__main__":
    main()

cURL Examples

# List all datasets
curl "https://donnees.iriu.ca/api/3/action/package_list"

# Get dataset metadata
curl "https://donnees.iriu.ca/api/3/action/package_show?id=DATASET_ID"

# Search datasets
curl "https://donnees.iriu.ca/api/3/action/package_search?q=transport&rows=10"

# Get all organizations
curl "https://donnees.iriu.ca/api/3/action/organization_list?all_fields=true"

Additional Resources

Need Help?

If you have questions about the API or need assistance: