Food details and portions
Logging a food or predicting glucose takes a FoodSelection: a food ID, a serving ID, and a quantity. Build one in three steps:
Find the food with
foods.search,foods.autocomplete, orfoods.lookupBarcode.Fetch the full food with
foods.get. Search and autocomplete results can carry less detail.Pick one of its servings and a quantity greater than zero.
import type { FoodSelection, JanuaryClient } from '@januaryai/react-native';
export async function selectFood(
january: JanuaryClient,
query: string,
quantity = 1
): Promise<FoodSelection | undefined> {
const { items } = await january.foods.search({ query });
const match = items[0]; // in your UI, the result the user picked
if (!match) return undefined;
const food = await january.foods.get({ foodId: match.id });
const serving = food.servings.find((s) => s.isPrimary) ?? food.servings[0];
if (!serving?.id) return undefined;
return { id: food.id, serving: { id: serving.id, quantity } };
}quantity is how many of that serving were eaten: for a "6 oz" serving, 1 is 6 oz and 0.5 is 3 oz. Take serving IDs from food.servings as returned; don't build them from labels.
A food analysis detection already carries a serving and a quantity, so it can be logged without these steps.
Last updated
Was this helpful?