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 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 |
| String | Optional | The search query to look up the ingredient |
| Boolean | Optional | The option, if set to |
| Number | Optional | The minimum percentage of protein the ingredients that we’re fetching should have Possible values: |
| Number | Optional | The maximum percentage of protein the ingredients that we’re fetching should have Possible values: |
| Number | Optional | The minimum percentage of fat that the ingredients we’re fetching should have Possible values: |
| Number | Optional | The maximum percentage of fat that the ingredients we’re fetching should have Possible values: |
| Number | Optional | The minimum percentage of carbohydrates that the ingredients we’re fetching should have Possible values: |
| Number | Optional | The maximum percentage of carbohydrates that the ingredients we’re fetching should have Possible values: |
| Boolean | Optional | The option, if set to |
| 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. |
| 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. |
| String | Optional | The direction in which to sort the results. To sort in ascending order, we use |
| 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:
// Import libraries hereimport fetch from 'node-fetch';// Define endpoint URL hereconst endpointUrl = new URL('https://api.spoonacular.com/food/ingredients/search');// 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({query: 'chocolate',addChildren: true,metaInformation: true,number: 10,});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Function to make API callasync function searchIngredients(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 callsearchIngredients(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 thex-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 aschocolate
.Line 19: We also fetch the child ingredients of the queried ingredients by setting the
addChildren
parameter totrue
.Line 20: We fetch more meta-information about the ingredient by setting the
metaInformation
parameter totrue
.
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 |
| Number | The ID of the ingredient |
| String | The title name of the ingredient |
| String | The filename of the ingredient’s image |
| String | The shopping aisle in which the ingredient would fall into |
| 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
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 |
| Number | Required | The identifier of the ingredient for which we’re fetching the detailed information. |
| Number | Optional | The amount of the ingredient whose information we’re fetching. |
| 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
:
// Define the ID of any ingredient hereconst id = '{{INGREDIENT_ID}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.spoonacular.com/food/ingredients/${id}/information`);// Define Query Parameters hereconst queryParameters = new URLSearchParams({amount: 50,unit: 'ounces',});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Attaching query parameters to the endpoint URLendpointUrl.search = queryParameters;// Calling function to make API callfetchIngredientInformation(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 |
| Number | The ID of the ingredient |
| String | The title name of the ingredient |
| Number | The amount of the ingredient |
| String | The ingredient’s amount unit |
| String | The filename of the ingredient’s image |
| Object | The estimated cost of the ingredient |
| String | The shopping aisle in which the ingredient would fall into |
| Object[ ] | A list of nutrition objects related to the ingredient |