Exploring Transit Land's Public APIs

Transit Land is a platform that curates and aggregates transit data from transit agencies and other sources. They offer public APIs that provide access to a wealth of transit data that you can use to build apps or improve existing ones.

In this article, we'll explore some of Transit Land's APIs and how to use them in JavaScript.

API Endpoints

Transit Land offers several APIs for accessing transit data. Here are a few of them:

  • https://api.transit.land/api/v2/schedule_stop_pairs - Returns a list of all transit trips that stop at a given stop.
  • https://api.transit.land/api/v1/feed_versions - Returns a list of feed versions available in Transit Land's Datastore.
  • https://api.transit.land/api/v2/router/router - Calculates possible routes between two stops using GTFS transit data.

Let's take a look at how to use these APIs in JavaScript.

Making API Calls in JavaScript

To make API calls from a JavaScript application, we can use the fetch() function, which is native to modern web browsers.

Example: Schedule Stop Pairs

To retrieve a list of all transit trips that stop at a given stop, we can make a GET request to the schedule_stop_pairs endpoint, passing the stop ID as a parameter.

fetch('https://api.transit.land/api/v2/schedule_stop_pairs?origin_onestop_id=o-9q8y-nfantacitypark&date=2019-05-03')
  .then(response => response.json())
  .then(data => console.log(data));

In this example, we're requesting schedule stop pairs for the stop with the ID o-9q8y-nfantacitypark on May 3, 2019.

Example: Feed Versions

To retrieve a list of feed versions available in Transit Land's Datastore, we can make a GET request to the feed_versions endpoint.

fetch('https://api.transit.land/api/v1/feed_versions')
  .then(response => response.json())
  .then(data => console.log(data));

Example: Router

To calculate possible routes between two stops using GTFS transit data, we can make a POST request to the router endpoint, passing the origin and destination stop IDs in the request body.

const requestBody = JSON.stringify({
  origin_onestop_id: 'o-9q8y-nfantacitypark',
  destination_onestop_id: 'o-9q8y-nfanthewestend'
});

const headers = {
  'Content-Type': 'application/json'
};

fetch('https://api.transit.land/api/v2/router/router', {
  method: 'POST',
  headers,
  body: requestBody
})
  .then(response => response.json())
  .then(data => console.log(data));

In this example, we're calculating possible routes between the stops with the IDs o-9q8y-nfantacitypark and o-9q8y-nfanthewestend.

Conclusion

In this article, we've explored a few of Transit Land's public APIs and how to use them in JavaScript. With these APIs, you can access a variety of transit data that you can use to build powerful transit-related apps and solutions.

Related APIs