MyTTC.ca Public API

MyTTC.ca provides a public API for developers to access transit information about Toronto Transit Commission (TTC) services. The API gives developers programmatic access to TTC schedules, arrival times, and real-time vehicle locations. In this post, we'll go over the MyTTC.ca public API documentation, and provide code examples to help you get started.

API Endpoints

MyTTC.ca provides two sets of endpoints:

  1. Static Schedule Data - this data is updated periodically and does not change based on real-time feed. The static data includes routes, stops, and timepoints.
  2. Real-Time Vehicle and Prediction Data - this data is updated in real-time and includes position, speed, and estimated arrival times.

API Documentation

You can find the MyTTC.ca public API documentation at the MyTTC.ca Developers page. The documentation provides instructions for using the API including:

  • Authentication and access instructions
  • Endpoint URL formats
  • Data formats for request and response
  • API examples in various programming languages

For this article, we'll focus on JavaScript examples using Axios library to interact with the API.

Getting Started with JavaScript & Axios

Before we begin, make sure you have Axios installed on your machine or included in your project dependencies. You can add Axios as a dependency in your package.json file:

npm install axios

To get started with API request in JavaScript and Axios, make sure to import the Axios library:

import axios from "axios";

Example API Requests

Here are some examples of API requests using the MyTTC.ca public API:

Get Route and Stop Data

const key = "INSERT_YOUR_API_KEY_HERE";
const endpoint = `https://myttc.ca/api/v1/routes.json?key=${key}&agency=ttc`;
axios.get(endpoint)
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.log(error);
  });

Get Arrival Time Data

const key = "INSERT_YOUR_API_KEY_HERE";
const endpoint = `https://myttc.ca/api/v1/routes/12/stops/22/next.json?key=${key}&agency=ttc&limit=3`;
axios.get(endpoint)
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.log(error);
  });

Get Real-Time Vehicle Location Data

const key = "INSERT_YOUR_API_KEY_HERE";
const endpoint = `https://myttc.ca/locations?type=vehicles&t=uber&key=${key}&agency=ttc`;
axios.get(endpoint)
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.log(error);
  });

Conclusion

The MyTTC.ca public API provides developers with the ability to access transit information about TTC services programmatically. In this post, we went over the MyTTC.ca public API documentation and provided code examples in JavaScript using Axios. By using the API, you can create real-time applications that can improve the transit experience for TTC riders.

Related APIs

Public APIs — A directory of free and public apis

Built by @mddanishyusuf