Skip to content

Comments

feat: Add FDC nutrition information per ingredient#6747

Open
EinToni wants to merge 3 commits intomealie-recipes:mealie-nextfrom
EinToni:POC_NutritionCalculation
Open

feat: Add FDC nutrition information per ingredient#6747
EinToni wants to merge 3 commits intomealie-recipes:mealie-nextfrom
EinToni:POC_NutritionCalculation

Conversation

@EinToni
Copy link

@EinToni EinToni commented Dec 19, 2025

What this PR does / why we need it:

This PR comes from the discussion 2748 and tries to implement a first step into the direction of adding nutritional data per ingredient. This would allow users to auto calculate nutritional data of recipes accurately form the ingretients.

I tried to keep the changes very simple for now as to not overload this pull request. The nutritional data is saved but unused. To be usable it would need a connection between the currently existing units and some internal "base units" like "gram", "kilogram" etc. If going that way the user could then select a base unit for each of the individualy created units. All weight units are then convertable and usable. For volume units like "cup" conversion per ingredient would need to be added or could even be pulled from FDC. But this is probably all for a different issue (i would be happy to work on).

Changes in this PR:

  • Added an FDC_API_KEY to the settings for access to the FDC API.
  • Added a backend API /recipe/nutrition/fetch_fda/{fda_id} that uses the FDC_API_KEY to call the API and return the data as nutrition_id/value pairs.
  • Changed the database model of the ingredients to include a list of nutrition data which consists of the FDC nutrition id and the respecting value.
  • Adapted foods.vue to allow for input of an FDC ID when creating an ingredient and when editing an ingredient. When the ID got edited the nutrition data is fetched and stored from the FDC API upon confirm of the dialog.
  • Added a button next to the FDC ID entry. If the ID field is empty, it opens the FDC search page. If the field contains an ID, it opens the details page of the FDC with that ID.
Overview table image image
Create/edit food widthout FDC ID image image
Create/edit food with existing FDC ID image image

Which issue(s) this PR fixes:

No issue exists yet, the PR arrises from discussion 2748.

Special notes for your reviewer:

  • All texts are currently hard coded and should instead be done via i18n before merging.
  • I think it would be better to completely handle the update of the nutrition data in the backend instead of passing the data to the frontend only to store it from there in the db (something like /recipe/ingredient/update_nutrition/{ingredient_id}). But I was not sure how to implement that, so I chose the current solution instead.
  • I am storing all nutrition information per ingredient. This is probably a bit too much as most data will never be used (like for example "SFA 14:0" id 1264, I have no clue what that is), but as it is just an int for the id and a float for the value it shouldn't matter too much.

Testing

  • Created a free FDC API key and added in the .dev.env file.
  • Added a valid FDC ID to an ingredient.
  • Displayed the stored data in a log I removed again as this was just for testing.

@michael-genson
Copy link
Collaborator

I think this is definitely a step in the right direction for better nutritional data support. I wonder if we should hold off on this until we have better unit handling with a base unit/conversions, etc. This is something in the backlog to support other areas of the app too.

Do you think it would make sense to wait for that first? I worry adding this before we have a better handle on units/conversions may cause a lot of headache. I haven't looked too closely at your implementation yet but I read your PR description mentioning this.

@SvenErik1968
Copy link

In addition to what @michael-genson suggested, would it be possible to make this feature more food composition database agnostic? As a user in Norway, I would be more interested in getting these values from the database that the Norwegian Food Safety Authority publishes. An alternative would also be the European Food Safety Authority's forthcoming open-access European Food Composition Database that is expected to be published in mid-2026.

@EinToni
Copy link
Author

EinToni commented Feb 15, 2026

It's probably a wise idea to first do proper unit handling and then get back to this, I simply wanted to start somewhere. Do you know if there is currently and work done regarding this @michael-genson? I will put this on hold until there is some better unit handling.

Regarding your more general idea of implementing this with different food database backends, I guess it is definitely possible, but will take way more architectural work to pull of cleanly, because every database has different identifiers for the inredients.

@michael-genson
Copy link
Collaborator

Do you know if there is currently and work done regarding this

It's in my (soft) backlog but I'm not currently working on it.

Regarding different db backends I'll have to dig into this PR, but I'm fine with picking one standard for now (like you did here). As long as we make it reasonably extensible it should be pretty easy to add on support later.

@michael-genson
Copy link
Collaborator

michael-genson commented Feb 22, 2026

@EinToni following up on this, I implemented some unit standardization here: #7121

quantity + standard unit + FDC food should hopefully be enough to generate reasonably accurate nutrition info. Let me know if the unit standardization using pint is sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants