Helsinki Tram-Departure API

I recently built a small hardware-device to show the departure times of the tram(s) near my house:

Unfortunately the project was built just as the official Helsinki API was going through a transition, from HTTP & JSON to GraphQL.

To avoid having to replace the code on the device itself I setup this simple API which I can poll instead, which will return a simple CSV set of data about the trams. That means I only have to update one location in the future, and also means I don't need to worry about anything complex on the device.

Tram API

Each tram-stop is identified by a numerical ID, and with that ID the following URL will return the next few departures for a given stop:

  • https://steve.fi/Helsinki/Tram-API/api.cgi?id=ID

For example the:

Source code is available here.

Implementation

The developer-documentation available gives you pretty much everything you need to get started, including a simple interactive query-runner (the graphql console).

You can visit the GraphQL development console, and paste in the following contents:

{
  stop(id: "HSL:1160404") {
    name
    code
    desc
    stoptimesWithoutPatterns(numberOfDepartures:10) {
      scheduledArrival
      realtimeArrival
      realtime
      timepoint
      trip {
        route {
          id
          shortName
          longName
         }
      }
    }
  }
}

The only part that was hard was figuring out how to specify the number of departures - adding (numberOfDepartures:10) in the example above. I found that via sniffing the requests the public map-site made, via my browser extension.

The code above is almost exactly what my simple API does, although there is a little more complexity.