Skip to main content

MTA API v3

Real-time NYC subway arrivals API with async concurrent feed fetching.

Key Differences from v2

  • Concurrent fetching: All 8 MTA feeds fetched in parallel using asyncio.to_thread()
  • Async cache: Uses asyncio.Lock to prevent concurrent refresh storms
  • Lazy refresh: Cache updates on first request after expiry

Performance

  • Sequential fetch (v2): ~2-4 seconds
  • Concurrent fetch (v3): ~500ms-1s

Quick Start

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
uvicorn app.main:app --reload

API Endpoints

Same as v2 - see /docs for OpenAPI documentation.

Docker

docker build -t mta-api-v3 .
docker run -p 8000:8000 mta-api-v3

Deploy to Fly.io

Fly.io is recommended for v3 since it supports persistent servers with the async caching model.

First-time setup

# Install flyctl
curl -L https://fly.io/install.sh | sh

# Login
fly auth login

# Launch app (creates fly.toml)
cd mta-api-v3
fly launch --name mta-api-v3 --region ewr --no-deploy

# Deploy
fly deploy

fly.toml

If you need to create it manually:

app = "mta-api-v3"
primary_region = "ewr"  # Newark, close to MTA servers

[build]
  dockerfile = "Dockerfile"

[http_service]
  internal_port = 8000
  force_https = true

[[http_service.checks]]
  path = "/"
  interval = "30s"
  timeout = "5s"

Useful commands

fly status              # Check app status
fly logs                # View logs
fly ssh console         # SSH into the container
fly scale count 2       # Scale to 2 instances
fly secrets set KEY=val # Set environment variables

CLI

Query subway data from the command line. Output is JSON by default.

# Setup
source venv/bin/activate

# List all stations
python -m mta.cli stations

# Search stations by name
python -m mta.cli stations -q "Times Sq"

# Get station by ID (with arrivals)
python -m mta.cli stations 127

# Get multiple stations
python -m mta.cli stations 127,A32

# Search by location
python -m mta.cli stations --lat 40.7580 --lon -73.9855

# List all routes
python -m mta.cli routes

# Get stations on a route
python -m mta.cli routes A

# Find next trains from a station
python -m mta.cli find "Times Sq"

# Find specific route from a station
python -m mta.cli find "Times Sq" -r A

# Human-readable output (--pretty goes before the command)
python -m mta.cli --pretty stations 127

MCP Server

Run as a Model Context Protocol server for AI assistants:

python -m mta.mcp

Available Tools

Tool Description
get_stations Search stations by name or location
get_station Get station(s) by ID with arrivals
get_routes List all active subway routes
get_route Get all stations on a route
find_train Find next trains from a station

Claude Desktop Configuration

Add to ~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "mta-subway": {
      "command": "python",
      "args": ["-m", "mta.mcp"],
      "cwd": "/path/to/mta-api-v3"
    }
  }
}

Metadata

Release files for mta-subway 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mta-subway 1.0.0
File Size Uploaded Provenance
mta_subway-1.0.0.tar.gz 26.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mta-subway 1.0.0
File Interpreter ABI Platform
mta_subway-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 57.2 kB

Release files / mta_subway-1.0.0.tar.gz

Download URL mta_subway-1.0.0.tar.gz
Size 26.5 kB
Tags Source
SHA-256 checksum
How to use checksums
09d6e49ae69ae6c5b823c3ee1182a2f4806a1490da25280e60a57856bc9e0903
BLAKE2b-256 checksum
How to use checksums
0eb4b6ec51334d83a7b7275b263d33b4a273ebddefa716e4b9eb73cceb43fd8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.3

Release files / mta_subway-1.0.0-py3-none-any.whl

Download URL mta_subway-1.0.0-py3-none-any.whl
Size 30.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a2e2381421960124ac649836a5e24c61784d18a8d0bca87ad8194c0151824d9
BLAKE2b-256 checksum
How to use checksums
cfbd74cf4851a27801b532f6850a2dfacb8c3be488267c2d7d77c7ece27c7058
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.3

Security for mta-subway 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page