C# Wrapper for FatSecret.com Nutrition Data

A collection of service wrappers and examples for retrieving food nutrition data, and diet and exercise tracking, powered by FatSecret.com REST based API's.

For more information about the FatSecret.com platform, their REST API, or to register for an API Key, visit their links.

Examples

Food Search

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FatSecretSharp.Services;
using FatSecretSharp.Services.Requests;

namespace FatSecretSharp.Examples.ConsoleApp
{
    class Program
    {
        // Get your api key from http://platform.fatsecret.com/api/Default.aspx?screen=r.
        
        // TODO: Replace with your key / secret so you don't have to enter it every time.
        private static string consumerKey = string.Empty;
        private static string consumerSecret = string.Empty;

        private static FoodSearch foodSearch;
        private static FoodDetails foodDetail;
        private static ExerciseCatalog exerCatalog;

        static void Main(string[] args)
        {
            if (String.IsNullOrEmpty(consumerKey))
            {
                Console.WriteLine("Enter your API Key:");
                consumerKey = Console.ReadLine();
            }
            if (String.IsNullOrEmpty(consumerSecret))
            {
                Console.WriteLine("Enter your API Secret:");
                consumerSecret = Console.ReadLine();
            }

            Console.WriteLine(string.Empty);

            Console.Write("Food Search Term?");
            var searchTerm = Console.ReadLine();

            foodSearch = new FoodSearch(consumerKey, consumerSecret);

            var response = foodSearch
                            .GetResponseSynchronously(
                                new FoodSearchRequest()
                                {
                                    SearchExpression = searchTerm,
                                    MaxResults = 50,
                                    PageNumber = 0
                                });

            if (response != null && response.HasResults)
            {
                Console.WriteLine("Got " + response.foods.food.Count + " Results: \n\n");
                var form = "id: {0}, \n - type: {1}, \n - name: {2}, \n - description: {3}";
                foreach (var food in response.foods.food)
                {
                    Console.WriteLine(String.Format(form, food.food_id, food.food_type, food.food_name, food.food_description));
                }
            }
            else
                Console.WriteLine("No results from term: " + searchTerm);

            Console.WriteLine("");
            Console.WriteLine("Any key to quit...");
            Console.ReadKey();
        }        
    }
}

Food Details / Nutrition Info
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FatSecretSharp.Services;
using FatSecretSharp.Services.Requests;

namespace FatSecretSharp.Examples.ConsoleApp
{
    class Program
    {
        // Get you're api key from http://platform.fatsecret.com/api/Default.aspx?screen=r.
        
        // TODO: Replace with your key / secret so you don't have to enter it every time.
        private static string consumerKey = string.Empty;
        private static string consumerSecret = string.Empty;

        private static FoodSearch foodSearch;
        private static FoodDetails foodDetail;
        private static ExerciseCatalog exerCatalog;

        static void Main(string[] args)
        {
            if (String.IsNullOrEmpty(consumerKey))
            {
                Console.WriteLine("Enter your API Key:");
                consumerKey = Console.ReadLine();
            }
            if (String.IsNullOrEmpty(consumerSecret))
            {
                Console.WriteLine("Enter your API Secret:");
                consumerSecret = Console.ReadLine();
            }

            Console.WriteLine(string.Empty);

            Console.Write("Enter a food id: ");
            var foodId = Console.ReadLine();

            long parsedId = 0;
            while (!long.TryParse(foodId, out parsedId))
            {
                Console.WriteLine("Invalid food id, try again:");
                foodId = Console.ReadLine();
            }
            
            foodDetail = new FoodDetails(consumerKey, consumerSecret);

            var response = foodDetail
                            .GetResponseSynchronously(
                                new FoodDetailsRequest()
                                {
                                    FoodId = parsedId
                                });

            if (response != null && response.HasResponse)
            {
                Console.WriteLine("Found " + response.food.servings.serving.Count + " Results: ");

                foreach (var serv in response.food.servings.serving)
                {
                    // Cheat and use a little reflection to help show the nutrition information.
                    var props = serv.GetType().GetProperties();
                    foreach (var prop in props)
                    {
                        if (prop == null || !prop.CanRead)
                            continue;

                        Console.WriteLine("{0} : {1}", prop.Name, prop.GetValue(serv, null));
                    }
                }

            }
            else
                Console.WriteLine("No response for food id: " + foodId);

            Console.WriteLine("");
            Console.WriteLine("Any key to quit...");
            Console.ReadKey();
        }        
    }
}

Last edited Jul 18, 2010 at 1:48 PM by jgable, version 6