A FastAPI application that provides real-time train arrival information for NYC subway stations.
- Get information about NYC subway stations
- Real-time train arrival data for supported stations
- Filter train arrivals by line
- API key authentication
- Request tracking with unique request IDs
- Comprehensive error handling
- Health check endpoint for monitoring
- CORS protection for production environments
- Python 3.7+
- pip (Python package installer)
- MTA API key (for real-time train data)
- Clone the repository:
git clone https://github.com/yourusername/nyc-mta-station-api.git
cd nyc-mta-station-api- Create a virtual environment:
python -m venv venv- Activate the virtual environment:
- On Windows:
venv\Scripts\activate- On macOS/Linux:
source venv/bin/activate- Install dependencies:
pip install -r requirements.txt- Create a
.envfile in the project root with the following variables:
API_KEY=your_api_key_here
ALLOWED_ORIGINS=http://localhost:3000,https://yourdomain.com
HOST=0.0.0.0
PORT=8000
WORKERS=4
uvicorn app:app --reloadpython app.pyor
uvicorn app:app --host 0.0.0.0 --port 8000 --workers 4Once the server is running, you can access the API documentation at:
- Swagger UI:
http://localhost:8000/api/docs - ReDoc:
http://localhost:8000/api/redoc
All endpoints are prefixed with /api except the root endpoint.
| Method | Endpoint | Description | Authentication |
|---|---|---|---|
| GET | / |
Welcome message and API information | No |
| GET | /api/health |
Health check endpoint | No |
| GET | /api/stations |
Get all stations | API Key |
| GET | /api/stations/{station_id} |
Get details for a specific station | API Key |
| GET | /api/stations/{station_id}/trains |
Get real-time train arrivals | API Key |
The API uses API key authentication. Include your API key in the X-API-Key header:
X-API-Key: your_api_key_here
curl -X 'GET' \
'http://localhost:8000/api/stations' \
-H 'X-API-Key: your_api_key_here'curl -X 'GET' \
'http://localhost:8000/api/stations/union-square' \
-H 'X-API-Key: your_api_key_here'curl -X 'GET' \
'http://localhost:8000/api/stations/union-square/trains' \
-H 'X-API-Key: your_api_key_here'curl -X 'GET' \
'http://localhost:8000/api/stations/union-square/trains?line=456' \
-H 'X-API-Key: your_api_key_here'nyc-mta-station-api/
├── app.py # Main FastAPI application
├── mta_data/ # MTA data fetching modules
│ ├── __init__.py
│ └── subway.py # Module for subway train data
├── .env # Environment variables
├── requirements.txt # Python dependencies
├── api.log # API logs
└── README.md # Project documentation
To add support for new stations:
- Add the station to the
STATIONSdictionary inapp.py - Create a data fetching function in the appropriate module
- Add the station ID and function mapping to the
STATION_DATA_FUNCTIONSdictionary
- FastAPI - Modern web framework for building APIs
- Uvicorn - ASGI server
- python-dotenv - Environment variable management
- Pydantic - Data validation
- requests - HTTP client
- gtfs-realtime-bindings - GTFS Realtime binding for Python
- protobuf-to-dict - Convert Protocol Buffers to Python dictionaries
Create a requirements.txt file with the following dependencies:
fastapi==0.103.1
uvicorn==0.23.2
python-dotenv==1.0.0
requests==2.31.0
gtfs-realtime-bindings==1.0.0
protobuf-to-dict==0.1.0
pydantic==2.3.0
starlette==0.27.0
python-multipart==0.0.6
For accessing the MTA data specifically:
pip install gtfs-realtime-bindings protobuf-to-dict google-api-python-client| Variable | Description | Default |
|---|---|---|
| API_KEY | Authentication key for the API | (required) |
| ALLOWED_ORIGINS | Comma-separated list of allowed CORS origins | "" |
| HOST | Host address to bind the server | 0.0.0.0 |
| PORT | Port to run the server | 8000 |
| WORKERS | Number of worker processes for Uvicorn | 4 |
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature-name - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- MTA for providing the GTFS Realtime API
- The FastAPI team for the excellent framework
This application is not affiliated with, endorsed by, or in any way officially connected to the MTA. This is an independent project that utilizes publicly available MTA data.