Use connectapi.garmin.com directly instead of web proxy
All checks were successful
Deploy / deploy (push) Successful in 1m38s

The connect.garmin.com/modern/proxy URL returns HTML (website) instead
of JSON API responses. Garth library uses connectapi.garmin.com subdomain
directly, which is the actual API endpoint.

- Change base URL from connect.garmin.com/modern/proxy to connectapi.garmin.com
- Update User-Agent to match garth library: GCM-iOS-5.19.1.2
- Factor out headers into getGarminHeaders() to avoid duplication
- Remove NK header (not needed when using connectapi subdomain)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-15 13:38:55 +00:00
parent 51f4c8eb80
commit 59d70ee414
2 changed files with 27 additions and 41 deletions

View File

@@ -4,10 +4,15 @@
import { logger } from "@/lib/logger";
import type { GarminTokens, HrvStatus } from "@/types";
const GARMIN_BASE_URL = "https://connect.garmin.com/modern/proxy";
// Use connectapi subdomain directly (same as garth library)
const GARMIN_API_URL = "https://connectapi.garmin.com";
interface GarminApiOptions {
oauth2Token: string;
// Headers matching garth library's http.py USER_AGENT
function getGarminHeaders(oauth2Token: string): Record<string, string> {
return {
Authorization: `Bearer ${oauth2Token}`,
"User-Agent": "GCM-iOS-5.19.1.2",
};
}
export interface BodyBatteryData {
@@ -17,14 +22,10 @@ export interface BodyBatteryData {
export async function fetchGarminData(
endpoint: string,
options: GarminApiOptions,
oauth2Token: string,
): Promise<unknown> {
const response = await fetch(`${GARMIN_BASE_URL}${endpoint}`, {
headers: {
Authorization: `Bearer ${options.oauth2Token}`,
NK: "NT",
"User-Agent": "GCM-iOS-5.7.2.1",
},
const response = await fetch(`${GARMIN_API_URL}${endpoint}`, {
headers: getGarminHeaders(oauth2Token),
});
if (!response.ok) {
@@ -58,12 +59,8 @@ export async function fetchHrvStatus(
oauth2Token: string,
): Promise<HrvStatus> {
try {
const response = await fetch(`${GARMIN_BASE_URL}/hrv-service/hrv/${date}`, {
headers: {
Authorization: `Bearer ${oauth2Token}`,
NK: "NT",
"User-Agent": "GCM-iOS-5.7.2.1",
},
const response = await fetch(`${GARMIN_API_URL}/hrv-service/hrv/${date}`, {
headers: getGarminHeaders(oauth2Token),
});
if (!response.ok) {
@@ -113,13 +110,9 @@ export async function fetchBodyBattery(
): Promise<BodyBatteryData> {
try {
const response = await fetch(
`${GARMIN_BASE_URL}/usersummary-service/stats/bodyBattery/dates/${date}`,
`${GARMIN_API_URL}/usersummary-service/stats/bodyBattery/dates/${date}`,
{
headers: {
Authorization: `Bearer ${oauth2Token}`,
NK: "NT",
"User-Agent": "GCM-iOS-5.7.2.1",
},
headers: getGarminHeaders(oauth2Token),
},
);
@@ -172,13 +165,9 @@ export async function fetchIntensityMinutes(
): Promise<number> {
try {
const response = await fetch(
`${GARMIN_BASE_URL}/fitnessstats-service/activity`,
`${GARMIN_API_URL}/fitnessstats-service/activity`,
{
headers: {
Authorization: `Bearer ${oauth2Token}`,
NK: "NT",
"User-Agent": "GCM-iOS-5.7.2.1",
},
headers: getGarminHeaders(oauth2Token),
},
);