Bus API Beginner help

Hello all.

I am a beginner and not sure how to proceed.

Basically I want to write an app which will tell users when the next bus(es) are due at a stop.
I have come across a lot of information but I am not so clear on how to proceed … being new at this.

I took the following photo with the intention of investigating and finding an API to which I can supply the information shown and be able to get results back.

Is that how it works? I need some guidance, please.

1 Like

Grab this

http://data.tfl.gov.uk/tfl/syndication/feeds/bus-sequences.csv?app_id=&app_key=

You might need an app_id and app_key from your registration, but it works for me without.

It’s a CSV format.

I would recommend using the Unified API rather than the bus CSV which is a legacy feed. I would also recommend using an api_key and app_id at all times, as we may change the anonymous quota restrictions without warning, at any time.

You can find the bus stop referred to by the short code like this:

curl -I "https://api.tfl.gov.uk/StopPoint/Sms/73241"
HTTP/1.1 302 Found
...
Cache-Control: public, must-revalidate, max-age=302400, s-maxage=604800
Location: /StopPoint/490010260SD

If we follow that HTTP 302 redirect we get the full StopPoint info for that bus stop https://api.tfl.gov.uk/StopPoint/490010260SD

But actually since you want the arrivals, you can just put /Arrivals on the end of that URL

$ curl "https://api.tfl.gov.uk/StopPoint/490010260SD/Arrivals" | jq .
[
  {
    "id": "875851908",
    "operationType": 1,
    "vehicleId": "LTZ1052",
    "naptanId": "490010260SD",
    "stationName": "St James's Park Station",
    "lineId": "11",
    "lineName": "11",
    "platformName": "SD",
    "direction": "inbound",
    "bearing": "248",
    "destinationNaptanId": "",
    "destinationName": "Fulham Broadway",
    "timestamp": "2017-07-27T13:15:21Z",
    "timeToStation": 293,
    "currentLocation": "",
    "towards": "Pimlico, Sloane Square or Marble Arch",
    "expectedArrival": "2017-07-27T13:20:14Z",
    "timeToLive": "2017-07-27T13:20:44Z",
    "modeName": "bus"
  },
// etc.

There’s more information on the Arrivals API here.

Thank you Tim. That was very helpful.

A few more questions, please.

My queries are returning the following data:

Towards: Gants Hill or Highams Park

Destination: Ilford
Expected Arrival: 2017-07-28T07:49:30Z
Line: 123

Towards: Gants Hill or Highams Park
Destination: Ilford
Expected Arrival: 2017-07-28T07:58:49Z
Line: 123

Towards: Gants Hill or Highams Park
Destination: Ilford
Expected Arrival: 2017-07-28T08:08:42Z
Line: 123

Towards: Gants Hill or Highams Park
Destination: Barkingside
Expected Arrival: 2017-07-28T07:56:01Z
Line: 275

Towards: Gants Hill or Highams Park
Destination: Barkingside
Line: 275

I wonder about the date and time. Having split the expectedArrival data into date and time segments, I noticed the earliest arrival time predictions are all more than an hour late. By that I mean the time according to my computer is about an hour ahead so the expected arrival times are all in the past.
Is this due to day light saving?

Hi @sisko - you’re correct in your assumption, the times are output in UTC.

Thanks James.

However, even adding an hour to the earliest arrival times still leaves more than half an hour before the next bus arrives.

I wonder if there’s a way to request earlier bus arrival times?

It’s possible the next bus is in fact a half hour away but I think it is unlikely given how busy the area is.

HI @sisko, which StopPoint are you querying or can you paste the URL please?

Hey Tim.

Here’s the URL.

Calling that at 13:34 BST, 12:35 UTC, I get the first arrival at 12:40 UTC, timeToStation is 288 seconds which is just under 5 minutes away. Looks reasonable to me:

[
  {
    "id": "-689234962",
    "operationType": 1,
    "vehicleId": "LJ03MXK",
    "naptanId": "490014157E",
    "stationName": "Waltham Forest College",
    "lineId": "123",
    "lineName": "123",
    "platformName": "BR",
    "direction": "outbound",
    "bearing": "71",
    "destinationNaptanId": "",
    "destinationName": "Ilford",
    "timestamp": "2017-07-28T12:35:50Z",
    "timeToStation": 288,
    "currentLocation": "",
    "towards": "Gants Hill or Highams Park",
    "expectedArrival": "2017-07-28T12:40:38Z",
    "timeToLive": "2017-07-28T12:41:08Z",
    "modeName": "bus",

I agree but my first couple of examples this morning were a lot more distant in arrival time.

But perhaps I made a mistake. I’ll keep my eye on it and provide some more detailed information should it happen again.

Thank you Tim.

If you do notice anything weird again, capture the full response JSON as this also contains the request timestamp and other debugging information. Thanks!