Open Government, Canada
Data AccessExploring Open Canada API Documentations
Open Canada API is an open platform of government data and information which provides access to a range of datasets. It's an initiative to promote transparency and collaboration between the Canadian Government and its citizens.
The API documentation provided on the Open Canada website is organized in a user-friendly manner which allows developers to get started quickly.
Accessing the API
Accessing the API requires an API key, which can be obtained by registering on the Open Canada website. Once you have your API key, you can use it to make requests to the API.
API Endpoints
The Open Canada API consists of several endpoints. Each endpoint contains a set of resources, each of which allows you to fetch data from the API. Here are a few examples:
Search API
This endpoint allows you to search for datasets on the Open Canada platform. Here's an example of how to search for datasets:
const query = 'climate change';
const url = `https://open.canada.ca/data/api/action/package_search?q=${query}&rows=10`;
const response = await fetch(url, { headers: { 'Authorization': apiKey } });
const data = await response.json();
console.log(data);
Catalogue API
This endpoint allows you to browse the complete catalogue of datasets on the Open Canada platform. Here's an example of how to retrieve the catalogue:
const url = 'https://open.canada.ca/data/api/3/action/package_list';
const response = await fetch(url, { headers: { 'Authorization': apiKey } });
const data = await response.json();
console.log(data);
Resource API
This endpoint allows you to retrieve and download resources for a given dataset. Here's an example of how to retrieve the resources for a dataset:
const datasetId = '64f332a5-7f5e-4e35-a6d7-b175f3c1400f';
const url = `https://open.canada.ca/data/api/3/action/package_show?id=${datasetId}`;
const response = await fetch(url, { headers: { 'Authorization': apiKey } });
const data = await response.json();
// get the first resource in the list
const resource = data.result.resources[0];
// download the resource
const fileUrl = `${resource.url}?api_key=${apiKey}`;
const fileResponse = await fetch(fileUrl);
const file = await fileResponse.blob();
// do something with the file
console.log(file);
Conclusion
The Open Canada API provides a wealth of data and information that can be used to power your applications. By exploring the API documentation and experimenting with the various endpoints, you can discover new and exciting ways to leverage this data for your own projects.