USDA Nutrients

USDA Nutrients

Health

National Nutrient Database for Standard Reference. 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. The API provides two endpoints: the Food Search endpoint, which returns foods that match desired search criteria, and the Food Details endpoint, which returns details on a particular food.

Visit API

📚 Documentation & Examples

Everything you need to integrate with USDA Nutrients

🚀 Quick Start Examples

USDA Nutrients Javascript Examplejavascript
// USDA Nutrients API Example
const response = await fetch('https://fdc.nal.usda.gov/api-guide.html', {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json'
    }
});

const data = await response.json();
console.log(data);

Using the USDA Food Data Central API with JavaScript

The USDA Food Data Central API provides access to a variety of food and nutrient data, including information on over 300,000 foods. In this post, we'll explore how to use this API with JavaScript.

Getting an API Key

Before we can start using the API, we'll need to get an API key. To do this, we'll need to create an account on the USDA Food Data Central website and request an API key. Once we have our API key, we can start using the API.

Making Requests

The API documentation provides information on the available endpoints and parameters. To make a request, we'll need to include our API key in the request URL, along with any other parameters we want to use.

const apiKey = 'YOUR_API_KEY';
const query = 'chicken';

fetch(`https://api.nal.usda.gov/fdc/v1/foods/search?api_key=${apiKey}&query=${query}`)
  .then(response => response.json())
  .then(data => console.log(data));

In this example, we're using the fetch function to make a request to the /foods/search endpoint. We're passing our API key and a query string parameter for the search term 'chicken'. Once we receive the response, we're parsing the JSON data and logging it to the console.

Filtering Results

The API also allows us to filter the results using various parameters, such as food category or nutrient information. Here's an example of how we can filter by food category:

const apiKey = 'YOUR_API_KEY';
const categoryId = 4; // Category ID for 'Dairy and Egg Products'

fetch(`https://api.nal.usda.gov/fdc/v1/foods/search?api_key=${apiKey}&category=${categoryId}`)
  .then(response => response.json())
  .then(data => console.log(data));

In this example, we're using the category parameter to filter the search results to only include foods from the 'Dairy and Egg Products' category.

Retrieving Nutrient Information

One of the main benefits of the USDA Food Data Central API is the ability to retrieve nutrient information for individual foods. We can do this by using the fdcId parameter to specify the ID of the food we're interested in, and including the nutrients parameter to specify which nutrient information we want to retrieve.

const apiKey = 'YOUR_API_KEY';
const fdcId = 167778; // ID for 'Chicken Breast'

fetch(`https://api.nal.usda.gov/fdc/v1/foods/${fdcId}/nutrients?api_key=${apiKey}&nutrients=203`)
  .then(response => response.json())
  .then(data => console.log(data));

In this example, we're using the fdcId parameter to retrieve nutrient information for chicken breast. We've also included the nutrients parameter to specify that we only want to retrieve information about protein (nutrient ID 203).

Conclusion

The USDA Food Data Central API provides a wealth of food and nutrient information that can be incredibly useful for a variety of applications. By using JavaScript, we can interact with the API and filter, search, and retrieve the information we need. Get creative and see what kind of food-related applications you can build!

📊 30-Day Uptime History

Daily uptime tracking showing online vs offline minutes

Jun 12Jun 14Jun 16Jun 18Jun 20Jun 22Jun 24Jun 26Jun 28Jun 30Jul 2Jul 4Jul 6Jul 8Jul 1104008001440Minutes
Online
Offline

Related APIs in Health