CensusMapper Documentation

API

The CensusMapper API serves census data and boundaries to scripts and programs. Most people use it through the cancensus R package or the pycancensus Python package rather than calling it directly.

Getting an API key

  1. Sign up for a free CensusMapper account, or log in.
  2. Open your profile and go to API Access. Your key (the profile calls it a token) is created the first time you open this section. It starts with CensusMapper_.
  3. Use Show or Copy to get the key.

Keep your key secret: anyone who has it can make requests on your behalf. If it gets out, use Regenerate Token on the same page. The old key stops working immediately, so update your scripts with the new one.

Using your key in R or Python

Install the key once, so it’s available in every session without appearing in your code:

library(cancensus)
set_cancensus_api_key("YOUR_API_KEY", install = TRUE)
import pycancensus
pycancensus.set_api_key("YOUR_API_KEY", install=True)

The API Access section of your profile shows the same code with your key filled in.

cancensus stores downloaded data in a cache, and requests it can answer from the cache are free. By default the cache is a temporary folder that’s emptied when R closes. Set a permanent one to avoid downloading the same data again:

set_cancensus_cache_path("~/cancensus_cache", install = TRUE)

Building a request

The API page on CensusMapper helps you put a request together. Pick regions on the map and variables from the census variable tree, and it writes the matching cancensus (R) or pycancensus (Python) code. When you’re logged in you can also check the cost of the request and download the data as CSV or GeoJSON.

On a map, the menu in the sidebar offers R code for this view (cancensus), which writes R code that downloads the data for the area you’re looking at and recomputes the map’s values.

API points and quotas

Each data request costs API points:

Request Cost
Census data 1 point per region and variable
Boundaries 5 points per region
Detailed boundaries (resolution = "high") 20 points per region

For example, 10 variables for the 800 or so dissemination areas in a mid-sized city cost about 8,000 points. Listing datasets, searching variables and regions, and checking costs are free.

A free account can use 100,000 points per day and 1,000,000 points per month. Your profile shows how many points you have used today, this month and in total. When you reach the limit, requests fail with the error Access denied, API limit exceeded until the next day or month.

Some limits apply to a single request: at most 50,000 boundaries per request, and at most 1,000,000 regions for data.

CensusMapper isn’t built for bulk downloads of many variables for whole provinces or all of Canada. For that, download the data directly from Statistics Canada.

Downloading from a map

When you’re logged in, the map menu offers Download data in view (CSV, ZIP) and Download data in view (JSON). These download the data for the regions on your screen without using API points. The ZIP file includes a description of the data and its licence.

Calling the API directly

If you don’t use cancensus or pycancensus, these are the main endpoints. All are under https://censusmapper.ca.

Endpoint Key needed Returns
GET /api/v1/list_datasets No Available datasets
GET /api/v1/vector_info/{dataset}.csv No The variables of a dataset
GET /data_sets/{dataset}/place_names.csv No Named regions with their IDs
GET /api/v1/calculate No The cost of a request, and with a key your remaining points
POST /api/v1/data.csv Yes Census data as CSV
POST /api/v1/geo.geojson Yes Boundaries as GeoJSON

For data and boundaries, send the key as the api_key field of the request body, along with dataset, level, regions (for example {"CMA": ["59933"]} for the Vancouver region) and, for data, vectors. Region IDs and variable IDs (vectors, such as v_CA21_1) are the ones cancensus uses.

Using the data

Census data is published by Statistics Canada under the Statistics Canada Open Licence. Credit Statistics Canada as the source. Crediting CensusMapper is welcome but not required. The boundaries have been processed for mapping and can differ slightly from the files Statistics Canada publishes.