Skip to main content
alert
Service Change Ongoing
MBTA service levels have been increased in the interest of the health and safety of our riders and employees. All riders must use face coverings when on the MBTA. More: MBTA.com/coronavirus

Best Practices

More from Developers

Select from the list below to learn more about the information and tools available for developers interested in MBTA data.

Documentation

The Swagger documentation is the best source for information about the different endpoints and fields we provide. Some particular places to look:

  • The endpoint documentation includes what filters are available and what additional information can be included
  • The resource documentation includes the format of the fields, as well as what the values can be

Rate limiting

Usage of the API v3 is subject to certain rate limits:

  • Requests made without an API key are subject to a limit of 20 requests per minute.
  • Requests made with a valid API key are limited to a default of 1000 requests per minute.

You can register to request an API key here. If you already have an API key, you can also view your current rate limit in the portal; you can also request an increase to your rate limit if you need one. See the sections of this document on Caching and Fields for ways to fetch less data and avoid hitting rate limits.

All API v3 responses include HTTP headers which show your rate limit status:

HeaderDescription
Headerx-ratelimit-limitDescriptionThe maximum number of requests you’re allowed to make per time window.
Headerx-ratelimit-remainingDescriptionThe number of requests remaining in the current time window.
Headerx-ratelimit-resetDescriptionThe time at which the current rate limit time window ends in UTC epoch seconds.

Caching

API v3 supports caching via the `Last-Modified` response and  `If-Modified-Since` request headers. Each response contains a `Last-Modified` header, specifying the last time that data was updated. If, on subsequent requests, your client passes an `If-Modified-Since` header with that value and the data hasn't changed, you'll quickly receive a 304 Not Modified. This cached response won't count against your API key limit either. Another advantage of using this header is that you won't receive an update if you hit a server which was updated slightly in the past.

Note: This only works for the root data type; included data isn't currently tracked by the `Last-Modified` header.

Compression

API v3 supports GZIP compression via the `Accept-Encoding` header. If your HTTP client doesn't do this transparently, you can pass `Accept-Encoding: gzip` as a request header, and the response will be compressed. This can result in large data savings: the full list of routes goes from 48k without compression to 3.4k with compression.

Fields

Each type of data supports a query parameter `fields[type]` which limits the returned attributes. For example, https://api-v3.mbta.com/routes/?fields%5Broute%5D=short_name,long_name returns only the name s for the routes. If you know what fields you need, this is another good way to reduce the amount of data you receive. This also works for included data types: https://api-v3.mbta.com/trips/?filter%5Broute%5D=CR-Providence&include=shape&fields%5Btrip%5D=name&fields%5Bshape%5D=name

Updates

The realtime data can update very frequently, even using `If-Modified-Since` headers to avoid stale data. You may want to include some logic in your clients to prevent relative times from bouncing (say between "3 minutes away" and "4 minutes away") if that would be confusing to your users. If you're displaying predictions at that level of granularity, you can also reduce the frequency of updates accordingly.

Alerts

Displaying alerts is one of the trickiest features to get correct. Service disruptions can affect large sets of riders, and you as the client developer are in the best position to know where they might be trying to ride.

Arrival and Departure Times

Follow these best practices for displaying prediction information for MBTA arrivals and departures of vehicles in a way that will match up with what is displayed on MBTA signage, apps, and the MBTA website.

When you make a "predictions" query, you'll receive predictions data that contains both arrival-time and departure-time values. Here's how to interpret and display that to customers:

Displaying predictions

The most popular way to display the MBTA's prediction information is a countdown clock showing how many minutes away the vehicle is from the stop.