I had Garmin watches for years now. The last one is recording my “wellness data” (steps, heart rate) and publishing it to Garmin’s servers. While the Garmin Connect website is great I want to get a backup of these data on my own computer.
Garmin has a Health API, but requesting access requires a painful process. So, I decided to write my own script to fetch the data.
In this article I detail the process used and how I came to choose BASH and curl as the best technology.
When I first try to backup the data, I used Puppeteer. This is a NodeJS module that allows to drive Chromium from NodeJS. I used it to navigate to the Garmin Connect application, enter my login and password, then perform the data retrieval. However, the process is heavier than what it should be, and there is no Puppeteer version for my Raspberry Pi.
So I decided to switch to BASH and curl to make the script lighter and compatible with the ARM architecture. The process is a bit harder though. Using curl requires to mimic the browser behavior, especially while handling the form submission and cookies.
The Garmin Connect authentication form is embedded inside an IFrame. Its URL is:
https://sso.garmin.com/sso/signin?…lots of arguments…
I did not managed to understand exactly how yet, but this ends with a SESSIONID parameter wrote to the cookie. I discovered that this is the only parameter that is really needed by the application to authenticate the user.
At this time, I’m not yet available to retrieve the SESSIONID using curl. This will force you to manually login and get the SESSIONID value form your cookie, for example using the “EditThisCookie” extension.
I found that the user id, required in the URLs is defined in the HTML code returned for the main application. So in fact, the HTML loaded is personalized with some personal information exported as script.
I inspected the different pages and selected 5 routes that allow me to retrieve : the daily summary, the heart rate data, the steps data, the movements data and the list of activities. I believe that it should be possible to retrieve the full activities data but I am not interested by those data yet.
As mentioned earlier, you will require the user “displayName” to form the URL and the SESSIONID value from the cookie to retrieve the data.
Running the script
So I wrote a simple BASH script to perform the necessary curl requests to retrieve the user “displayName” and fetch the data in JSON format. The code is available below and on Github.
This are the instructions to prepare the required data and launch it:
- Open a browser and connect to the Garmin Connect website.
- Login into the application using your usual login and password.
- Retrieve the SESSIONID field from your cookie. Personally, I use the “EditThisCookie” extension for Google Chrome.
- Launch the script with the SESSIONID value as argument.
- The script will retrieve your data from yesterday to the MIN_DATE value in the script file.
Writing the code was really funny, specially trying to understand the authentication process. I will continue to investigate and learn new things. Hopefully, I will be capable to figure out the whole authentication process and make a fully autonomous script in a near future.