SDKs overview
Transport for Los Angeles, US (LA Metro) provides public access to its transit data primarily through General Transit Feed Specification (GTFS) feeds. These feeds include both static data (schedules, routes, stops) and real-time information (vehicle positions, service alerts, trip updates). While LA Metro does not publish formal, language-specific SDKs in the traditional sense, it offers comprehensive API documentation and examples that guide developers in consuming these feeds directly. The developer portal serves as the central hub for accessing these resources and understanding the data structures. The approach aligns with common practices for public transit data, often relying on standardized data formats like GTFS, which can be parsed using various programming languages and libraries.
Developers typically interact with LA Metro's data by making HTTP requests to designated API endpoints and processing the GTFS-formatted responses. Authentication for these API calls is managed via an API key, which is obtained through the LA Metro developer portal. This key ensures controlled access and allows for usage monitoring.
Official SDKs by language
LA Metro primarily offers direct API access rather than formal, language-specific SDKs. The core products, GTFS Realtime API and GTFS Static Data, are delivered in standardized formats which developers can integrate using HTTP clients and parsers available in most programming languages. This model provides flexibility, allowing developers to choose their preferred tools and frameworks. The LA Metro developer portal provides extensive documentation and cURL examples to demonstrate API interactions.
The following table outlines the primary methods and tools for interacting with LA Metro's data, reflecting the official guidance and available resources:
| Language/Method | Description | Recommended Approach | Maturity |
|---|---|---|---|
| cURL | Command-line tool for making HTTP requests. Used for direct API interaction and testing. | Direct HTTP requests | Stable (API-level) |
| Python | High-level programming language with extensive libraries for HTTP requests and data parsing. | requests library for HTTP, gtfs-realtime-bindings for GTFS parsing |
Stable (Community libraries) |
| JavaScript | Client-side and server-side language for web applications. | fetch API or axios for HTTP, GTFS parsing libraries |
Stable (Community libraries) |
| Other Languages | Any language with HTTP client capabilities (e.g., Java, Ruby, Go, PHP, C#). | Standard HTTP client libraries and GTFS parsing libraries specific to the language | Stable (Language-specific tools) |
Installation
Since LA Metro's primary interface is direct API access, installation typically involves setting up HTTP client libraries and, for GTFS Realtime data, protocol buffer parsing libraries in your chosen programming environment.
Python
For Python, the requests library is commonly used for making HTTP requests, and the gtfs-realtime-bindings library is essential for parsing GTFS Realtime data, which is based on Google's Protocol Buffers.
pip install requests
pip install gtfs-realtime-bindings
The gtfs-realtime-bindings library can be found on the Python Package Index.
JavaScript (Node.js)
For Node.js environments, axios or the built-in fetch API (available in recent Node.js versions) can handle HTTP requests. Parsing GTFS Realtime data requires a protobuf.js library or similar.
npm install axios
npm install protobufjs
Consult the protobuf.js npm package documentation for usage details.
cURL
cURL is typically pre-installed on most Unix-like operating systems. No specific installation steps are usually required.
# Check if cURL is installed
curl --version
Quickstart example
This example demonstrates how to fetch and parse GTFS Realtime data for LA Metro using Python. It retrieves vehicle position data.
import requests
from google.transit import gtfs_realtime_pb2
import os
# Replace with your actual API key from developer.metro.net
# It's recommended to use environment variables for API keys in production
METRO_API_KEY = os.getenv("METRO_API_KEY", "YOUR_METRO_API_KEY")
# GTFS Realtime Vehicle Positions feed URL for LA Metro
# Check the LA Metro developer portal for the most current URLs
VEHICLE_POSITIONS_URL = "http://api.metro.net/agencies/lametro/vehicles/"
headers = {
"Accept": "application/x-protobuf",
"Authorization": f"api_key {METRO_API_KEY}"
}
def get_vehicle_positions():
try:
response = requests.get(VEHICLE_POSITIONS_URL, headers=headers, timeout=10)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
feed = gtfs_realtime_pb2.FeedMessage()
feed.ParseFromString(response.content)
print(f"Successfully fetched {len(feed.entity)} vehicle entities.")
for entity in feed.entity:
if entity.HasField('vehicle'):
vehicle = entity.vehicle
print(f" Vehicle ID: {entity.id}")
print(f" Trip ID: {vehicle.trip.trip_id}")
print(f" Route ID: {vehicle.trip.route_id}")
print(f" Position: Lat {vehicle.position.latitude}, Lon {vehicle.position.longitude}")
print(f" Bearing: {vehicle.position.bearing}")
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err} - Response: {response.text if hasattr(response, 'text') else 'N/A'}")
except requests.exceptions.ConnectionError as conn_err:
print(f"Connection error occurred: {conn_err}")
except requests.exceptions.Timeout as timeout_err:
print(f"Timeout error occurred: {timeout_err}")
except requests.exceptions.RequestException as req_err:
print(f"An error occurred during the request: {req_err}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
# Ensure you set your METRO_API_KEY environment variable or replace the placeholder
if METRO_API_KEY == "YOUR_METRO_API_KEY":
print("WARNING: Please set your METRO_API_KEY environment variable or replace the placeholder in the script.")
get_vehicle_positions()
This script demonstrates fetching vehicle positions. For other GTFS Realtime feeds like trip updates or service alerts, the process is similar, but you would use different API endpoints and potentially different parsing logic based on the GTFS Realtime specification outlined by Google Transit documentation.
Community libraries
Given the standardized nature of GTFS data, a variety of community-developed libraries exist across different programming languages to facilitate parsing and working with GTFS (both static and Realtime) feeds. These libraries are not officially endorsed or maintained by LA Metro but are widely used within the transit developer community.
- Python:
gtfs-realtime-bindings: Official Google-maintained Python bindings for the GTFS Realtime Protocol Buffer specification. Essential for parsing real-time data.gtfs-kit: A Python package for analyzing GTFS static data.pandas-gtfs: Integrates GTFS static data with the Pandas data analysis library.
- JavaScript/TypeScript:
gtfs-rt-bindings(npm package): JavaScript bindings for GTFS Realtime protocol buffers.gtfs-parser: A Node.js library for parsing GTFS static data.protobufjs: A general-purpose Protocol Buffer library that can be used to parse GTFS Realtime data messages.
- Java:
onebusaway-gtfs-realtime-api: Part of the OneBusAway project, provides Java bindings for GTFS Realtime.gtfs-lib: A Java library for reading and writing GTFS static data.
- Ruby:
gtfs-realtime.rb: Ruby bindings for GTFS Realtime.
When using community libraries, developers should review their documentation, community support, and maintenance status to ensure they meet project requirements. The LA Metro API overview provides details on the specific GTFS feeds available.