Skip to main content
This guide walks you through fetching real-time subway arrivals for a specific stop and route. You will initialize the MTA client, call mta.subway.arrivals(), and process the response to display upcoming trains in your application.

Prerequisites

  • mta-js installed in your project (npm install mta-js)
  • An MTA API key set as MTA_API_KEY in your environment
1

Find your stop ID

Every MTA subway stop has a unique stop ID. Stop IDs follow a letter-and-number format where the letter generally corresponds to the train line complex and the number identifies the specific station.A few examples to get you oriented:You can look up stop IDs in the MTA GTFS Static Data or reference the stops.txt file in the MTA’s GTFS feed. Pass the stop ID as a string exactly as it appears in those resources.
2

Call mta.subway.arrivals()

Import the MTA client and call mta.subway.arrivals() with the stopId and route you want to query.
Both stopId and route are required. The route value is the train letter or number as it appears on signage (e.g., 'A', '1', 'N', 'L').
3

Process the response

The response is an array of Arrival objects. Each entry includes the route and stop as structured objects, the predicted arrival time as an ISO 8601 string, a minutes field pre-computed for you, and optional destination / displayDirection fields for building human-readable UI.
Use displayDirection first (e.g. "toward 8 Av"), falling back to destination (the raw headsign), and finally the raw direction string.Example response:
4

Handle empty results and errors

When no trains are scheduled—late at night or during a service gap—arrivals will be an empty array. Wrap your call in a try/catch and check for an empty result before rendering.

Complete example

The function below ties all the steps together into a reusable arrivals display function.
MTA real-time data updates roughly every 30 seconds. Poll on the same interval to keep your UI current without overloading the API.
The direction field uses NYCT’s 'north' / 'south' values even on east-west lines. For the L train, mta-js also accepts rider-facing 'east' / 'west' aliases as query input and maps them to the underlying feed directions. For display, prefer displayDirection (e.g. "toward 8 Av") or destination over the raw direction string.
If your rider enters where they want to go rather than a compass direction, use mta.subway.direction() to resolve a destination like "Union Sq" into the north / south value, then pass it straight to mta.subway.arrivals({ ..., direction }).