Compute Ingredient Amounts
Learn how to compute ingredient amounts based on nutritional goals using the Spoonacular API.
We'll cover the following
We can use the Spoonacular API to find the amount of an ingredient required to meet a nutritional goal and convert those amounts to more understandable units if needed. To do so, we can use the following endpoints based on our use case:
- compute ingredient amount
- convert ingredient amount
The compute ingredient amount endpoint
The compute ingredient amount endpoint computes the amount of a specific ingredient according to the provided amount of nutrition. The compute ingredient amount endpoint has a cost of
GET https://api.spoonacular.com/food/ingredients/{id}/amount
We can use one of the following ingredient IDs for the code widget in this section:
Ingredient IDs
Ingredient Name | ID |
Banana | 9040 |
Bread | 18064 |
Chicken Wings | 5100 |
Tomato | 11529 |
Chocolate Chips | 99278 |
Milk | 1077 |
Here are the request parameters we can use with the compute ingredient amount endpoint when computing the ingredient amount:
Request Parameters
Parameter | Type | Category | Description |
| Number | Required | The identifier of the ingredient for which we want the amount |
| String | Required | The target nutrient according to which we want to compute the ingredient amount |
| Number | Optional | The target amount of the nutrient |
| String | Optional | The unit to quantify the ingredient |
Let’s see how to call the API endpoint. Click the “Run” button to execute the code. Please provide the value for INGREDIENT_ID
.
// Importing libraries hereimport fetch from 'node-fetch';// Define the ID of any ingredient hereconst id = '{{INGREDIENT_ID}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.spoonacular.com/food/ingredients/${id}/amount`);// 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({nutrient: 'protein',target: 5,unit: 'oz',});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Function to make API callasync function computeIngredientAmount(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 callcomputeIngredientAmount(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 5: We set and store the ID of an ingredient for which we want to compute the amount.
Line 8: We use the endpoint URL
/food/ingredients/{id}/amount
to compute the ingredient amount.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–24: We define the query parameters for the API call. We can modify or add parameters using the
queryParameters
variable.Line 21: We compute the ingredient amount for proteins by setting the
nutrient
parameter toprotein
.Line 22: We compute the ingredient amount for five of the given nutrient by setting the
target
parameter to5
.Line 23: We compute the ingredient amount in ounces by setting the
unit
parameter tooz
.
Here are the fields from the API response:
Response Fields
Name | Type | Description |
| Number | The amount of the ingredient required to fulfill our nutritional need. |
| String | The ingredient’s amount unit. |
The convert ingredient amount endpoint
The convert ingredient amount endpoint converts the amount of an ingredient from one unit to another. The convert ingredient amount endpoint has a cost of
GET https://api.spoonacular.com/recipes/convert
Here are the request parameters we can use with the convert ingredient amount endpoint:
Request Parameters
Parameter | Type | Category | Description |
| String | Required | The name of the ingredient which we want to convert. |
| Number | Required | The source amount of the ingredient from which we want to convert. |
| String | Optional | The source unit of the ingredient from which we want to convert. |
| String | Required | The target unit of the ingredient to which we want to convert. |
Let’s see how to call the API endpoint. Click the “Run” button to execute the code.
// Define endpoint URL hereconst endpointUrl = new URL('https://api.spoonacular.com/recipes/convert');// Define Query Parameters hereconst queryParameters = new URLSearchParams({ingredientName: 'milk',sourceAmount: 20,sourceUnit: 'cups',targetUnit: 'oz',});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Attaching query parameters to the endpoint URLendpointUrl.search = queryParameters;// Calling function to make API callconvertIngredientAmount(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 2: We use the endpoint URL /recipes/convert
to convert the ingredient amount.
Lines 5–10: We define the query parameters for the API call. We can modify or add parameters using the
queryParameters
variable.Line 6: We define that we’re converting the amount for milk by setting the
ingredientName
parameter tomilk
.Lines 7–8: We define that we’re converting the amount for twenty cups of milk by setting the
sourceAmount
andsourceUnit
parameters to20
andcups
, respectively.Line 9: We define that we’re converting the milk amount to ounces by setting the
targetUnit
parameter tooz
.
Here are some of the fields from the API response:
Response Fields
Name | Type | Description |
| Number | The source amount of the ingredient |
| String | The source unit of the ingredient’s amount |
| Number | The target amount of the ingredient |
| String | The target unit of the ingredient’s amount |
| String | The text string detailing the conversion |