A Python wrapper for the Realtime Trains API v1.
This library simplfies the process of making a call to the Realtime Trains API.
Requests to specific endpoints can be made from the RttApi
class, with the returned
JSON data parsed and wrapped into a data object.
For now this is a literal mapping of the Realtime Trains API data structure into Python data classes. Helper functionality may be added in future.
In order to make requests from this library you must first sign up for a Realtime Trains API account. You can do so at api.rtt.io.
IMPORTANT - Note that you must use the API Auth Credentials as displayed within your Realtime Trains API account, not the credentials you use to log into the website.
Create an instance of an RttApi
object using your Realtime Trains API auth credentials
like so:
from rttapi.api import RttApi
api = RttApi('rttapi_exampleuser', '00112233aabbccdd')
To request the list of upcoming departures from Clapham Junction station:
departures = api.search_station_departures('CLJ')
This will return a SearchResult
object with containing the station details and the list of
upcoming departures. You may either search using the three-letter CRS (computer reservation system)
code, or a longer TIPLOC (timing point location) code. You can find a station's CRS code by
searching on the National Rail website.
Arrivals can also be queried by using:
arrivals = api.search_station_arrivals('CLJ')
Which again returns a SearchResult
object.
A detailed breakdown on the information returned can be found on the Realtime Trains
API documentation page.
This library mirrors the data returned by the API, albeit using Pythonesque underscore_case
properties instead of the camelCase
properties returned by the API.
Detailed journey information for an individual service can be queried using:
service_info = api.fetch_service_info_datetime(service_uid, service_date)
where:
service_uid
is the unique identifier of the service obtained from theSearchResult
e.g.8U09FW
service_datetime
is adatetime.date
object representing the date on which the service is due to depart.
Alternatively, the year, month and day of departure can be explicitly set (e.g. 27th March 2021) by using:
service_info = api.fetch_service_info_ymd('8U09FW', '2021', '3', '27')
Both of these examples will return a Service
object, which is explained in more detail
on the Realtime Trains API documentation page.
A more detailed example on how to use this library can be found in my pyRailTimes project.
With thanks to the Realtime Trains API team.