Skip to main content
The mta-js SDK exposes four namespaces on the MTA client — subway, bus, alerts, and stops — each covering a different aspect of NYC transit data. The subway namespace has two methods (arrivals() and direction()); the others have one each. Every namespace method is async, returns typed results, and requires only a single initialized client to access the full API surface.

Subway

subway.arrivals() — real-time arrival predictions for any subway stop and route.

Direction

subway.direction() — resolve a rider’s destination to a north/south direction.

Bus

bus.arrivals() and bus.vehicles() — live arrivals and vehicle positions for any MTA bus route.

Alerts

alerts.current() — current service alerts including delays and planned work.

Stops

stops.near() — find nearby subway and bus stops by geographic coordinates.

Client initialization

Create a single MTA instance at startup and reuse it throughout your application. The constructor accepts an options object — pass an apiKey from mtaapi.dev for the hosted API, or busTimeKey + databaseUrl to run self-hosted.
The TypeScript signature for the constructor is:

Core exported types

The following types are exported from mta-js and cover the full shape of SDK responses.
Store your API key in an environment variable rather than hardcoding it. Use a .env file locally and your deployment platform’s secret manager in production.

Error handling

All SDK methods throw a typed MtaError when the request fails. Wrap calls in a try/catch block and inspect the code property to handle specific failure modes.
Common error codes returned by the SDK: