Fetching my Garmin wellness data

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.

Authentication

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.

User “displayName”

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.

Retrieving data

The Garmin Connect application has several HTTP routes that allow it to retrieve the necessary data to display the different charts and statistics. The routes are called as HTTP requests from the JavaScript and return data in JSON format which is exactly what I want.

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:

  1. Open a browser and connect to the Garmin Connect website.
  2. Login into the application using your usual login and password.
  3. Retrieve the SESSIONID field from your cookie. Personally, I use the “EditThisCookie” extension for Google Chrome.
  4. Launch the script with the SESSIONID value as argument.
  5. The script will retrieve your data from yesterday to the MIN_DATE value in the script file.

The script

Conclusion

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.