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
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
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 |
| 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
.
// Importing libraries hereimport fetch from 'node-fetch';// Define the ID of any recipe hereconst id = '{{RECIPE_ID}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/nutritionWidget.json`);// Define API key hereconst apiKey = '{{API_KEY}}';// Define Header Parameters hereconst headerParameters = {'x-api-key': apiKey,'content-type': 'application/json',};// Define Query Parameters hereconst queryParameters = new URLSearchParams({});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Function to make API callasync function fetchRecipeAttributes(url, options) {try {// Using the fetch method to make an API call to Spoonacularconst response = await fetch(url, options);// Custom function for printing the API responseprintResponse(response);} catch (error) {// Custom function for printing the error messageprintError(error);}}// Attaching query parameters to the endpoint URLendpointUrl.search = queryParameters;// Calling function to make API callfetchRecipeAttributes(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 thex-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 |
| String | The number of calories in the recipe along with its units |
| String | The number of carbohydrates in the recipe along with its units |
| String | The number of fats in the recipe along with its units |
| String | The number of proteins in the recipe along with its units |
| Object[ ] | A list of all nutrient objects that are bad in the recipe |
| Object[ ] | A list of all nutrient objects that are good in the recipe |
| Object[ ] | A list of nutrient objects detailing the nutritional data of the recipe |
| Object[ ] | A list of property objects detailing the food properties of the recipe |
| Object[ ] | A list of flavonoid objects detailing the flavonoid data of the recipe |
| Object[ ] | A list of ingredient objects detailing the nutritional data of the ingredients in the recipe |
| Object | An object that gives a percentage breakdown of calories in the recipe |
| Object | The per-serving weight of the recipe |
| String | The name of the associated nutrient |
| String | The amount of the associated nutrient in the recipe along with its units |
| 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 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 |
| Number | Required | The identifier of the recipe for which we’re fetching the taste data. |
| Boolean | Optional | The option, if set to |
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.
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 |
| Number | The percentage of sweetness in a recipe. Possible values: |
| Number | The percentage of saltiness in a recipe. Possible values: |
| Number | The percentage of sourness in a recipe. Possible values: |
| Number | The percentage of bitterness in a recipe. Possible values: |
| Number | The percentage of savoriness in a recipe. Possible values: |
| Number | The percentage of fattiness in a recipe. Possible values: |
| Number | The Scoville level of spiciness in a recipe. Possible values: |
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 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 |
| 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.
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 |
| String | The name of the equipment |
| 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 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 |
| 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.
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 |
| String | The name of the ingredient |
| String | The image filename of the ingredient |
| 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 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 |
| 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.
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 |
| Object[ ] | An array of ingredient objects, including their price |
| String | The name of the ingredient |
| String | The image filename of the ingredient |
| Number | The price of the ingredient |
| Object | The amount object that details the amount of the ingredient |
| Number | The total cost of the recipe |
| Number | The total per-serving cost of the recipe |