SDKs overview

The City, Toronto Open Data portal provides access to over 200 datasets covering areas such as transportation, public safety, and city finances. While the City of Toronto does not publish or officially maintain dedicated SDKs, access to these datasets is primarily facilitated through a web interface and a CKAN API. The CKAN platform is an open-source data portal software that allows for programmatic interaction, enabling developers to query, retrieve, and integrate public datasets into their applications or analyses. Community-contributed libraries have emerged to simplify this interaction for popular programming languages such as Python and R, abstracting the direct HTTP requests to the CKAN API (Toronto Open Data API documentation).

These libraries typically wrap the CKAN API's RESTful endpoints, allowing users to search for datasets, retrieve metadata, download resources, and manage data programmatically. Developers can use these tools to build custom dashboards, research tools, or integrate city data into other services. The underlying CKAN API supports various operations, including searching for packages (datasets), resources (files within datasets), and organizations, as well as retrieving detailed metadata for each. Data resources are commonly available in formats such as CSV, JSON, and XML, accommodating diverse application needs.

Official SDKs by language

The City of Toronto Open Data initiative does not currently provide or officially maintain any first-party SDKs or client libraries for direct programmatic access. The primary method for data interaction is through the CKAN API, which is a standard RESTful API. Developers are expected to interact with this API directly using HTTP requests or leverage community-developed libraries that wrap these interactions. The decision to not provide official SDKs aligns with a model where the focus is on maintaining the data portal and its API, allowing the developer community to build tools based on their specific language and framework preferences (City of Toronto Open Data documentation).

Below is a table summarizing the availability of official SDKs, noting the absence of directly maintained ones by the City of Toronto:

Language Package Name Installation Command Maturity / Status
Python N/A (Community-maintained alternatives exist) N/A No official SDK provided
R N/A (Community-maintained alternatives exist) N/A No official SDK provided
JavaScript N/A N/A No official SDK provided

Installation

Given the absence of official SDKs, installation steps primarily refer to community-developed libraries that interface with the CKAN API. These libraries are typically installed using standard package managers for their respective languages.

Python

For Python, popular community libraries include ckanapi and ckan-api-client. These libraries provide methods to interact with CKAN instances, including the City, Toronto Open Data portal. To install ckanapi, which is often recommended for its comprehensive features and active maintenance:

pip install ckanapi

Alternatively, if you prefer ckan-api-client:

pip install ckan-api-client

Ensure you have Python and pip installed on your system before attempting these commands.

R

For R, the ckanr package is a widely used community library for interacting with CKAN APIs. You can install it directly from CRAN (Comprehensive R Archive Network):

install.packages("ckanr")

After installation, load the package into your R session:

library(ckanr)

Make sure your R environment is properly set up and updated for package installations (R-project.org download page).

Quickstart example

This quickstart example demonstrates how to use the ckanapi Python library to access datasets from the City, Toronto Open Data portal. The example focuses on searching for a dataset and retrieving its resources.

from ckanapi import RemoteCKAN

# Initialize the CKAN API client for the Toronto Open Data portal
# The base URL for the CKAN API is usually the portal's main URL
# followed by /api/3 (for v3 of the CKAN API)
ua = RemoteCKAN('https://open.toronto.ca')

# Search for datasets containing 'parking' in their title or description
# This uses the 'package_search' method, which is common in CKAN APIs
try:
    search_results = ua.action.package_search(
        q='parking enforcement'
    )

    print(f"Found {search_results['count']} datasets related to 'parking enforcement'.\n")

    if search_results['count'] > 0:
        # Iterate through the first few results and print their titles and IDs
        for dataset in search_results['results'][:3]:
            print(f"Dataset Title: {dataset['title']}")
            print(f"Dataset ID: {dataset['id']}")
            print(f"Dataset URL: https://open.toronto.ca/dataset/{dataset['name']}")

            # Retrieve resources (files) associated with this dataset
            if 'resources' in dataset and len(dataset['resources']) > 0:
                print("  Resources:")
                for resource in dataset['resources'][:2]: # Show first 2 resources
                    print(f"    - Resource Name: {resource['name']}")
                    print(f"      Format: {resource['format']}")
                    print(f"      Download URL: {resource['url']}")
            print("\n---\n")
    else:
        print("No datasets found matching the query.")

except Exception as e:
    print(f"An error occurred: {e}")

This script first initializes a RemoteCKAN object pointing to the Toronto Open Data portal. It then performs a package search for datasets related to 'parking enforcement'. For each found dataset, it prints the title, ID, and information about its associated resources, including download URLs. This demonstrates a fundamental interaction for discovering and accessing data programmatically from the portal.

Community libraries

As the City of Toronto does not offer official SDKs, community-developed libraries play a crucial role in facilitating programmatic access to its open data portal. These libraries are built and maintained by developers within the open-source community, leveraging the standard CKAN API. They typically aim to simplify common tasks such as searching for datasets, retrieving metadata, and downloading data resources by providing language-specific functions and abstractions over raw HTTP requests. The reliability and feature set of these libraries can vary, with some being more actively maintained and widely adopted than others.

Key community libraries include:

  • Python:
    • ckanapi: A Python client for the CKAN API, offering extensive functionality for interacting with CKAN instances. It supports searching, retrieving, creating, and updating datasets and resources. It is widely used and generally well-maintained.
    • ckan-api-client: Another Python library providing a client for the CKAN API, often used for simpler programmatic access.
  • R:
    • ckanr: An R client for the CKAN API, designed for data scientists and analysts using R. It provides functions to search, retrieve, and process data from CKAN portals, making it accessible within the R statistical computing environment.

When selecting a community library, developers should consider its activity level, documentation, and compatibility with the specific CKAN API version used by the Toronto Open Data portal. Developers can also consult the broader CKAN community resources for additional client libraries or tools that might be available for other programming languages or specialized use cases (CKAN extensions and tools).