Transport for Sweden
TransportationIntroduction to Trafiklab API
Trafiklab API is a public API that provides access to public transport data in Sweden. It offers a range of APIs that can be used to retrieve data such as real-time vehicle positions, traffic disturbances, timetables, and much more.
The API offers a variety of endpoints that can be accessed through HTTPS requests with JSON responses or by using SOAP. In this blog post, we will look at some examples of how to use the API in JavaScript.
Getting Started
Before we start, we need to obtain an API key from the Trafiklab Developer Portal. Once you have the API key, you can start making requests to the API.
In your JavaScript code, you will need to include the fetch function to make HTTP requests. Here's an example:
fetch('https://api.trafiklab.se/some-endpoint?key=your-api-key')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
Replace some-endpoint
with the specific endpoint you want to call. For example, if you want to retrieve a list of bus stops, you can use the SL Hållplatsinformation 2
API endpoint:
fetch('https://api.trafiklab.se/sl-hallplatser-och-linjer-2/hallplatser?key=your-api-key&lat=59.33258&lng=18.0649')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
This endpoint returns a list of bus stops near the specified latitude and longitude.
Example API Endpoints
Here are some other example Trafiklab API endpoints you can use in your JavaScript code:
SL Realtime Trafikinformation 3
This API endpoint provides real-time information about public transport in Sweden. You can retrieve information such as vehicle positions, delays, and disruptions.
fetch('https://api.trafiklab.se/samtrafiken/resrobot/StationsAndPlaces/Search/Find?apiKey=your-api-key&searchString=stockholm')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
SL Hållplatsinformation 2
This API endpoint provides information about public transport stops in Sweden. You can retrieve information such as bus and train schedules.
fetch('https://api.trafiklab.se/sl-hallplatser-och-linjer-2/hallplatser?key=your-api-key&lat=59.33258&lng=18.0649')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
SL Störningsinformation 2
This API endpoint provides information about traffic disruptions in Sweden. You can retrieve information about accidents, maintenance work, etc.
fetch('https://api.trafiklab.se/sl-storningsinformation-2/grovStorningar?key=your-api-key')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
Conclusion
Trafiklab API is a great resource for anyone looking to build public transport apps or services in Sweden. With its range of API endpoints, you can retrieve all the information you need about public transport in Sweden. In this blog post, we have explored some examples of how to use the API in JavaScript.