Confused by v3 API

776 views
Skip to first unread message

Carl Lambrecht

unread,
Apr 18, 2018, 9:44:44 AM4/18/18
to MBTA Developers
I'm trying to migrate my commuter rail application from v2 to v3, and unfortunately find the results I get from the v3 API confusing.

In v2, I can make a single call to /predictionsbyroute and get the trains on a particular line and their predicted arrival at all the remaining stops on the route.
Ex. http://realtime.mbta.com/developer/api/v2/predictionsbyroute?api_key={{v2-api-key}}&route=CR-Providence&format=json

It seems that in v3, the recommendation for migration is /schedules, but that seems to give schedule for all trips on the route, not just the ones currently in progress.
Ex. https://api-v3.mbta.com/schedules?api-key={{api-key}}&include=prediction,trip&sort=direction_id&filter[route]=CR-Providence

Alternatively, calling the /predictions endpoint only seems to give partial information. 
Ex. https://api-v3.mbta.com/predictions?api-key={{api-key}}&filter[route]=CR-Providence
The above call, at 9:37 am, only provides predictions for the last 2 stops for trip 910. This trip is scheduled to originate at Stoughton at 10:40am, but the only predictions provided for it are arrival at Back Bay and South Station.

Is the GTFS realtime feed a better migration option than the v3 API if I want to replicate the behavior from v2? Or is there a recommendation that anyone can provide that would help me navigate one or two calls in the v3 API that would get me the same specific information that I have been getting from the v2 /predictionsbyroute endpoint?

At the end of the day, I'm looking to create a list of the active trips on a particular line at a particular time, and their predicted arrival at remain stops on that trip.

Cheers,
Carl


Developer at MBTA

unread,
Apr 18, 2018, 11:09:22 AM4/18/18
to MBTA Developers
Hi Carl,

You can also use `include=vehicle` to get the vehicle information that /predictionsbyroute included, or `include=schedule` to get the schedule information.


thanks,
developer@mbta

Carl Lambrecht

unread,
Apr 18, 2018, 11:24:53 AM4/18/18
to MBTA Developers
That does look promising. On first glance, that looks like it provides the information I'm looking to represent in the app. I'll work on parsing the information for individual trips out of that JSON. Thank you very much for the quick response!

Cheers,
Carl

Dylan Sabo

unread,
Jun 22, 2018, 1:44:06 PM6/22/18
to MBTA Developers
So when I try to run this query for example:

I get data returned and it doesn't reject my api key, and I do see vehicle information in terms of lat and long. But it does not list out the station information, it doesn't list out next arrival times for the train for predictions. I was just wondering where this data is and what I am doing wrong. Currently I am using the V2 api and running calls like...


and this data returns very clean and easily parsed data as it shows the trip_name, trip_headsign, and the vehicle info for each direction and next train information. Any help on fixing the api-v3.mbta.com query i'm using would be of much help!

Thank you in advance!

Developer at MBTA

unread,
Jun 22, 2018, 2:03:55 PM6/22/18
to MBTA Developers
Hi Carl,

If you're looking for stop information, you can include it with `include=stop`. 

To include both vehicle and stop information, you can combine them with a comma: `include=vehicle,stop`

Hope this helps!
Developer@MBTA

Dylan S

unread,
Jun 22, 2018, 2:38:29 PM6/22/18
to MBTA Developers
Hello thanks for the response,

Is their any way to have the vehicle information under the trip type for like forest hills or oak grove, or match them for the next available train / pre_away information like the V2 Api has?

Regards,

Dylan S 

Dylan S

unread,
Jun 22, 2018, 3:45:06 PM6/22/18
to MBTA Developers
Are their any comparable fields to the pre_away seconds information that was provided in V2 that are available in V3?

Developer at MBTA

unread,
Jun 22, 2018, 3:53:50 PM6/22/18
to MBTA Developers
Hi Dylan,

If you're looking for the direction the vehicle is headed, you can use `include=trip`: the `heading` attribute will include the destination of the vehicle.

In order to calculate the number of seconds before a vehicle is arriving or departing, you can calculate the difference between the current time and `departure_time` or `arrival_time`. The V3 API does not include the number of seconds directly as it's non amenable to caching.

Sincerely,
Developer@MBTA
Reply all
Reply to author
Forward
0 new messages