Trafikverket Helper
5 minute read
As I was about to get my drivers license, I was looking for ways to speed up the process. I found that the Swedish Transport Agency (Trafikverket) had a system where you could book a time for your drivers license test, and although there is no public API (as far as I know), the private API was simple enough to reverse engineer.
Tools Used
- Python: The primary programming language.
- Fiddler: Used to intercept and analyze the network traffic between the client and the server.
Method
Overview
If you have ever used the Trafikverket website, you know that it is not the most user-friendly. The booking system is no exception. To find a time that suits you, you have to manually click through all locations on a map to find available times.
As all locations close to me were fully booked for the next months, I was forced to look at other parts of the country. This was obviously very frustrating to do through the website, and I thought that there must be a better way to do this.
Mapping the API
I started by intercepting the network traffic between the client and the server using Fiddler. I then looked at the requests and responses to see if I could find the API that the website was using. Although users are required to log in using BankID before looking for available times, the API does not require any authentication at all.
Note: I'm not sure this is intended or not, but this also means you can scan through and extract details about drivers licenses, test results, and so on, given only a full name or SSN (without BankID).
The list of locations and prices was available through a simple GET request to https://fp.trafikverket.se/Boka/occasion-bundles
with the specific locationId
of the location you wanted to book at. The response was a JSON object with all available times, prices, and other details. To get the prices for all locations, I simply looped through all locations and saved the prices in memory.
Logging server changes
I then created a simple script that would run every 5 minutes and check if there were any changes to the server. If there were, it would indicate this with a colorfull printout in the terminal. This way I could see if there were any new times available without having to check the website manually. After running the script for a while, I found that many "good" times were released at really awkward times, like 3 AM on a thursday. There were also many times that were released in batches far in advance.
I also found that it was common for people to unbook their times, likely because they felt unprepared for the test. This was indicated by single new times popping up, often shortly before the test was scheduled to start.
Sometimes really good times would be released in the middle of the night, and shortly thereafter be booked by someone else. Although I have no proof, I suspect that some people have automated systems to book times for them. I wanted to analyze this further, but there were very large quantities of data to go through (gigabytes per day!), and I quickly ran into problems with storage and processing power. I leave this to further research, as the academics say.
Result
This project helped me find a time for my drivers license test much faster than I would have been able to do manually, at a time where times were scarce. I also learned a lot about how the Trafikverket booking system works, and how it could be improved.
You can find the code for this project on my GitHub: trafikverket-helper
Update 2024-05-04: As I wrote this post, I tried the bot again, and it seems that the API has been updated to require a valid capcha token. This means that the bot is no longer functional, and I have archived the project. You can still use the code as a base for your own project, but you will have to solve the capcha problem yourself. I might look into this in the future, as it's definetly doable, but for now I have other projects that I want to focus on.