Fetch Ingredients

Learn about the different ways you can search and fetch food ingredients using the Spoonacular API.

We can use the Spoonacular API to search for ingredients and fetch information about them. To do so, we can use the following endpoints based on our use case:

  • Search ingredients
  • Fetch ingredient

The search ingredients endpoint

The search ingredients endpoint performs an ingredient search for a given query of simple whole foods. The search ingredients endpoint has a cost of 11 quota point, while each ingredient returned has a cost of 0.010.01 quota points if the metaInformation parameter is set to true. This endpoint utilizes the GET method and uses the following URL:

GET https://api.spoonacular.com/food/ingredients/search

Request parameters

Here are some of the request parameters we can use with the search ingredients endpoint when performing an ingredient search:

Parameter

Type

Category

Description

query

String

Optional

The search query to look up the ingredient

addChildren

Boolean

Optional

The option, if set to true, ensures the API also fetches the child ingredients of the queried ingredients

minProteinPercent

Number

Optional

The minimum percentage of protein the ingredients that we’re fetching should have

Possible values: 0100

maxProteinPercent

Number

Optional

The maximum percentage of protein the ingredients that we’re fetching should have

Possible values: 0100

minFatPercent

Number

Optional

The minimum percentage of fat that the ingredients we’re fetching should have

Possible values: 0100

maxFatPercent

Number

Optional

The maximum percentage of fat that the ingredients we’re fetching should have

Possible values: 0100

minCarbsPercent

Number

Optional

The minimum percentage of carbohydrates that the ingredients we’re fetching should have

Possible values: 0100

maxCarbsPercent

Number

Optional

The maximum percentage of carbohydrates that the ingredients we’re fetching should have

Possible values: 0100

metaInformation

Boolean

Optional

The option, if set to true, includes the meta information about each ingredient in the API response

intolerances

String

Optional

The health intolerance(s) for which the queried results should not contain any associated ingredients. This parameter accepts a single intolerance or a comma-separated list of intolerances. A complete list of all supported intolerance values can be found in the Supported Parameter Values lesson.

sort

String

Optional

The sorting option to sort the ingredients by. A complete list of all supported sorting options can be found in the Supported Parameter Values lesson.

sortDirection

String

Optional

The direction in which to sort the results. To sort in ascending order, we use asc, and to sort in descending order, we use desc.

language

String

Optional

The language code to set the language of the response

Let’s see how to call the API endpoint to query ingredients. Click the “Run” button to execute the code:

Press + to interact
// Import libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://api.spoonacular.com/food/ingredients/search');
// 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({
query: 'chocolate',
addChildren: true,
metaInformation: true,
number: 10,
});
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function searchIngredients(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
searchIngredients(endpointUrl, endpointOptions);

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

  • Line 5: We use the endpoint URL, /food/ingredients/search, to search for ingredients.

  • 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–22: We define the query parameters for the API call. We can modify or add parameters using the queryParameters variable.

    • Line 18: We fetch chocolate-related ingredients using the query parameter to set the search query as chocolate.

    • Line 19: We also fetch the child ingredients of the queried ingredients by setting the addChildren parameter to true.

    • Line 20: We fetch more meta-information about the ingredient by setting the metaInformation parameter to true.

Response fields

The response of this endpoint is an array of ingredient objects stored in the results field. Here are some of the fields from the API response:

Name

Type

Description

id

Number

The ID of the ingredient

name

String

The title name of the ingredient

image

String

The filename of the ingredient’s image

aisle

String

The shopping aisle in which the ingredient would fall into

possibleUnits

String[ ]

The possible units that can be used with the ingredient

The fetch ingredient endpoint

The fetch ingredient endpoint takes in the ID of an ingredient as a path parameter and fetches detailed information for that ingredient. The fetch ingredient endpoint has a cost of 11 quota point. The endpoint method and URL are as follows:

GET https://api.spoonacular.com/food/ingredients/{id}/information

We can use one of the following ingredient IDs for the code widget in this section:

Ingredient Name

ID

Banana

9040

Bread

18064

Chicken Wings

5100

Tomato

11529

Chocolate Chips

99278

Milk

1077

Request parameters

Here are the request parameters we can use with the fetch ingredient endpoint:

Parameter

Type

Category

Description

id

Number

Required

The identifier of the ingredient for which we’re fetching the detailed information.

amount

Number

Optional

The amount of the ingredient whose information we’re fetching.

unit

String

Optional

The unit for the amount of the ingredient whose information we’re fetching.

Let’s see how to call the API endpoint. Click the “Run” button to execute the code. Make sure you’ve provided the value for INGREDIENT_ID:

Press + to interact
// 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}/information`);
// Define Query Parameters here
const queryParameters = new URLSearchParams({
amount: 50,
unit: 'ounces',
});
// 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
fetchIngredientInformation(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 fetch the information.

  • Line 5: We use the endpoint URL, /ingredient/{id}/information, to fetch detailed information about the ingredient.

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

Response fields

The response of this endpoint is an array of ingredient objects stored in the results field. Here are some of the fields from the API response:

Name

Type

Description

id

Number

The ID of the ingredient

name

String

The title name of the ingredient

amount

Number

The amount of the ingredient

unit

String

The ingredient’s amount unit

image

String

The filename of the ingredient’s image

estimatedCost

Object

The estimated cost of the ingredient

aisle

String

The shopping aisle in which the ingredient would fall into

nutrition

Object[ ]

A list of nutrition objects related to the ingredient