Skip to content

feat: integrate Metrolinx Open Data API for live service alerts#18

Open
faizm10 wants to merge 12 commits into
mainfrom
feat/metrolinx-api
Open

feat: integrate Metrolinx Open Data API for live service alerts#18
faizm10 wants to merge 12 commits into
mainfrom
feat/metrolinx-api

Conversation

@faizm10

@faizm10 faizm10 commented May 23, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds client/lib/metrolinx.ts — a typed API client for the Metrolinx Open Data API (service alerts, stop departures, stop details), keeping the API key server-side
  • Adds two Next.js API proxy routes: GET /api/metrolinx/alerts and GET /api/metrolinx/departures?stop=<code>&mode=departures|next
  • Upgrades fetchServiceUpdates to use the Metrolinx API as the primary source (with the existing gotransit.com HTML scrape as automatic fallback) — no downtime if the key is missing
  • Adds a SourceBadge to the Service Updates page showing "Live — Metrolinx API" vs "Scraped — gotransit.com" vs "Source unavailable"
  • Adds an at-a-glance stats row (active alerts / delays / cancellations) at the top of the page
  • Upgrades AlertCard to severity-coloured borders and expandable "Read more / Show less" body text for long alerts

Test plan

  • Set METROLINX_API_KEY in .env.local and verify the Service Updates page shows the green "Live — Metrolinx API" badge
  • Remove/unset the key and confirm the page falls back to the scrape source (amber badge) without errors
  • Hit GET /api/metrolinx/alerts directly and confirm JSON is returned with cache headers
  • Hit GET /api/metrolinx/departures?stop=UN and confirm departure data comes back
  • Verify long alert bodies show "Read more" expand/collapse button
  • Confirm alerts with no active updates render cleanly (empty state)

🤖 Generated with Claude Code

faizm10 and others added 11 commits May 20, 2026 22:38
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Replaces the gotransit.com HTML scraper with the official Metrolinx API
(key: env METROLINX_API_KEY) for service alerts, with the scraper kept as
a graceful fallback when the API is unavailable.

- lib/metrolinx.ts: typed API client for the Metrolinx Open Data API
  at api.openmetrolinx.com — covers service alerts, stop departures,
  next service, and stop details. All requests server-side only.
- lib/serviceUpdates.ts: fetchServiceUpdates() now tries the real API
  first; falls back to gotransit.com scrape if METROLINX_API_KEY is
  missing or the API errors
- app/api/metrolinx/departures/route.ts: proxy endpoint for live stop
  departures (?stop=UN&mode=departures|next), cached 60 s
- app/api/metrolinx/alerts/route.ts: raw alerts proxy, cached 5 min

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- serviceUpdates.ts: adds "metrolinx-api" source type so the UI can
  distinguish real API data from the HTML scraper fallback
- service-updates/page.tsx: source-aware badge ("Live — Metrolinx API"
  vs "Scraped — gotransit.com"), active alert stats row (total / delays /
  cancellations), and footer attribution switches between Metrolinx API
  docs and gotransit.com based on actual data source
- AlertCard.tsx: "use client" expand/collapse for long alert bodies
  (real API responses are often verbose), per-severity card border tint,
  whitespace-pre-line to preserve line breaks in API body text

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vercel

vercel Bot commented May 23, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
transit-flow Error Error May 28, 2026 5:00am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant