Till innehållet
kart-api.se

Live public transport on a map

curl 'https://api.kart-api.se/v1/trafik'         # the operators
curl 'https://api.kart-api.se/v1/trafik/sl'      # vehicles right now

The response is GeoJSON — add it as a source in MapLibre and refresh every 30 seconds:

setInterval(async () => {
  const d = await (await fetch('https://api.kart-api.se/v1/trafik/sl')).json();
  map.getSource('vehicles').setData(d);
}, 30000);

Each vehicle carries bearing, speed in m/s, the route short name and headsign when the mapping exists, and route_type (0 tram, 1 metro, 2 rail, 3 bus, 4 ferry).

Six counties have no open feed

/v1/trafik answers with saknar_flode — the county codes without real-time data at Trafiklab. Västra Götaland, and therefore Gothenburg, is one of them.

Write that out for your user. An empty layer looks exactly like "nothing is moving", and the difference between "we don't know" and "it is standing still" is the whole point of a live map.

Freshness

feed_ts is the operator's timestamp, hamtad is ours. If they drift apart, the feed is behind — show that rather than letting the map look live.