Find Ingredient Substitutes

Learn how to find substitutes for a given ingredient using the Spoonacular API.

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

Press + to interact

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

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

ingredientName

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:

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://api.spoonacular.com/food/ingredients/substitutes');
// 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({
ingredientName: 'butter',
});
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function findIngredientSubstituteName(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
findIngredientSubstituteName(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 the x-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 to butter.

Here are the fields from the API response:

Response Fields

Name

Type

Description

status

String

The status of the API request

ingredient

String

The name of the ingredient for which we’re finding the substitute

substitutes

String[ ]

The list of substitutes for the ingredient

message

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

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

id

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.

Press + to interact
// Define the ID of any ingredient here
const id = '{{INGREDIENT_SUBSTITUTE_ID}}';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.spoonacular.com/food/ingredients/${id}/substitutes`);
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Calling function to make API call
findIngredientSubstituteByID(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.