For the complete documentation index, see llms.txt. This page is also available as Markdown.

Foods

Use client.foods to autocomplete and search the food database, look up a barcode, fetch a full food, and suggest alternatives. These examples use the client from Authentication. Food details and portions explains how the steps fit together.

Autocomplete

let suggestions = try await client.foods.autocomplete(.init(query: "ban", limit: 8))

A suggestion is search text, not a food with servings. When the user picks one, search for its name:

guard let name = suggestions.items.first?.name else { return }
let results = try await client.foods.search(.init(query: name, limit: 10))

Autocomplete's category filter takes .generic or .branded.

Search by name

let results = try await client.foods.search(.init(query: "banana", category: .generic, limit: 10))

category is .generic, .branded, or .recipe; leave it out to search every category. Page with offset.

Fetch the full food and compute a portion

guard let match = results.items.first else { return }

let food = try await client.foods.get(id: match.id)
let portion = try food.portion() // One primary serving
print(portion.nutrition.calories?.value ?? 0)

To use another amount, pass quantity in the serving's unit, not a number of servings (Quantity and servings).

Look up a barcode

A barcode is 6–14 digits. Coverage is US-only: a code issued elsewhere (for example GS1 prefixes 73, 64, 54, or 93) comes back as .notFound, so fall back to text search.

Suggest alternatives

Leave out dietRestrictions and dietPreferences, or pass empty arrays, when none apply.

To analyze a meal description instead, see Food analysis. Input limits are in Validation limits.

Next: Restaurants.

Last updated

Was this helpful?