Transport for Ottawa, Canada

Transportation

Making use of the OC Transpo Public API

In the world of software development, APIs (Application Programming Interfaces) are becoming more and more important. APIs act as intermediate programs that allow different software applications to communicate with each other. In order to access any API, you'll need to follow a specific set of instructions provided in the API documentation.

OC Transpo Public API is one such example. It provides access to the Ottawa's public transit data, enabling individuals to create applications that can query data related to routes, schedules, and trip plans. In this blog post, we'll provide an example of how to call the OC Transpo Public API using JavaScript.

Getting Started

Before diving into writing code to call the API, you'll need to ensure that you’ve properly set up your environment. The OC Transpo Public API is open to the public, so there is no need to sign up for an account or to get any credentials before you can make use of it.

Example Code

In order to call the API using JavaScript, you'll first need to create an XMLHttpRequest object and use it to make an HTTP request to the OC Transpo Public API URL. Here's an example code to get the route summary for a given bus stop:

let xhttp = new XMLHttpRequest();  // create a new XMLHttpRequest object
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        let response = JSON.parse(this.responseText); // parse the JSON response
        console.log(response.GetRouteSummaryForStopResult.Routes.Route);
    }
};
xhttp.open("GET", "https://api.octranspo1.com/v1.3/GetRouteSummaryForStop", true); // set up the request
xhttp.setRequestHeader("accept", "application/json");
xhttp.send("appID=[Your API ID]&apiKey=[Your API Key]&stopNo=[Your Bus Stop Number]"); // send the request

In the above example code, we first create a new XMLHttpRequest object. We then use the onreadystatechange function to check if the response from the API is complete and successful. Once we receive the response, we parse it using the JSON.parse() method and display the list of routes using the console.log() method.

You'll also need to provide your appID and apiKey as part of the API URL parameters. To get these values, you'll need to visit the OC Transpo Public API documentation page and obtain them from the API key registration form. The stopNo parameter is the bus stop number for which you want to retrieve the route summary, and it's required.

Conclusion

In this blog post, we've demonstrated how to call the OC Transpo Public API in JavaScript to retrieve the route summary for a given bus stop. We hope this example will help you get started with making use of the API for your own projects. Please note that this is just one example of how to use the API; there are many other endpoints with different data formats and query parameters. Therefore, if you're interested in using the API, we recommend that you check out the complete documentation on the OC Transpo Public API official website.

Related APIs in Transportation