SDKs overview
Transport for Vancouver, Canada, operating under the brand TransLink, provides open data and APIs to facilitate the integration of its public transit information into third-party applications and services. The primary method for developers to interact with TransLink's data programmatically is through its RESTful APIs, which deliver information such as real-time vehicle locations, estimated arrival times, and static schedule data. While TransLink does not currently offer officially maintained, language-specific Software Development Kits (SDKs) in the traditional sense, it provides extensive API documentation for developers, including sample code snippets in several programming languages to assist with direct API consumption. This approach allows developers to use standard HTTP client libraries available in their preferred language to interact with the TransLink APIs, abstracting the need for a dedicated SDK. The API key registration process is designed to be straightforward, granting access for basic usage scenarios.
Developers commonly build custom client libraries or utilize existing generic HTTP request libraries to interact with the TransLink API. This section will detail common approaches to integrate with TransLink's APIs, including example installation patterns for typical programming environments and a quickstart guide using a widely supported language. The TransLink developer portal serves as the central hub for all technical documentation, API specifications, and support resources.
Official SDKs by language
TransLink's developer program focuses on providing well-documented RESTful APIs rather than distributing language-specific SDKs. This model requires developers to make direct HTTP requests to the API endpoints. However, the documentation includes sample code in C#, Java, PHP, and Python, demonstrating how to construct these requests and parse responses. The table below outlines the common client libraries used to interact with REST APIs in these languages, which serve a similar function to an SDK by simplifying HTTP communication.
| Language | Common HTTP Client | Installation Command (Example) | Maturity |
|---|---|---|---|
| C# | HttpClient (built-in) |
No external package needed for basic usage |
Stable (part of .NET) |
| Java | OkHttp | Add to pom.xml or build.gradle |
Stable, widely used |
| PHP | Guzzle HTTP Client | composer require guzzlehttp/guzzle |
Stable, widely used |
| Python | Requests | pip install requests |
Stable, widely used |
Installation
As TransLink primarily exposes RESTful APIs, installation typically involves adding an HTTP client library to your project rather than installing a dedicated TransLink SDK. Below are common installation instructions for the recommended HTTP client libraries in several popular programming languages. These clients handle the underlying network communication, allowing developers to focus on constructing the correct API requests and processing the responses.
Python (Requests library)
pip install requests
The Requests library for Python simplifies HTTP requests, making integration with RESTful APIs straightforward.
JavaScript (Fetch API)
// No installation needed for modern browsers/Node.js >= 18
// For older Node.js or polyfills:
npm install node-fetch
The Fetch API is a standard for making network requests in web browsers and increasingly in Node.js environments. For environments where Fetch is not natively available, libraries like node-fetch can provide compatibility.
Java (OkHttp)
If using Maven, add the following to your pom.xml:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
For Gradle, add to your build.gradle:
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
OkHttp is a popular HTTP client for Java and Android, offering efficient and robust network communication.
PHP (Guzzle HTTP Client)
composer require guzzlehttp/guzzle
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.
Quickstart example
This Python example demonstrates how to retrieve the current estimated arrival times for a specific stop using the TransLink API. You will need to replace YOUR_API_KEY with an actual API key obtained from the TransLink developer registration page.
import requests
import json
# Replace with your actual API key and desired stop number
API_KEY = "YOUR_API_KEY"
STOP_NUMBER = "50993" # Example: Marine Drive Station (Southbound)
BASE_URL = "http://api.translink.ca/rttiapi/v1/" # Note: TransLink uses HTTP, not HTTPS for this API version
ENDPOINT = "stops/{}/estimates".format(STOP_NUMBER)
params = {
"apikey": API_KEY,
"timeframe": "60" # Get estimates for the next 60 minutes
}
headers = {
"Accept": "application/json"
}
try:
response = requests.get(BASE_URL + ENDPOINT, params=params, headers=headers)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
data = response.json()
if data:
print(f"Estimated Arrivals for Stop {STOP_NUMBER}:")
for route_estimate in data:
route_no = route_estimate.get('RouteNo')
route_name = route_estimate.get('RouteName')
schedules = route_estimate.get('Schedules', [])
print(f"\nRoute: {route_no} - {route_name}")
if schedules:
for schedule in schedules:
destination = schedule.get('Destination')
expected_countdown = schedule.get('ExpectedCountdown', 'N/A')
schedule_status = schedule.get('ScheduleStatus')
print(f" - To {destination}: {expected_countdown} minutes ({schedule_status})")
else:
print(" No schedules available.")
else:
print(f"No data returned for Stop {STOP_NUMBER}.")
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
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: {req_err}")
except json.JSONDecodeError:
print(f"Error decoding JSON from response: {response.text}")
This script first defines the API key and a target stop number. It then constructs the URL and parameters for the TransLink Get Estimates API endpoint. The requests.get() call sends the request, and the response.json() method parses the JSON response. Error handling is included to catch common issues such as network problems or invalid API responses. The output details the estimated arrival times for different routes at the specified stop.
Community libraries
While TransLink does not provide official SDKs, the open nature of its APIs has fostered the development of various community-contributed libraries and wrappers. These libraries are typically hosted on platforms like GitHub and often provide more idiomatic ways to interact with the TransLink API in specific programming languages than direct HTTP calls. Developers interested in these resources should search public code repositories using terms like "TransLink API Python," "TransLink SDK Java," or similar queries for their preferred language.
Key considerations when using community libraries include:
- Maintenance Status: Community projects can vary in their level of active maintenance. Check the last commit date, issue tracker, and contributing guidelines.
- Feature Set: Ensure the library covers the specific TransLink API endpoints you need to access. Some libraries might only implement a subset of the available APIs.
- Error Handling: Evaluate how the library handles API errors, rate limits, and other potential issues.
- Documentation: Good community libraries provide clear documentation and examples, similar to official SDKs.
- Licensing: Understand the licensing terms of any community library before integrating it into your project.
Examples of common approaches to finding and evaluating such libraries include searching GitHub for TransLink API clients. Although not officially supported, these libraries can significantly accelerate development by abstracting API specifics and providing higher-level functions.
Developers are encouraged to contribute back to the community by sharing their own wrappers or improvements, fostering a collaborative ecosystem around TransLink's open data initiatives.