Getting Recipe Attributes

Learn how to fetch recipe attributes like nutritional values, taste, equipment, ingredients, and price breakdown using the Spoonacular API.

The Spoonacular API offers widgets that provide key insight into recipe attributes like nutritional values, taste, equipment, ingredients, and price breakdown. In this lesson, we will fetch the JSON data of these widgets using the following endpoints:

  • recipe nutrition

  • recipe taste

  • recipe equipment

  • recipe ingredient

  • recipe price

Press + to interact

We can use one of the following recipe IDs for the code widgets in this lesson:

Recipe IDs

Recipe Name

ID

Classic Fruit Salad

639600

Tex-Mex Burger

663050

Baked Sirloin Steak

633790

Vegan Chana Masala Curry

1096306

Belgian Hot Chocolate

634818

The recipe nutrition endpoint

The recipe nutrition endpoint takes in the ID of a recipe as a path parameter and fetches the nutritional JSON data for that recipe. The recipe nutrition endpoint has a cost of 11 quota point. The endpoint method and URL are as follows:

GET https://api.spoonacular.com/recipes/{id}/nutritionWidget.json

Here is the request parameter we can use with the recipe nutrition endpoint:

Request Parameter

Parameter

Type

Category

Description

id

Number

Required

The identifier of the recipe for which we’re fetching the nutrition data.

Let’s see how to call the API endpoint. Click the “Run” button to execute the code. Please provide the value for RECIPE_ID.

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define the ID of any recipe here
const id = '{{RECIPE_ID}}';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/nutritionWidget.json`);
// Define API key here
const apiKey = '{{API_KEY}}';
// Define Header Parameters here
const headerParameters = {
'x-api-key': apiKey,
'content-type': 'application/json',
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
});
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function fetchRecipeAttributes(url, options) {
try {
// Using the fetch method to make an API call to Spoonacular
const response = await fetch(url, options);
// Custom function for printing the API response
printResponse(response);
} catch (error) {
// Custom function for printing the error message
printError(error);
}
}
// Attaching query parameters to the endpoint URL
endpointUrl.search = queryParameters;
// Calling function to make API call
fetchRecipeAttributes(endpointUrl, endpointOptions);

Take a look at the following explanation of the code above:

  • Line 5: We set and store the ID of a recipe.

  • Line 8: We use the provided endpoint URL to fetch the corresponding recipe attribute data.

  • Line 11: We assign the API key here to the apiKey variable.

  • Lines 14–17: We define the header parameters for the API call. We can modify or add parameters using the headerParameters variable.

    • Line 15: We pass the apiKey variable to the x-api-key header parameter.

  • Lines 20–21: We define the query parameters for the API call. We can add or modify parameters using the queryParameters variable.

The response of this endpoint is a recipe nutrition object. Here are some of the fields from the API response:

Response Fields

Name

Type

Description

calories

String

The number of calories in the recipe along with its units

carbs

String

The number of carbohydrates in the recipe along with its units

fat

String

The number of fats in the recipe along with its units

protein

String

The number of proteins in the recipe along with its units

bad

Object[ ]

A list of all nutrient objects that are bad in the recipe

good

Object[ ]

A list of all nutrient objects that are good in the recipe

nutrients

Object[ ]

A list of nutrient objects detailing the nutritional data of the recipe

properties

Object[ ]

A list of property objects detailing the food properties of the recipe

flavonoids

Object[ ]

A list of flavonoid objects detailing the flavonoid data of the recipe

ingredients

Object[ ]

A list of ingredient objects detailing the nutritional data of the ingredients in the recipe

caloricBreakdown

Object

An object that gives a percentage breakdown of calories in the recipe

weightPerServing

Object

The per-serving weight of the recipe

title

String

The name of the associated nutrient

amount

String

The amount of the associated nutrient in the recipe along with its units

percentOfDailyNeeds

Number

The percentage of the associated nutrients that satisfies the daily need of an average person

The recipe taste endpoint takes in the ID of a recipe as a path parameter and fetches that recipe’s taste data in JSON. The recipe taste endpoint has a cost of 11 quota point. The endpoint method and the URL are as follows:

GET https://api.spoonacular.com/recipes/{id}/tasteWidget.json

Here are the request parameters we can use with the recipe taste endpoint:

Request Parameters

Parameter

Type

Category

Description

id

Number

Required

The identifier of the recipe for which we’re fetching the taste data.

normalize

Boolean

Optional

The option, if set to true, normalizes the results according to the strongest taste.

Let’s see how to call the API endpoint. Simply copy the code below, paste it onto line 8 of the first code widget in this lesson, and test it there.

Press + to interact
const endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/tasteWidget.json`);

