Compute Ingredient Amounts

Learn how to compute ingredient amounts based on nutritional goals using the Spoonacular API.

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 11quota point. This endpoint utilizes the GET method and uses the following URL:

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

id

Number

Required

The identifier of the ingredient for which we want the amount

nutrient

String

Required

The target nutrient according to which we want to compute the ingredient amount

target

Number

Optional

The target amount of the nutrient

unit

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.

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define the ID of any ingredient here
const id = '{{INGREDIENT_ID}}';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.spoonacular.com/food/ingredients/${id}/amount`);
// 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({
nutrient: 'protein',
target: 5,
unit: 'oz',
});
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function computeIngredientAmount(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
computeIngredientAmount(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 the x-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 to protein.

    • Line 22: We compute the ingredient amount for five of the given nutrient by setting the target parameter to 5.

    • Line 23: We compute the ingredient amount in ounces by setting the unit parameter to oz.

Here are the fields from the API response:

Response Fields

Name

Type

Description

amount

Number

The amount of the ingredient required to fulfill our nutritional need.

unit

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 11quota point. The endpoint method and the URL are as follows:

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

ingredientName

String

Required

The name of the ingredient which we want to convert.

sourceAmount

Number

Required

The source amount of the ingredient from which we want to convert.

sourceUnit

String

Optional

The source unit of the ingredient from which we want to convert.

targetUnit

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.

Press + to interact
// Define endpoint URL here
const endpointUrl = new URL('https://api.spoonacular.com/recipes/convert');
// Define Query Parameters here
const queryParameters = new URLSearchParams({
ingredientName: 'milk',
sourceAmount: 20,
sourceUnit: 'cups',
targetUnit: 'oz',
});
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Attaching query parameters to the endpoint URL
endpointUrl.search = queryParameters;
// Calling function to make API call
convertIngredientAmount(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 to milk.

    • Lines 7–8: We define that we’re converting the amount for twenty cups of milk by setting the sourceAmount and sourceUnit parameters to 20 and cups, respectively.

    • Line 9: We define that we’re converting the milk amount to ounces by setting the targetUnit parameter to oz.

Here are some of the fields from the API response:

Response Fields

Name

Type

Description

sourceAmount

Number

The source amount of the ingredient

sourceUnit

String

The source unit of the ingredient’s amount

targetAmount

Number

The target amount of the ingredient

targetUnit

String

The target unit of the ingredient’s amount

answer

String

The text string detailing the conversion