garmin connect bulk delete in javascript

Here's an approach to bulk delete activities in Garmin Connect using javascript and the unofficial Garmin Connect API:

  1. First, we need to authenticate to get an access token using your Garmin Connect credentials:
index.tsx
const axios = require('axios');

const LOGIN_URL = 'https://sso.garmin.com/sso/signin';
const REDIRECT_URL = 'https://connect.garmin.com/modern';

const username = 'your_garmin_connect_username';
const password = 'your_garmin_connect_password';

async function authenticate() {
  const response1 = await axios.get(LOGIN_URL);
  const { name, value } = extractLoginResponseParams(response1.data);
  const response2 = await axios.post(LOGIN_URL, {
    embed: 'true',
    username,
    password,
    [name]: value,
  });
  const response3 = await axios.get(REDIRECT_URL, {
    maxRedirects: 0,
    validateStatus: status => status === 302,
  });
  const accessToken = extractAccessToken(response3.headers.location);
  return accessToken;
}

function extractLoginResponseParams(html) {
  // extract name and value from html to make login request with username and password
}

function extractAccessToken(url) {
  // extract access token from url fragment after authentication redirect
}
983 chars
33 lines
  1. After authentication, you can retrieve a list of activities using the access token and the /proxy/usersummary-service/usersummary/daily/{date}/activities endpoint. This endpoint retrieves the activities for the date parameter provided, which in this case can be set to a very wide range to retrieve all activities:
index.tsx
async function getActivities(accessToken) {
  const response = await axios.get(
    'https://connect.garmin.com/proxy/usersummary-service/usersummary/daily/20220101/activities',
    {
      headers: { 'Authorization': `Bearer ${accessToken}` },
    },
  );
  const activities = response.data.activitySummaryDTOList;
  return activities;
}
339 chars
11 lines
  1. Finally, you can delete each activity using the access token and the /proxy/activity-service/activity/{activity_id} endpoint for each activity:
index.tsx
async function deleteActivity(accessToken, activityId) {
  const response = await axios.delete(
    `https://connect.garmin.com/proxy/activity-service/activity/${activityId}`,
    {
      headers: { 'Authorization': `Bearer ${accessToken}` },
    },
  );
  return response.status === 204;
}
291 chars
10 lines

To bulk delete activities, you can simply iterate over the list of activities retrieved in step 2 and call deleteActivity for each one:

index.tsx
async function bulkDelete(accessToken) {
  const activities = await getActivities(accessToken);
  for (const activity of activities) {
    const success = await deleteActivity(accessToken, activity.activityId);
    console.log(success ? `Activity ${activity.activityId} deleted` : `Error deleting activity ${activity.activityId}`);
  }
}
338 chars
8 lines

Note that the above approach uses the unofficial Garmin Connect API, which may stop working if Garmin decides to change their API. Also, there are risks associated with using unofficial APIs as they may violate the terms of use of the service being accessed.

gistlibby LogSnag