Last Updated: 19 April 2021

UK Realtime Train Data

Install-Package ParkSquare.RealtimeTrains

This is a C# library for retrieving realtime train information from Network Rail, National Rail other data sources via the Realtime Trains API. It also provides accurate data regarding timetables, services, bus replacements and platform information.

How To Use

The source data feed is free for personal, academic and educational use. To connect to the Realtime Trains API you will need access credentials, which can be obtained by going to the Realtime Trains Developer portal. These credentials are in the form of a username and password, which this library will automatically add to your calls.

The main class you will need is RealTimeTrainsClient. To create one of these, you will need to pass in an HttpClient and an implementation of IClientConfig. It's up to you how you implement IClientConfig, for example you may bind it to entries in your appSettings.config or to secrets in an Azure Key Vault. Here is a simple example with hardcoded values:


    public class ClientConfig : IClientConfig
    {
        public string BaseUrl => "https://api.rtt.io/api/v1/";

        public string Username => "rttapi_yourusername";

        public string Password => "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    }

The recommended way to instantiate your RealTimeTrainsClient is to use Dependency Injection. To jog your memory, you can read about HttpClient dependency injection with .Net Core and then come back. Here is an example of how to register the required dependencies in your project:


    services.AddSingleton<IRealTimeTrainsClient, RealTimeTrainsClient>();
    services.AddSingleton<IClientConfig, ClientConfig>();
    services.AddHttpClient();

You can now access the RealTimeTrainsClient by simply adding an IRealTimeTrainsClient to the constructor of the class where you want to use it. This allows you to search for arrivals, departures, or get detailed information on a specific service. There are several overloaded methods that allow your search to be more, or less, precise. Here is an example that retrieves current departures from London Kings Cross, and then lists the realtime station data for the last service it finds.


    public class RealTimeTrainsDemo
    {
        private readonly IRealTimeTrainsClient _client;

        public RealTimeTrainsDemo(IRealTimeTrainsClient client)
        {
            _client = client ?? throw new ArgumentNullException(nameof(client));
        }

        public void Run()
        {
            var services = _client.GetDepartures("KGX");

            Console.WriteLine("Services departing from London King's Cross:");

            foreach (var service in services.Services)
            {
                var origin = service.LocationDetail.Origin.First();
                var destination = service.LocationDetail.Destination.Last();

                Console.WriteLine($"\t{service.RunningIdentity}\t{service.ServiceUid}\t" +
                $"{origin.PublicTime} {origin.Description} --> " +
                $"{destination.PublicTime} {destination.Description} " +
                $"({service.LocationDetail.Description})");
            }

            var lastService = services.Services.Last();

            var now = DateTime.Now;
            var serviceDetail = _client.GetService(lastService.ServiceUid, new DateFilter(now.Year, now.Month, now.Day));

            foreach (var callingPoint in serviceDetail.Locations)
            {
                Console.WriteLine($"\t\t\tRealtime Data: ARR {callingPoint.RealtimeArrival} DEP {callingPoint.RealtimeDeparture} AT {callingPoint.Description} PLATFORM {callingPoint.Platform}");
            }
        }
    }

For more information, see the Realtime Trains Location Listing documentation.

Note that 'Service Unique IDs' are only unique for a particular day, so you must also pass in the date you're interested in to this call. For more information, see the Realtime Trains Service Information documentation.

Example Usage

Check out a live example of this library in use by going to https://www.thetrain.rocks. Train Rocks is a fully responsive web app that uses this library to search for UK train services and report their status in real time.