get_nutrition_by_date_range
Retrieve nutrition data like calories, water, protein, carbs, fat, fiber, or sodium from Fitbit for a specific date range to analyze dietary patterns and track nutritional intake over time.
Instructions
Get the raw JSON response for nutrition data from Fitbit for a specific resource and date range. Requires 'resource' parameter (caloriesIn, water), 'startDate' and 'endDate' parameters in 'YYYY-MM-DD' format. Note: The API enforces a maximum range of 1,095 days.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| resource | Yes | The nutrition resource to retrieve data for. | |
| startDate | Yes | The start date for which to retrieve nutrition data (YYYY-MM-DD). | |
| endDate | Yes | The end date for which to retrieve nutrition data (YYYY-MM-DD). |
Implementation Reference
- src/nutrition.ts:284-332 (handler)The core handler function for the 'get_nutrition_by_date_range' tool. It constructs the Fitbit API endpoint for the specified nutrition resource and date range, fetches the data using makeFitbitRequest, handles errors and empty results, and returns the raw JSON response.async ({ resource, startDate, endDate, }: NutritionRangeParams): Promise<ToolResponseStructure> => { // Construct the endpoint dynamically const endpoint = `foods/log/${resource}/date/${startDate}/${endDate}.json`; // Make the request const nutritionData = await makeFitbitRequest<NutritionTimeSeriesResponse>( endpoint, getAccessTokenFn, FITBIT_API_BASE ); // Handle API call failure if (!nutritionData) { return { content: [ { type: 'text', text: `Failed to retrieve nutrition data from Fitbit API for resource '${resource}' and the date range '${startDate}' to '${endDate}'. Check token, permissions, date format, and ensure the range is 1,095 days or less.`, }, ], isError: true, }; } // Handle no data found const resourceKey = `foods-log-${resource}`; const nutritionEntries = nutritionData[resourceKey] || []; if (nutritionEntries.length === 0) { return { content: [ { type: 'text', text: `No nutrition data found for resource '${resource}' and the date range '${startDate}' to '${endDate}'.`, }, ], }; } // Return successful response const rawJsonResponse = JSON.stringify(nutritionData, null, 2); return { content: [{ type: 'text', text: rawJsonResponse }], }; }
- src/nutrition.ts:241-265 (schema)Zod input schema for the tool parameters: resource (enum of nutrition types), startDate and endDate (YYYY-MM-DD format strings).const rangeParametersSchemaShape = { resource: z .enum([ 'caloriesIn', 'water', 'protein', 'carbs', 'fat', 'fiber', 'sodium', ]) .describe('The nutrition resource to retrieve data for.'), startDate: z .string() .regex(/^\d{4}-\d{2}-\d{2}$/, 'Start date must be in YYYY-MM-DD format.') .describe( 'The start date for which to retrieve nutrition data (YYYY-MM-DD).' ), endDate: z .string() .regex(/^\d{4}-\d{2}-\d{2}$/, 'End date must be in YYYY-MM-DD format.') .describe( 'The end date for which to retrieve nutrition data (YYYY-MM-DD).' ), };
- src/nutrition.ts:280-333 (registration)The server.tool registration call for 'get_nutrition_by_date_range', including name, description, schema reference, and inline handler function. This occurs within the registerNutritionTools function.server.tool( rangeToolName, rangeDescription, rangeParametersSchemaShape, async ({ resource, startDate, endDate, }: NutritionRangeParams): Promise<ToolResponseStructure> => { // Construct the endpoint dynamically const endpoint = `foods/log/${resource}/date/${startDate}/${endDate}.json`; // Make the request const nutritionData = await makeFitbitRequest<NutritionTimeSeriesResponse>( endpoint, getAccessTokenFn, FITBIT_API_BASE ); // Handle API call failure if (!nutritionData) { return { content: [ { type: 'text', text: `Failed to retrieve nutrition data from Fitbit API for resource '${resource}' and the date range '${startDate}' to '${endDate}'. Check token, permissions, date format, and ensure the range is 1,095 days or less.`, }, ], isError: true, }; } // Handle no data found const resourceKey = `foods-log-${resource}`; const nutritionEntries = nutritionData[resourceKey] || []; if (nutritionEntries.length === 0) { return { content: [ { type: 'text', text: `No nutrition data found for resource '${resource}' and the date range '${startDate}' to '${endDate}'.`, }, ], }; } // Return successful response const rawJsonResponse = JSON.stringify(nutritionData, null, 2); return { content: [{ type: 'text', text: rawJsonResponse }], }; } );
- src/index.ts:82-82 (registration)Invocation of registerNutritionTools in the main index.ts, which registers the nutrition tools including get_nutrition_by_date_range.registerNutritionTools(server, getAccessToken);
- src/nutrition.ts:267-278 (schema)TypeScript type definition for the NutritionRangeParams used in the tool handler and schema.type NutritionRangeParams = { resource: | 'caloriesIn' | 'water' | 'protein' | 'carbs' | 'fat' | 'fiber' | 'sodium'; startDate: string; endDate: string; };