Skip to content

jeffhigham-f3/fatsecret_nutrition

Β 
Β 

Repository files navigation

Fatsecret Nutrition

Developed by F3 Software πŸš€

A Dart package for interacting with the FatSecret Nutrition API by F3 Software

License: MIT Coverage

Installation πŸ“¦

dependencies:
  dotenv: ^4.2.0
  fatsecret_nutrition: ^0.1.0

Usage πŸ“š

Obtain Client ID and Client Secret

  1. Go to the FatSecret Platform Website
  2. Sign up or log in
  3. Create a new application
  4. Copy the Client ID and Client Secret
  5. Copy the .env.example file to .env in the project root.
  6. Add the Client ID and Client Secret to the .env file.
CLIENT_ID=your_client_id
CLIENT_SECRET=your_client_secret

Set Up Environment Variables

export 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.api

Initialize The Client

final fatSecret = FatSecretNutrition(
    clientId: env['CLIENT_ID']!,
    clientSecret: env['CLIENT_SECRET']!,
    tokenUrl: env['TOKEN_URL']!,
    apiUrl: env['API_URL']!,
);

Search for Foods

final foods = await fatSecret.search(
  FoodSearchV3Props(
    searchExpression: 'apple',
    maxResults: 10,
    pageNumber: 0,
  ),
);

Auto-complete Food Search

final suggestions = await fatSecret.autoComplete(
  const FoodAutoCompleteV2Props(
    expression: 'apple',
  ),
);

Find Food by Barcode

final food = await fatSecret.foodFindIdForBarcode(
  const FoodForBarcodeProps(
    barcode: '9300675079655',
  ),
);

Search for Brands

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');
  }
}

Get Food Categories

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}');
  }
}

Get Food Sub-Categories

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');
  }
}

Get Recipe by ID

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}');
  }
}

Search Recipes

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');
  }
}

Process Natural Language Input

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');
  }
}

Image Recognition

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('---');
  }
}

Features

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 ⏳

License πŸ“

This project is licensed under the MIT License.

About

A Dart package for interacting with the FatSecret Nutrition API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 100.0%