Recipe Equipment Widget
Learn how to fetch interactive Spoonacular widgets that display the equipment data for recipes.
We'll cover the following
We can use the Spoonacular API to fetch Spoonacular’s widgets that display the recipes’ equipment data in an interactive way. To do so, we can use the following endpoints based on our use case:
- Recipe equipment widget
- Recipe equipment 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 equipment widget endpoint
The recipe equipment widget endpoint fetches a widget that displays the equipment data for a recipe in an interactive way. The recipe equipment widget endpoint has a cost of
POST https://api.spoonacular.com/recipes/visualizeEquipment
Here are the request parameters we can use with the recipe equipment widget endpoint:
Request Parameters
Parameter | Type | Category | Description |
| String | Required | The instructions of a recipe. We need to pass this parameter in the request body rather than as a query or path parameter. |
| String | Optional | The option on how to view the equipment. We can choose the view to be either |
| Boolean | Optional | The option, if set to |
| Boolean | Optional | The option, if set to |
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/visualizeEquipment`);// Define API key hereconst apiKey = '{{API_KEY}}';// Define Header Parameters hereconst headerParameters = {'x-api-key': apiKey,'accept': 'text/html','content-type': 'application/x-www-form-urlencoded',};// Define Body Parameters hereconst bodyParameters = {instructions: 'Preheat oven. Cut banana with a knife and put in a blender.',view: 'list',defaultCss: true,}// Converting body parameters as form dataconst bodyFormData = new URLSearchParams();for (key in bodyParameters) {bodyFormData.append(key, bodyParameters[key])}// Setting API call optionsconst endpointOptions = {method: 'POST',headers: headerParameters,body: bodyFormData,};// Function to make API callasync function recipeEquipmentWidget(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);}}// Calling function to make API callrecipeEquipmentWidget(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 2: We use the endpoint URL
/recipes/visualizeEquipment
to fetch the Spoonacular widget that displays the equipment data for a recipe 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 thex-api-key
header parameter.Line 10: We need to set the
accept
header parameter totext/html
.Line 11: We need to set the
content-type
header parameter toapplication/x-www-form-urlencoded
.
Lines 15–19: We define the body parameters for the API call. We can modify or add parameters using the
bodyParameters
variable.
The recipe equipment by ID widget endpoint
The recipe equipment by ID widget endpoint takes in the ID of a recipe as a path parameter and fetches a widget that displays the equipment data for a recipe in an interactive way. The recipe equipment by ID widget endpoint has a cost of
GET https://api.spoonacular.com/recipes/{id}/equipmentWidget
Here are the request parameters we can use with the recipe equipment by ID widget endpoint:
Request Parameters
Parameter | Type | Category | Description |
| Number | Required | The identifier of the recipe for which we’re fetching the equipment widget. |
| Boolean | Optional | The option, if set to |
Let’s see how to call the API endpoint. Click the “Run” button to execute the code. Please provide the value for RECIPE_ID
.
// Define the ID of any recipe hereconst id = '{{RECIPE_ID}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.spoonacular.com/recipes/${id}/equipmentWidget`);// Define Header Parameters hereconst headerParameters = {'x-api-key': apiKey,'accept': 'text/html',};// Define Query Parameters hereconst queryParameters = new URLSearchParams({defaultCss: true,});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Attaching query parameters to the endpoint URLendpointUrl.search = queryParameters;// Calling function to make API callrecipeEquipmentWidget(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 equipment widget.
Line 5: We use the endpoint URL
/recipes/{id}/equipmentWidget
to fetch the Spoonacular widget that displays the equipment data for the recipe 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 totext/html
.
Lines 14–16: We define the query parameters for the API call. We can modify or add parameters using the
queryParameters
variable.