delete all garmin activities in javascript

To delete all Garmin activities using JavaScript, you would need to use the Garmin Connect API. Here's an example code snippet that demonstrates how to do this using the fetch API:

index.tsx
const username = 'your_garmin_username';
const password = 'your_garmin_password';

// Step 1: authenticate with Garmin Connect API
fetch('https://sso.garmin.com/sso/signin', {
  method: 'POST',
  body: JSON.stringify({
    username,
    password,
    embed: true,
    rememberMe: true,
  }),
  headers: {
    'Content-Type': 'application/json',
  },
}).then(response => {
  if (response.ok) {
    // Step 2: get activities
    fetch('https://connect.garmin.com/modern/proxy/activitylist-service/activities/search/activities', {
      method: 'POST',
      credentials: 'include',
      cache: 'no-cache',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        start: 0,
        limit: 1000, // increase limit if you have more than 1000 activities
        activityTypeDTO: {
          typeKey: 'all',
          parentTypeKey: 'all',
        },
        dateRangeDTO: {},
        activityStatusDTO: {
          statusKey: 'all',
        },
        trainingPlanDTO: {},
        orderBy: 'startTime',
        isDeleted: false,
        detailed: true,
        fields: 'activitySummary,activityType,device,owner',
      }),
    }).then(response => response.json())
      .then(data => {
        // Step 3: delete activities
        Promise.all(data.activities.map(activity => {
          return fetch(`https://connect.garmin.com/modern/proxy/activity-service/activity/${activity.activityId}`, {
            method: 'DELETE',
            credentials: 'include',
            headers: {
              'Content-Type': 'application/json',
            },
          });
        })).then(() => console.log('All activities deleted successfully!'))
          .catch(error => console.error(error));
      })
      .catch(error => console.error(error));
  } else {
    console.error('Failed to authenticate with Garmin Connect API');
  }
})
.catch(error => console.error(error));
1913 chars
63 lines

Note that this code assumes that your Garmin username and password are stored in the username and password variables, respectively, and that you have already registered an app with the Garmin Connect API and obtained an API key.

related categories

gistlibby LogSnag