Developed by F3 Software π
A Dart package for interacting with the FatSecret Nutrition API by F3 Software
dependencies:
dotenv: ^4.2.0
fatsecret_nutrition: ^0.1.0- Go to the FatSecret Platform Website
- Sign up or log in
- Create a new application
- Copy the Client ID and Client Secret
- Copy the
.env.examplefile to.envin the project root. - Add the Client ID and Client Secret to the
.envfile.
CLIENT_ID=your_client_id
CLIENT_SECRET=your_client_secretexport CLIENT_ID=your_client_id
export CLIENT_SECRET=your_client_secret
export TOKEN_URL=https://oauth.fatsecret.com/connect/token
export API_URL=https://platform.fatsecret.com/rest/server.apifinal fatSecret = FatSecretNutrition(
clientId: env['CLIENT_ID']!,
clientSecret: env['CLIENT_SECRET']!,
tokenUrl: env['TOKEN_URL']!,
apiUrl: env['API_URL']!,
);final foods = await fatSecret.search(
FoodSearchV3Props(
searchExpression: 'apple',
maxResults: 10,
pageNumber: 0,
),
);final suggestions = await fatSecret.autoComplete(
const FoodAutoCompleteV2Props(
expression: 'apple',
),
);final food = await fatSecret.foodFindIdForBarcode(
const FoodForBarcodeProps(
barcode: '9300675079655',
),
);final brands = await fatSecret.searchBrands(
const FoodBrandsGetAllV2Props(
startsWith: 'k',
brandType: 'restaurant',
language: 'en',
region: 'US',
),
);
if (brands != null) {
for (final brand in brands.foodBrands.foodBrand) {
print('Brand: $brand');
}
}final categories = await fatSecret.getFoodCategories(
props: const FoodCategoriesProps(
region: 'US',
language: 'en',
),
);
if (categories != null) {
for (final category in categories.foodCategories.foodCategory) {
print('Category: ${category.foodCategoryName}');
print('ID: ${category.foodCategoryId}');
print('Description: ${category.foodCategoryDescription}');
}
}final subCategories = await fatSecret.getFoodSubCategories(
props: const FoodSubCategoriesProps(
foodCategoryId: 3, // Breads & Cereals
region: 'US',
language: 'en',
),
);
if (subCategories != null) {
for (final subCategory in subCategories.foodSubCategories.foodSubCategory) {
print('Sub-category: $subCategory');
}
}final recipe = await fatSecret.getRecipeById(
const RecipeGetByIdProps(
recipeId: '91', // Baked Lemon Snapper recipe
),
);
if (recipe != null) {
print('Recipe: ${recipe.recipe.recipeName}');
print('Description: ${recipe.recipe.recipeDescription}');
print('Servings: ${recipe.recipe.numberOfServings}');
print('Grams per portion: ${recipe.recipe.gramsPerPortion}');
// Print ingredients
for (final ingredient in recipe.recipe.ingredients.ingredient) {
print('- ${ingredient.ingredientDescription}');
}
// Print directions
for (final direction in recipe.recipe.directions.direction) {
print('${direction.directionNumber}. ${direction.directionDescription}');
}
}final recipes = await fatSecret.searchRecipes(
const RecipeSearchProps(
searchExpression: 'chocolate',
maxResults: 5,
pageNumber: 0,
mustHaveImages: true,
),
);
if (recipes != null) {
for (final recipe in recipes.recipes.recipe) {
print('Recipe: ${recipe.recipeName}');
print('Description: ${recipe.recipeDescription}');
print('Image: ${recipe.recipeImage}');
// Print nutrition
print('Calories: ${recipe.recipeNutrition.calories}');
print('Protein: ${recipe.recipeNutrition.protein}g');
print('Carbs: ${recipe.recipeNutrition.carbohydrate}g');
print('Fat: ${recipe.recipeNutrition.fat}g');
}
}final nlpResponse = await fatSecret.processNaturalLanguage(
'A toast with ham and cheese',
includeFoodData: true,
region: 'US',
language: 'en',
);
if (nlpResponse != null) {
for (final food in nlpResponse.foodResponse) {
print('Food: ${food.foodEntryName}');
print('Food ID: ${food.foodId}');
// Print eaten food details
print('Eaten:');
print('- Singular: ${food.eaten.foodNameSingular}');
print('- Plural: ${food.eaten.foodNamePlural}');
print('- Units: ${food.eaten.units}');
print('- Metric Description: ${food.eaten.metricDescription}');
// Print nutritional content
final nutrition = food.eaten.totalNutritionalContent;
print('Calories: ${nutrition.calories}');
print('Protein: ${nutrition.protein}g');
print('Carbs: ${nutrition.carbohydrate}g');
print('Fat: ${nutrition.fat}g');
// Print suggested serving
final serving = food.suggestedServing;
print('Serving: ${serving.servingDescription}');
print('Metric Amount: ${serving.metricMeasureAmount}g');
}
}final imageB64 = '...'; // Your base64-encoded image string
final imageResponse = await fatSecret.imageRecognitionV2(
imageB64: imageB64,
region: 'US',
language: 'en',
includeFoodData: true,
);
if (imageResponse != null) {
for (final food in imageResponse.foodResponse) {
print('Food: οΏ½[1m${food.foodEntryName}οΏ½[0m');
print('Food ID: ${food.foodId}');
print('Eaten:');
print('- Singular: ${food.eaten.foodNameSingular}');
print('- Plural: ${food.eaten.foodNamePlural}');
print('- Units: ${food.eaten.units}');
print('- Metric Description: ${food.eaten.metricDescription}');
print('- Total Metric Amount: ${food.eaten.totalMetricAmount}g');
print('- Per Unit Metric Amount: ${food.eaten.perUnitMetricAmount}g');
print('---');
}
}| Feature | Status |
|---|---|
| Foods: Autocomplete Search | β |
| Foods: Find Id For Barcode | β |
| Foods: Search | β |
| Foods: Get By Id | β |
| Food Brands: Get All | β |
| Food Categories: Get All | β |
| Food Sub Categories: Get All | β |
| Recipes: Get By ID | β |
| Recipes: Search | β |
| Recipe Types | β |
| Natural Language Processing | β |
| Image Recognition | β |
| Profile: Foods | β³ |
| Profile: Recipes | β³ |
| Profile: Saved Meals | β³ |
| Profile: Authentication | β³ |
| Profile: Food Diary | β³ |
| Profile: Exercise Diary | β³ |
| Profile: Weight Diary | β³ |
This project is licensed under the MIT License.