MTA API - Multi-Modal Realtime Transit Data
MTAPI is a comprehensive HTTP server that provides realtime transit data for NYC Subway, LIRR (Long Island Rail Road), and Metro-North Railroad (MNR). The API converts MTA's Protocol Buffer feeds to JSON, now with MNR and LIRR search, outages, alerts and more!
Massive credit and thanks to Jon Thornton for the original MTAPI project. This fork builds upon his excellent foundation to add multi-modal support and advanced search features. Original project: https://github.com/jonthornton/MTAPI
Before installing, ensure you have:
- Python 3.7 or higher
- pip (Python package installer)
- Git (for cloning the repository)
Install required system packages:
Ubuntu/Debian:
sudo apt update
sudo apt install python3 python3-pip python3-venv gitmacOS:
# Install Homebrew if not already installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install python3 gitWindows:
- Download Python 3.7+ from https://python.org/downloads/
- Install Git from https://git-scm.com/download/win
git clone https://github.com/your-username/MTAPI.git
cd MTAPIpython3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activatepip install -r requirements.txtThe API requires GTFS data files for LIRR and MNR. Download these files:
LIRR Data:
mkdir -p data/lirr
cd data/lirr
curl -O https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/lirr%2Fgtfs.zip
unzip gtfs.zip
cd ../..MNR Data:
mkdir -p data/mnr
cd data/mnr
curl -O https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/mnr%2Fgtfs.zip
unzip gtfs.zip
cd ../..Generate station JSON files for all systems:
# Generate MNR stations
python3 scripts/make_mnr_stations.py > data/mnr-stations.json
# Generate LIRR stations
python3 scripts/make_lirr_stations.py > data/lirr-stations.json
# Generate subway stations (if you don't have stations.json)
python3 scripts/make_stations_csv.py data/gtfs/stops.txt data/gtfs/transfers.txt > data/stations.csv
python3 scripts/make_stations_json.py data/stations.csv > data/stations.jsonCreate a settings.cfg file (copy from settings.cfg.sample):
MTA_KEY = your_mta_api_key_here
STATIONS_FILE = data/stations.json
DEBUG = False
THREADED = True
MAX_TRAINS = 10
MAX_MINUTES = 30
CACHE_SECONDS = 60Get your MTA API key from: https://api.mta.info/
python app.pyThe API will be available at http://localhost:5000
For production deployment, use a WSGI server:
# Install gunicorn
pip install gunicorn
# Run with gunicorn
gunicorn -w 4 -b 0.0.0.0:5000 app:appSearch Stations (Multi-System)
GET /search?q=grand+central&system=all
q: Search query (supports multiple keywords)system: Filter byall,subway,lirr,mnr
Find Stations by Location
GET /by-location?lat=40.7589&lon=-73.9851&system=all&limit=5
lat,lon: GPS coordinatessystem: System filterlimit: Max resultsradius: Search radius in degrees
Get Station by ID
GET /by-id/125,A24
Returns stations by comma-separated IDs (supports parent ID lookup)
Subway
/routes- List routes/by-route/6- Stations on route 6
LIRR
/lirr/routes- LIRR routes/lirr/stops- All LIRR stops/lirr/by-route/1- Stops on LIRR route/lirr/search?q=jamaica- Search LIRR stops
Metro-North
/mnr/routes- MNR routes/mnr/stops- All MNR stops/mnr/by-route/1- Stops on MNR route/mnr/search?q=stamford- Search MNR stops
Outages
GET /outages/search?station=union+square
Service Alerts
GET /alerts/search?q=delay&service=all
Route Planning
GET /route-plan?from_lat=40.7589&from_lon=-73.9851&to_lat=40.6892&to_lon=-73.9442
Generate API documentation:
python3 generate_openapi.pyThis creates openapi.json and openapi.yaml files compatible with Swagger UI and other OpenAPI tools.
- MTA_KEY: Your MTA API key (required)
- STATIONS_FILE: Path to subway stations JSON file (required)
- CROSS_ORIGIN: CORS headers (
*for development) - MAX_TRAINS: Maximum trains per station (default: 10)
- MAX_MINUTES: How far ahead to show arrivals (default: 30)
- CACHE_SECONDS: Data refresh interval (default: 60)
- THREADED: Enable background refresh (default: True)
- DEBUG: Flask debug mode (default: False)
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
# Download and process GTFS data
RUN ./scripts/setup-data.sh
EXPOSE 5000
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Update documentation
- Submit a pull request
python -m pytest tests/- Subway Real-time: MTA GTFS-Realtime feeds
- LIRR Real-time:
https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/lirr%2Fgtfs-lirr - MNR Real-time:
https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/mnr%2Fgtfs-mnr - Outages: MTA Elevator/Escalator status feeds
- Alerts: MTA Service alert feeds
- API responses are cached for 60 seconds by default
- Multi-system searches may take slightly longer than single-system
- Use the
limitparameter to control response sizes - Enable
THREADEDmode for production to prevent blocking
Contributions are welcome! Please read the contributing guidelines and submit pull requests to improve the API.
- Additional transit systems (buses, ferry)
- GraphQL support
- WebSocket real-time updates
- Jon Thornton: Original MTAPI creator and maintainer (https://github.com/jonthornton/MTAPI)
- MTA: For providing comprehensive real-time transit data APIs
- GTFS Community: For transit data standards and tools
- Contributors: Everyone who has contributed to making transit data more accessible