Learn to seamlessly integrate Lovable with Garmin Connect using our step-by-step guide. Sync your devices and boost performance effortlessly.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
garminConnect.ts
.garminConnect.ts
:
export class GarminConnect {
private clientId: string;
private redirectUri: string;
private authUrl: string;
private tokenUrl: string;
private scope: string;
constructor(clientId: string, redirectUri: string) {
this.clientId = clientId;
this.redirectUri = redirectUri;
// Garmin Connect OAuth2 endpoints (update these if Garmin updates their API)
this.authUrl = 'https://connect.garmin.com/oauth2/authorize';
this.tokenUrl = 'https://connect.garmin.com/oauth2/token';
// Define the scopes your application needs (modify as required)
this.scope = 'activity heartrate';
}
// Generates the URL where users need to login and authorize your app
public getAuthorizationUrl(): string {
const params = new URLSearchParams({
client_id: this.clientId,
redirect_uri: this.redirectUri,
response_type: 'code',
scope: this.scope,
});
return ${this.authUrl}?${params.toString()};
}
// Exchanges the authorization code obtained from Garmin for an access token
public async exchangeCodeForToken(code: string): Promise {
// Prepare the request payload. Replace 'YOURCLIENTSECRET' with your actual secret.
const body = new URLSearchParams({
granttype: 'authorizationcode',
code: code,
redirect_uri: this.redirectUri,
client_id: this.clientId,
clientsecret: 'YOURCLIENT_SECRET'
});
const response = await fetch(this.tokenUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: body.toString(),
});
return response.json();
}
}
main.ts
).
import { GarminConnect } from './garminConnect';
const CLIENTID = 'YOURGARMINCLIENTID';
const REDIRECTURI = 'YOURREDIRECT_URI';
const garmin = new GarminConnect(CLIENTID, REDIRECTURI);
connectGarminBtn
, add a click event listener to redirect users to the Garmin Connect authorization URL. Insert the following code in your main file:
document.getElementById('connectGarminBtn')?.addEventListener('click', () => {
// Redirect the user to Garmin Connect's OAuth2 authorization page
window.location.href = garmin.getAuthorizationUrl();
});
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
if (code) {
garmin.exchangeCodeForToken(code)
.then(token => {
console.log('Garmin Connect token:', token);
// Here you can save the token to local storage or state to use in subsequent API calls
})
.catch(err => {
console.error('Error exchanging code for token:', err);
});
}
fetch
API, so no additional dependency installs are needed.<script>
tag or similar loader in your project configuration.
garminConnect.ts
file and that your main application file includes the integration code as described.connectGarminBtn
if it does not already exist. For example:
<button id="connectGarminBtn">Connect to Garmin</button>
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.