> ## Documentation Index
> Fetch the complete documentation index at: https://mtaapi.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Get NJ Transit rail departures

> Fetch NJ Transit rail departures, stations, vehicles, alerts, and route station patterns from the MTA API.

Use the MTA API to access NJ Transit RailData alongside subway and bus data. The API manages RailData credentials on the server, so your requests only need an MTA API key.

## Prerequisites

* Install `mta-js` with `npm install mta-js`.
* Set `MTA_API_KEY` in your environment.
* Requires `mta-js` 2.4.0 or later.

**1. Find a station code**

Call the stations endpoint to look up the RailData station code. For New York Penn Station, use `NY`.

```typescript theme={null}
import { MTA } from 'mta-js'

const mta = new MTA({ apiKey: process.env.MTA_API_KEY })
const stations = await mta.njTransit.rail.stations()

for (const station of stations) {
  console.log(`${station.id} — ${station.name}`)
}
```

**2. Request station departures**

Pass the station code to the departures endpoint. You can optionally filter by a line and limit the number of results.

```typescript theme={null}
const departures = await mta.njTransit.rail.departures({ station: 'NY', limit: 10 })

for (const train of departures) {
  console.log(
    `${train.departureTime} — ${train.destination ?? "Unknown destination"} — ` +
      `${train.status ?? "Status unavailable"} — track ${train.track ?? "TBD"}`,
  )
}
```

**3. Use the departure data**

Each departure includes its station, route, scheduled departure time, and minutes until departure. When RailData provides them, results also include the train ID, destination, track, status, delay, and last-updated time. Pass `includeStops=true` to request each train's stop list.

The route is available as a normalized rail line with its name and color:

```typescript theme={null}
const firstTrain = departures[0]

console.log(firstTrain?.route.longName)
console.log(firstTrain?.delaySeconds)
console.log(firstTrain?.stops)
```

**4. Check vehicles, alerts, and route stations**

Vehicle positions and alerts can be filtered by line or station. Route stations come from the static GTFS snapshot:

```typescript theme={null}
const vehicles = await mta.njTransit.rail.vehicles({ line: 'NEC', limit: 5 })
for (const vehicle of vehicles) {
  console.log(`${vehicle.trainId} — ${vehicle.route.longName ?? vehicle.route.id}`)
}

const alerts = await mta.njTransit.rail.alerts({ station: 'NY' })
for (const alert of alerts) {
  console.log(`${alert.header ?? alert.id} — ${alert.description ?? ''}`)
}

const routeStations = await mta.njTransit.rail.routeStations({ route: 'NEC' })
for (const direction of routeStations.directions) {
  console.log(`${direction.direction}: ${direction.stops.map((stop) => stop.name).join(' → ')}`)
}
```

## Other rail endpoints

| Endpoint | Description |
| - | - |
| `GET /api/v1/nj-transit/rail/stations` | List RailData stations. |
| `GET /api/v1/nj-transit/rail/departures?station=NY` | Get station departures; optionally filter by `line` or include stop lists. |
| `GET /api/v1/nj-transit/rail/vehicles` | Get active trains, optionally filtered by line. |
| `GET /api/v1/nj-transit/rail/alerts` | Get station and line messages. |
| `GET /api/v1/nj-transit/rail/routes/NEC/stations` | Get ordered stations for a route from the static GTFS snapshot. |

If RailData credentials are not configured on the API server, realtime rail requests return a `503` error. Requests use the same API-key authentication and usage limits as other MTA API endpoints.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.