MTBA API Client
mbta-client
is a promise-based Node.js client library for the MTBA API v3, with a few helper functions to parse response data.
Installation:
npm i mbta-client
Basic Usage
Fetch functions
import MBTA from 'mbta-client';
const mbta = new MBTA(YOUR_API_KEY);
const predictions = await mbta.fetchPredictions({ stop: 42 });
const stops = await mbta.fetchStops({ id: [70080, 'Back Bay'] });
const subwayRoutes = await mbta.fetchRoutes({ type: 'subway' });
const north = await mbta.fetchPredictions({ route: 'Red', direction_id: 1 });
const local = await mbta.fetchStops({ latitude: 42.373, longitude: -71.119 });
const sorted = mbta.fetchPredictions({
stop: 42,
sort: 'arrival_time',
descending: true,
});
Helper functions:
Arrivals/Departures
Note: arrival and departure helpers have the same API/options.
const arr = mbta.selectArrivals(response);
const dep = mbta.selectDepartures(response, { convertTo: 'minutes' });
Helper functions fetch the first, next, previous and last pages.
const results = await mbta.fetchPredictions({ stop: 42, limit: 2 });
const secondPageResults = await mbta.getNextPage(results);
const thirdPageResults = await mbta.getNextPage(secondPageResults);
const firstPageResults = await mbta.getFirstPage(results);
const lastPageResults = await mbta.getLastPage(results);
API
Fetch functions
(These return a promise that resolves to an MBTA response object)
These map to the endpoints listed at https://api-v3.mbta.com/docs/swagger/index.html. They return a promise that resolves to an MBTA response object. options
for each function maps to the filters listed on that page. options
that accept multiple values can be provided as an array or comma separated string.
mbta.fetchStops(options);
mbta.fetchTrips(options);
mbta.fetchRoutes(options);
mbta.fetchShapes(options);
mbta.fetchVehicles(options);
mbta.fetchServices(options);
mbta.fetchSchedules(options);
mbta.fetchFacilities(options);
mbta.fetchPredictions(options);
Helper functions
mbta.selectArrivals(response: MBTAResponse, options?: TimeOptions);
mbta.selectDepartures(response: MBTAResponse, options?: TimeOptions);
type TimeOptions = { convertTo?: 'ms' | 'seconds' | 'minutes' | 'hours' };
Note: arrival_time
could be null if it's the first stop on a route. If departure_time
is not null, the MBTA recommends using that instead. Departure could be null if it's the final stop on a route. See https://www.mbta.com/developers/v3-api/best-practices for more info.
mbta.selectIncluded(response: MBTAResponse, options?: TypeOptions);
type TypeOptions = { type?: include_value | include_value[] };
(These return a promise that resolves to an MBTA response object)
Note: Input must include links
property.
mbta.fetchFirstPage(response: MBTAResponse);
mbta.fetchLastPage(response: MBTAResponse);
mbta.fetchNextPage(response: MBTAResponse);
mbta.fetchPrevPage(response: MBTAResponse);
MBTA API Documentation: https://api-v3.mbta.com/docs/swagger/index.html
MBTA API Best Practices: https://www.mbta.com/developers/v3-api/best-practices
Note: This library is not affiliated with the MBTA or MassDOT.
License
MIT