Find Ingredient Substitutes
Learn how to find substitutes for a given ingredient using the Spoonacular API.
We'll cover the following
We can use the Spoonacular API to find good substitutes for unavailable ingredients so we can still cook our favorite recipes. To do so, we can use the following endpoints based on our use case:
ingredient substitute by name
ingredient substitute by ID
The ingredient substitute by name endpoint
The ingredient substitute by name endpoint takes in the name of an ingredient as a query parameter and fetches substitute ingredients for it. The ingredient substitute by name endpoint has a cost of
GET https://api.spoonacular.com/food/ingredients/substitutes
Here is the request parameter we can use with the ingredient substitute by name endpoint:
Request Parameters
Parameter | Type | Category | Description |
| String | Optional | The name of the ingredient for which we’re finding substitutes. |
Let’s see how to call the API endpoint. Click the “Run” button to execute the code:
// Importing libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://api.spoonacular.com/food/ingredients/substitutes');// 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({ingredientName: 'butter',});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Function to make API callasync function findIngredientSubstituteName(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 callfindIngredientSubstituteName(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 5: We use the endpoint URL
/food/ingredients/substitutes
to find the substitute of an ingredient by its name.Line 8: We assign the API key here to the
apiKey
variable.Lines 11–14: We define the header parameters for the API call. We can modify or add parameters using the
headerParameters
variable.Line 12: We pass the
apiKey
variable to thex-api-key
header parameter.
Lines 17–19: We define the query parameters for the API call. We can modify or add parameters using the
queryParameters
variable.Line 18: We find ingredient substitutes for butter by setting the
ingredientName
parameter tobutter
.
Here are the fields from the API response:
Response Fields
Name | Type | Description |
| String | The status of the API request |
| String | The name of the ingredient for which we’re finding the substitute |
| String[ ] | The list of substitutes for the ingredient |
| String | A text string that relays if Spoonacular finds any substitutes |
The ingredient substitute by ID endpoint
The ingredient substitute by ID endpoint takes in the ID of an ingredient as a path parameter and fetches substitute ingredients for it. The ingredient substitute by ID endpoint has a cost of
GET https://api.spoonacular.com/food/ingredients/{id}/substitutes
We can use one of the following ingredient IDs for the code widget in this section:
Ingredient IDs
Ingredient Name | ID |
Butter | 1001 |
Olive Oil | 4053 |
Soy Sauce | 16124 |
Ketchup | 11935 |
Garam Masala | 93663 |
Here is the request parameter we can use with the ingredient substitute by ID endpoint:
Request Parameters
Parameter | Type | Category | Description |
| Number | Required | The ID of the ingredient for which we’re finding substitutes. |
Let’s see how to call the API endpoint. Click the “Run” button to execute the code. Make sure the value for INGREDIENT_SUBSTITUTE_ID
from the table in this lesson has been provided since not all ingredients have substitutes.
// Define the ID of any ingredient hereconst id = '{{INGREDIENT_SUBSTITUTE_ID}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.spoonacular.com/food/ingredients/${id}/substitutes`);// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Calling function to make API callfindIngredientSubstituteByID(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 2: We set and store the ID of an ingredient for which we want to find substitutes.
Line 5: We use the endpoint URL
/food/ingredients/{id}/substitutes
to find the substitute of an ingredient by its ID.
The response fields for this endpoint are the same as the ones for the ingredient substitute by name endpoint in the table above.