Transport for Minneapolis, US
TransportationIntroduction to Metro Transit's Public API
Metro Transit provides a public API that allows developers to access real-time information about transit services in the Twin Cities metropolitan area. This API can be used to develop applications that provide up-to-date information on schedules, routes, and vehicle locations for buses and trains operated by Metro Transit.
Getting Started with the API
To use Metro Transit's public API, you will need to register for an API key on the Metro Transit website. Once you have an API key, you can start making requests to the API endpoints.
The base URL for the API is http://svc.metrotransit.org/. All requests to the API must include your API key as a query parameter.
Here is an example of how to make a request to the API for real-time bus information:
const apiKey = 'YOUR_API_KEY';
const busRoute = 'METRO Blue Line'; // Replace with desired bus route.
const busStop = 'Target Field Station'; // Replace with desired bus stop.
fetch(`http://svc.metrotransit.org/NexTrip/${encodeURIComponent(busRoute)}/${encodeURIComponent(busStop)}`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
}
})
.then(response => response.json())
.then(data => console.log(data));
This code will fetch real-time bus information from the Metro Transit API for the specified bus route and stop, using your API key for authorization.
Available Endpoints
The Metro Transit API provides several endpoints for accessing transit information. Here are some examples of how to use some of these endpoints:
1. Real-Time Bus Information
You can use the /NexTrip
endpoint to retrieve real-time bus information for a specific bus stop. Here is an example of how to use this endpoint in JavaScript:
const apiKey = 'YOUR_API_KEY';
const busRoute = 'METRO Blue Line'; // Replace with desired bus route.
const busStop = 'Target Field Station'; // Replace with desired bus stop.
fetch(`http://svc.metrotransit.org/NexTrip/${encodeURIComponent(busRoute)}/${encodeURIComponent(busStop)}`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
}
})
.then(response => response.json())
.then(data => console.log(data));
2. Real-Time Train Information
You can use the /NexTrip
endpoint to retrieve real-time train information for a specific train station. Here is an example of how to use this endpoint in JavaScript:
const apiKey = 'YOUR_API_KEY';
const trainRoute = 'METRO Blue Line'; // Replace with desired train route.
const trainStation = 'Target Field Station Platform 1'; // Replace with desired train station.
fetch(`http://svc.metrotransit.org/NexTrip/${encodeURIComponent(trainRoute)}/${encodeURIComponent(trainStation)}`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
}
})
.then(response => response.json())
.then(data => console.log(data));
3. Scheduled Departures
You can use the /ScheduledDeparture
endpoint to retrieve scheduled departure times for a specific bus stop or train station. Here is an example of how to use this endpoint in JavaScript:
const apiKey = 'YOUR_API_KEY';
const routeType = '3'; // Replace with the route type (3 for buses, 2 for trains).
const stopId = 'TF12'; // Replace with the stop ID.
fetch(`http://svc.metrotransit.org/ScheduledDeparture/${encodeURIComponent(routeType)}/${encodeURIComponent(stopId)}`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
}
})
.then(response => response.json())
.then(data => console.log(data));
Conclusion
Metro Transit's public API provides a convenient way for developers to access real-time transit information for the Twin Cities metropolitan area. By using the API endpoints, you can build applications that provide users with up-to-date information on bus and train schedules, routes, and vehicle locations.
To get started with the Metro Transit API, simply register for an API key on the Metro Transit website and start making requests to the API endpoints using JavaScript or your preferred programming language.