Skip to main content
NYC subway realtime feeds describe trains by NYCT’s north / south direction — even on east–west lines like the L. But riders don’t think in compass directions; they think “I’m trying to get to Union Sq.” This guide shows you how to use mta.subway.direction() to translate a destination string into the feed direction, then use that direction to fetch the right arrivals.
mta.subway.direction() requires a hosted apiKey. It resolves destinations against the hosted API’s static GTFS route order and is not available in direct-feed mode. See Authentication to get a key.

Prerequisites

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

Resolve a destination to a direction

Call mta.subway.direction() with the route the rider is on, the fromStopId they’re departing from, and the destination they typed. The method resolves the destination against the route’s stop order and returns the north / south direction that heads toward it.
All three parameters are required:
2

Branch on the result

The method always resolves to a SubwayDirectionResolution object — it never returns null. Check the resolved flag before reading the direction.
When resolved is true, the direction, displayDirection, terminal, and destinationStop fields are populated. When it’s false, reason explains why, and matches may contain ambiguous candidate stops you can disambiguate in your UI.
3

Feed the direction into arrivals

Pass the resolved direction straight into mta.subway.arrivals() to show only the trains heading the rider’s way.

Complete example

This helper takes a rider’s origin, route, and destination and returns the next trains heading the right way — or a helpful message when the destination can’t be resolved.

Example resolution

Handling ambiguous or unresolved destinations

When a destination string matches more than one stop — or none — resolved is false. Use reason for a short explanation and matches to offer the rider a choice.
mta.subway.arrivals() also accepts the rider-facing aliases 'uptown' / 'downtown' (and 'east' / 'west' on some lines), which it maps to the underlying feed direction for you. Use mta.subway.direction() when you only know where the rider wants to go, and the aliases when the rider already knows which way they’re heading.
Subway feed directions are always north / south, even on east–west lines like the L. mta.subway.direction() resolves to one of those two values so it can be passed directly to mta.subway.arrivals(). For the full parameter and type reference, see the Direction API reference.