Skip to main content
Menu

Best Practices

When working with the V3 API, we recommend several best practices.

Skip to any section: 

V3 API Documentation

Read the latest endpoint and resource documentation about available fields, filters, and includes.

Read the documentation

Rate Limiting

Use of the V3 API 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 1,000 requests per minute. In the V3 API Portal, you can view your current rate limit and, if needed, request a rate limit increase.

See the sections on Caching and Sparse Fieldsets for ways to fetch less data and avoid hitting rate limits.

All V3 API 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

Request a V3 API Key

Requests made with a valid API key are limited to a default of 1,000 requests per minute.

Request a key

Caching

The V3 API 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 receive a 304 Not Modified status code. This cached response won't count against your API key limit. Another advantage of using this header is that you won't receive an update if you hit a server that was updated in the past.

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

Streaming

Often when working with predictions or other realtime data, it’s desirable to get new data as it changes. Rather than using polling for this purpose, the V3 API supports streaming new data in real time.

Read documentation about streaming

Compression

The V3 API 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 ~10x reduction in data size.

Example: The full list of routes as of October 2024 is reduced from 86 KB without compression to 7.1 KB with compression.

Sparse Fieldsets

Each type of data supports a query parameter `fields[type]`, which limits the returned attributes (reference).

Example: https://api-v3.mbta.com/routes/?fields%5Broute%5D=short_name,long_name returns only the names 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/route_patterns?filter[route]=CR-Providence&include=representative_trip&fields[trip]=headsign .

Nested Includes

JSON:API can include multiple levels of relationships by connecting the relationship names with dots (reference).

Example: The response for https://api-v3.mbta.com/routes/Red?include=route_patterns.representative_trip.shape will include the requested route, its route patterns, the representative trips of those patterns, and the shapes of those trips.

This works to any depth of relationships, and it can save you the need to make N+1 requests in many situations.

Updates

The realtime data can update very frequently, even using `If-Modified-Since` headers to avoid stale data. Predictions update every ~12 seconds, and vehicles update more than once per second. 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.

Predictions

Displaying realtime predictions is a popular use of the API, and there are some complexities involved in interpreting and displaying this data correctly.

V3 API Documentation

Read the latest endpoint and resource documentation about available fields, filters, and includes.

Read the documentation

Request a V3 API Key

Requests made with a valid API key are limited to a default of 1,000 requests per minute.

Request a key