Transport for U.S. (multiple locations)
TransportationNextBus Public API Documentation
The NextBus public API provides access to real-time transit data, allowing developers to retrieve bus and train schedules and predict their arrival times. This API is available in XML format and can be queried using a set of APIs.
API Endpoints
The NextBus public API endpoint is http://webservices.nextbus.com/service/publicXMLFeed
. The different parameters that can be used along with this endpoint are:
Parameter | Description |
---|---|
command |
Specify the type of request to be made. |
a |
Specify the agency that provides the transit data. |
r |
Specify the route on which to report data. |
s |
Specify the stop IDs. |
Accessing the API with JavaScript
To access the NextBus public API using JavaScript, you can use the fetch
method. Here are some examples of how to use this method:
Example 1: List all available commands
fetch('http://webservices.nextbus.com/service/publicXMLFeed?command=help')
.then(response => response.text())
.then(data => console.log(data));
Example 2: List all the available agencies
fetch('http://webservices.nextbus.com/service/publicXMLFeed?command=agencyList')
.then(response => response.text())
.then(data => console.log(data));
Example 3: Get information about a specific route
fetch('http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=<agency>&r=<route>')
.then(response => response.text())
.then(data => console.log(data));
Example 4: Get a list of all the stops on a specific route
fetch('http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=<agency>&r=<route>')
.then(response => response.text())
.then(data => console.log(data));
Example 5: Get real-time scheduling information for a specific stop
fetch('http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a=<agency>&s=<stop>&r=<route>')
.then(response => response.text())
.then(data => console.log(data));
Conclusion
The NextBus public API is an essential tool for developers interested in building transit applications. With the help of the API, developers can access real-time transit data and build applications that can help commuters plan their trips more efficiently. The API can be accessed through JavaScript, making it easy to integrate into your web applications.