The response of this endpoint is a taste object. Here are some of the fields from the API response:

Response Fields

Name

Type

Description

sweetness

Number

The percentage of sweetness in a recipe.

Possible values: 0100

saltiness

Number

The percentage of saltiness in a recipe.

Possible values: 0100

sourness

Number

The percentage of sourness in a recipe.

Possible values: 0100

bitterness

Number

The percentage of bitterness in a recipe.

Possible values: 0100

savoriness

Number

The percentage of savoriness in a recipe.

Possible values: 0100

fattiness

Number

The percentage of fattiness in a recipe.

Possible values: 0100

spiciness

Number

The Scoville level of spiciness in a recipe.

Possible values: 0

The recipe equipment endpoint takes in the ID of a recipe as a path parameter and fetches the list of required equipment for that recipe. The recipe equipment endpoint has a cost of 11 quota point. The endpoint method and the URL are as follows:

GET https://api.spoonacular.com/recipes/{id}/equipmentWidget.json

Here is the request parameter we can use with the recipe equipment endpoint:

Request Parameter

Parameter

Type

Category

Description

id

Number

Required

The identifier of the recipe for which we’re fetching the equipment data.

Let’s see how to call the API endpoint. Simply copy the code below, paste it onto line 8 of the first code widget in this lesson, and test it there.

Press + to interact
const endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/equipmentWidget.json`);

The response of this endpoint is an array of equipment objects stored in the equipment field. Here are the fields from the API response:

Response Fields

Name

Type

Description

name

String

The name of the equipment

image

String

The image filename of the equipment

The recipe ingredient endpoint takes in the ID of a recipe as a path parameter and fetches the list of required ingredients for that recipe. The recipe ingredient endpoint has a cost of 11 quota point. The endpoint method and the URL are as follows:

GET https://api.spoonacular.com/recipes/{id}/ingredientWidget.json

Here is the request parameter we can use with the recipe ingredient endpoint:

Request Parameters

Parameter

Type

Category

Description

id

Number

Required

The identifier of the recipe for which we’re fetching the ingredient data.

Let’s see how to call the API endpoint. Simply copy the code below, paste it onto line 8 of the first code widget in this lesson, and test it there.

Press + to interact
const endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/ingredientWidget.json`);

The response of this endpoint is an array of ingredient objects stored in the ingredients field. Here are the fields from the API response:

Response Fields

Name

Type

Description

name

String

The name of the ingredient

image

String

The image filename of the ingredient

amount

Object

The amount object that details the amount of the ingredient

The recipe price endpoint takes in the ID of a recipe as a path parameter and fetches that recipe’s price breakdown in JSON. The recipe price endpoint has a cost of 11 quota point. The endpoint method and the URL are as follows:

GET https://api.spoonacular.com/recipes/{id}/priceBreakdownWidget.json

Here is the request parameter we can use with the recipe price endpoint:

Request Parameters

Parameter

Type

Category

Description

id

Number

Required

The identifier of the recipe for which we’re fetching the price data

Let’s see how to call the API endpoint. Simply copy the code below, paste it onto line 8 of the first code widget in this lesson, and test it there.

Press + to interact
const endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/priceBreakdownWidget.json`);

Here are the response fields for this endpoint:

Response Fields

Name

Type

Description

ingredients

Object[ ]

An array of ingredient objects, including their price

name

String

The name of the ingredient

image

String

The image filename of the ingredient

price

Number

The price of the ingredient

amount

Object

The amount object that details the amount of the ingredient

totalCost

Number

The total cost of the recipe

totalCostPerServing

Number

The total per-serving cost of the recipe