Recipe Ingredients Widget

Learn how to fetch interactive Spoonacular widgets that display the ingredients data for recipes.

We can use the Spoonacular API to fetch widgets that display data on the recipes’ ingredients in an interactive way. To do so, we can use the following endpoints based on our use case:

  • recipe ingredients widget
  • recipe ingredients by ID widget

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

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 ingredients widget endpoint

The recipe ingredients widget endpoint fetches a widget that displays the data on the ingredients of a recipe in an interactive way. The recipe ingredients widget endpoint has a cost of 11quota point. This endpoint utilizes the GET method and uses the following URL:

POST https://api.spoonacular.com/recipes/visualizeIngredients

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

Request Parameters

Parameter

Type

Category

Description

ingredientList

String

Required

The list of ingredients of the recipe with each ingredient on a new line with the ingredient amount. Here’s an example:

chocolate

2 cups of coffee

1.4 liters almond milk

We need to pass this parameter in the request body rather than as a query or path parameter.

servings

Number

Optional

The number of servings of the recipe. We need to pass this parameter in the request body rather than as a query or path parameter.

measure

String

Optional

The measurement system option. We can choose either the metric or the us measurement system. The default value of this parameter is us. We need to pass this parameter in the request body rather than as a query or path parameter.

view

String

Optional

The option provides a choice of how to view the equipment. We can choose a view of either a list or a grid. The default value of this parameter is grid. We need to pass this parameter in the request body rather than as a query or path parameter.

defaultCss

Boolean

Optional

The option, if set to true, adds the default CSS to the response. The default value of this parameter is true. We need to pass this parameter in the request body rather than as a query or path parameter.

showBacklink

Boolean

Optional

The option, if set to true, shows a backlink to Spoonacular, and there’s no quota cost. The default value of this parameter is true. We need to pass this parameter in the request body rather than as a query or path parameter.

language

String

Optional

The input language of the response. It can be either en or de. The default value of this parameter is en. We need to pass this parameter as a query or path parameter.

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/visualizeIngredients`);
// Define API key here
const apiKey = '{{API_KEY}}';
// Define Header Parameters here
const headerParameters = {
'x-api-key': apiKey,
'accept': 'text/html',
'content-type': 'application/x-www-form-urlencoded',
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
language: 'en',
});
// Define Body Parameters here
const bodyParameters = {
ingredientList: '1 potato\n \
2 tbsp lemon juice\n \
1 cup of olive oil\n \
2 1/2 salmon fillets\n \
kale',
servings: 3,
view: 'grid',
measure: 'metric',
defaultCss: true,
}
// Converting body parameters as form data
const bodyFormData = new URLSearchParams();
for (key in bodyParameters) {
bodyFormData.append(key, bodyParameters[key])
}
// Setting API call options
const endpointOptions = {
method: 'POST',
headers: headerParameters,
body: bodyFormData,
};
// Function to make API call
async function recipeIngredientsWidget(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
recipeIngredientsWidget(endpointUrl, endpointOptions);

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

  • Line 2: We use the endpoint URL /recipes/visualizeIngredients to fetch the Spoonacular widget that displays the data on the recipe's ingredients in an interactive way.

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

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

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

    • Line 10: We need to set the accept header parameter to text/html.

    • Line 11: We need to set the content-type header parameter to application/x-www-form-urlencoded.

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

  • Lines 20–30: We define the body parameters for the API call. We can modify or add parameters using the bodyParameters variable.

The recipe ingredients by ID widget endpoint

The recipe ingredients by ID widget endpoint takes in the ID of a recipe as a path parameter and fetches a widget that displays the data on the recipe's ingredients in an interactive way. The recipe ingredients by ID widget endpoint has a cost of 11quota point. This endpoint utilizes the GET method and uses the following URL:

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

Here are the request parameters we can use with the recipe ingredients by ID widget endpoint:

Request Parameters

Parameter

Type

Category

Description

id

Number

Required

The identifier of the recipe for which we’re fetching the ingredients widget.

measure

String

Optional

The option provides a choice of what measurement system to use. We can choose either the metric or the us measurement system. The default value of this parameter is us.

defaultCss

Boolean

Optional

The option, if set to true, adds the default CSS to the response. The default value of this parameter is true.

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
// 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}/ingredientWidget`);
// Define Header Parameters here
const headerParameters = {
'x-api-key': apiKey,
'accept': 'text/html',
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
defaultCss: true,
});
// 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
recipeIngredientsWidget(endpointUrl, endpointOptions);

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

  • Line 2: We set and store the ID of a recipe for which we want to fetch the ingredients widget.

  • Line 5: We use the endpoint URL /recipes/{id}/ingredientWidget to fetch the Spoonacular widget that displays data on the recipe's ingredients in an interactive way.

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

    • Line 10: We need to set the accept header parameter to text/html.

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