Skip to main content
Glama

getHeartRate

Retrieve heart rate data from Fitbit for specified dates or periods to monitor cardiovascular activity and track fitness progress.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dateNoDate in YYYY-MM-DD format. If not specified, will use today.
periodNoPeriod for heart rate data: 1d, 7d, 30d

Implementation Reference

  • The handler function implementing the getHeartRate tool logic. It formats the input date, builds the appropriate Fitbit API endpoint for heart rate data (using 1d period by default), fetches the data, and returns a structured JSON response or an error message.
    async ({ date, period }) => { try { const formattedDate = formatDate(date); let endpoint = ""; if (period) { endpoint = `/user/-/activities/heart/date/${formattedDate}/${period}.json`; } else { endpoint = `/user/-/activities/heart/date/${formattedDate}/1d.json`; } const data = await makeApiRequest(endpoint); return { content: [ { type: "text", text: JSON.stringify( { date: formattedDate, heartRate: data["activities-heart"] || [], intraday: data["activities-heart-intraday"] || {}, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } }
  • Zod input schema defining optional parameters 'date' (YYYY-MM-DD) and 'period' (1d, 7d, 30d) for the getHeartRate tool.
    { date: z .string() .optional() .describe("Date in YYYY-MM-DD format. If not specified, will use today."), period: z .string() .optional() .describe("Period for heart rate data: 1d, 7d, 30d"), },
  • src/server.ts:197-252 (registration)
    The registration of the getHeartRate tool on the MCP server, including the tool name, input schema, and inline handler function.
    // Register tool for getting heart rate data server.tool( "getHeartRate", { date: z .string() .optional() .describe("Date in YYYY-MM-DD format. If not specified, will use today."), period: z .string() .optional() .describe("Period for heart rate data: 1d, 7d, 30d"), }, async ({ date, period }) => { try { const formattedDate = formatDate(date); let endpoint = ""; if (period) { endpoint = `/user/-/activities/heart/date/${formattedDate}/${period}.json`; } else { endpoint = `/user/-/activities/heart/date/${formattedDate}/1d.json`; } const data = await makeApiRequest(endpoint); return { content: [ { type: "text", text: JSON.stringify( { date: formattedDate, heartRate: data["activities-heart"] || [], intraday: data["activities-heart-intraday"] || {}, }, null, 2 ), }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } } );
  • Helper function used by getHeartRate (and other tools) to format or default the date to today in YYYY-MM-DD format.
    function formatDate(date?: string): string { if (date) return date; const today = new Date(); return today.toISOString().split("T")[0]; // YYYY-MM-DD }
  • Shared helper function used by getHeartRate to perform authenticated API requests to the Fitbit API.
    async function makeApiRequest(endpoint: string): Promise<any> { try { const url = `${baseUrl}${endpoint}`; const response = await fetch(url, { headers: { Authorization: `Bearer ${accessToken}`, Accept: "application/json", }, }); if (!response.ok) { throw new Error( `Fitbit API error: ${response.status} ${response.statusText}` ); } return await response.json(); } catch (error) { console.error(`Error making request to ${endpoint}:`, error); throw error; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/NitayRabi/fitbit-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server