Skip to content

The FoodData Central API provides REST access to FoodData Central (FDC). It is intended primarily to assist application developers wishing to incorporate nutrient data into their applications or websites.

Notifications You must be signed in to change notification settings

kjr247/FoodDataCentralSDK

Repository files navigation

IO.FoodDataCentralSDK - the C# library for the Food Data Central API

The FoodData Central API provides REST access to FoodData Central (FDC). It is intended primarily to assist application developers wishing to incorporate nutrient data into their applications or websites. To take full advantage of the API, developers should familiarize themselves with the database by reading the database documentation available via links on Data Type Documentation. This documentation provides the detailed definitions and descriptions needed to understand the data elements referenced in the API documentation. Additional details about the API including rate limits, access, and licensing are available on the FDC website

This C# SDK is automatically generated by the Swagger Codegen project:

Frameworks supported

  • .NET 4.0 or later
  • Windows Phone 7.1 (Mango)

Dependencies

The DLLs included in the package may not be the latest version. We recommend using NuGet to obtain the latest version of the packages:

Install-Package RestSharp
Install-Package Newtonsoft.Json
Install-Package JsonSubTypes

NOTE: RestSharp versions greater than 105.1.0 have a bug which causes file uploads to fail. See RestSharp#742

Installation

Run the following command to generate the DLL

  • [Mac/Linux] /bin/sh build.sh
  • [Windows] build.bat

Then include the DLL (under the bin folder) in the C# project, and use the namespaces:

using IO.FoodDataCentralSDK.Api;
using IO.FoodDataCentralSDK.Client;
using IO.FoodDataCentralSDK.Model;

Packaging

A .nuspec is included with the project. You can follow the Nuget quickstart to create and publish packages.

This .nuspec uses placeholders from the .csproj, so build the .csproj directly:

nuget pack -Build -OutputDirectory out IO.FoodDataCentralSDK.csproj

Then, publish to a local feed or other host and consume the new package via Nuget as usual.

Getting Started

using System;
using System.Diagnostics;
using IO.FoodDataCentralSDK.Api;
using IO.FoodDataCentralSDK.Client;
using IO.FoodDataCentralSDK.Model;

namespace Example
{
    public class Example
    {
        public void main()
        {
            // Configure API key authorization: ApiKeyAuth
            Configuration.Default.ApiKey.Add("api_key", "YOUR_API_KEY");
            // Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
            // Configuration.Default.ApiKeyPrefix.Add("api_key", "Bearer");

            var apiInstance = new FDCApi();
            var fdcId = fdcId_example;  // string | FDC id of the food to retrieve
            var format = format_example;  // string | Optional. 'abridged' for an abridged set of elements, 'full' for all elements (default). (optional) 
            var nutrients = new List<int?>(); // List<int?> | Optional. List of up to 25 nutrient numbers. Only the nutrient information for the specified nutrients will be returned. Should be comma separated list (e.g. nutrients=203,204) or repeating parameters (e.g. nutrients=203&nutrients=204). If a food does not have any matching nutrients, the food will be returned with an empty foodNutrients element. (optional) 

            try
            {
                // Fetches details for one food item by FDC ID
                InlineResponse200 result = apiInstance.GetFood(fdcId, format, nutrients);
                Debug.WriteLine(result);
            }
            catch (Exception e)
            {
                Debug.Print("Exception when calling FDCApi.GetFood: " + e.Message );
            }
        }
    }
}

Documentation for API Endpoints

All URIs are relative to https://api.nal.usda.gov/fdc

Class Method HTTP request Description
FDCApi GetFood GET /v1/food/{fdcId} Fetches details for one food item by FDC ID
FDCApi GetFoods GET /v1/foods Fetches details for multiple food items using input FDC IDs
FDCApi GetFoodsList GET /v1/foods/list Returns a paged list of foods, in the 'abridged' format
FDCApi GetFoodsSearch GET /v1/foods/search Returns a list of foods that matched search (query) keywords
FDCApi GetJsonSpec GET /v1/json-spec Returns this documentation in JSON format
FDCApi GetYamlSpec GET /v1/yaml-spec Returns this documentation in JSON format
FDCApi PostFoods POST /v1/foods Fetches details for multiple food items using input FDC IDs
FDCApi PostFoodsList POST /v1/foods/list Returns a paged list of foods, in the 'abridged' format
FDCApi PostFoodsSearch POST /v1/foods/search Returns a list of foods that matched search (query) keywords

Documentation for Models

Documentation for Authorization

ApiKeyAuth

  • Type: API key
  • API key parameter name: api_key
  • Location: URL query string

publish to nuget registry

click on pack.bat

cd .\src\IO.FoodDataCentralSDK
..\..\nuget pack -Build -OutputDirectory out FoodDataCentralSDK.csproj

add your api key in INSERT_Nuget_API_Key_Here and create and run publish.bat note that this file is not checked in and is ignored. be sure to change the version by clicking on the project and opening properties and changing the assembly info. then update the version below as x.x.x.nupkg

..\..\nuget push out\FoodDataCentralSDK.1.0.2.nupkg <INSERT_Nuget_API_Key_Here> -Source https://api.nuget.org/v3/index.json

About

The FoodData Central API provides REST access to FoodData Central (FDC). It is intended primarily to assist application developers wishing to incorporate nutrient data into their applications or websites.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages