Food logs
Use client.foodLogs to create, list, summarize, get, update, and delete a user's meals. These examples use the client from Authentication.
Lists and summaries take inclusive yyyy-MM-dd dates in the client's timezone (days and dates). A list covers at most 60 days and a summary at most 366; a longer range fails with the code date_range_too_large.
Choose a food and serving
Fetch the full food, then build a portion from one of its servings:
let food = try await client.foods.get(id: selectedFoodID)
let portion = try food.portion(servingID: selectedServingID) // One serving
let selectedFood = portion.selectionportion.selection is the FoodSelection that food logs and glucose predictions take. Leave quantity out for one serving; any other amount is in the serving's unit (Quantity and servings).
Log an analyzed meal
A food analysis detection has its food id, selected serving, and the quantity eaten, so it logs without another lookup. They're optionals in Swift; unwrap them:
let selections = scan.detections.compactMap { detection -> FoodSelection? in
guard let id = detection.food.id,
let servingID = detection.food.serving.id,
let quantity = detection.food.quantity else { return nil }
return FoodSelection(id: id, serving: ServingSelection(id: servingID, quantity: quantity))
}Create
timestampUTC is when the meal was eaten, as ISO 8601 with any offset; leave it out to mean now. Creates aren't idempotent: after a timed-out create, list the day before you retry (retries).
List
Summarize a range
Ask for a summary instead of adding up logs yourself when a screen shows daily or weekly totals:
Get one log
FoodLog.id is optional in Swift, because a listed log can rarely have none. Unwrap it before you get, update, or delete the log:
Update
Only the fields you pass change. An update that passes none fails with .validation before it's sent.
Delete
A successful delete returns nothing.
Next: Water and weight logs.
Last updated
Was this helpful?