Search Grocery Products
Learn about the different ways you can search for grocery products using the Spoonacular API.
We can use the Spoonacular API to search for grocery products either by a search query or nutrients. To do so, we can use the following endpoints based on our use case:
- search products by query string
- search products by UPC
- search products by ingredients
The search products by query string endpoint
The search products by query string endpoint performs a product search for a given query of packaged food products. The search products by query string endpoint has a cost of addProductInformation
parameter is set to true
, each product will have a cost of
GET https://api.spoonacular.com/food/products/search
Here are some of the request parameters we can use with the search products by query string endpoint:
Request Parameters
Parameter | Type | Category | Description |
| String | Optional | The search query to look up the ingredient. |
| Number | Optional | The minimum number of calories that the products we’re fetching should have. |
| Number | Optional | The maximum number of calories that the products we’re fetching should have. |
| Number | Optional | The minimum number of carbohydrates that the products we’re fetching should have in grams. |
| Number | Optional | The maximum number of carbohydrates that the products we’re fetching should have in grams. |
| Number | Optional | The minimum amount of protein that the products we’re fetching should have in grams. |
| Number | Optional | The maximum amount of protein that the products we’re fetching should have in grams. |
| Number | Optional | The maximum amount of fat that the products we’re fetching should have in grams. |
| Number | Optional | The maximum amount of fat that the products we’re fetching should have in grams. |
| Boolean | Optional | The option, if set to |
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/products/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',maxCalories: 500,minProtein: 10,number: 10,});// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Function to make API callasync function searchProductsByQuery(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 callsearchProductsByQuery(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 5: We use the endpoint URL
/food/products/search
to search for grocery products using a search query.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 products using the
query
parameter to set the search query aschocolate
.Line 19: We fetch products with a maximum calorie amount of
500
using themaxCalories
parameter.Line 20: We fetch products with a minimum protein amount of
10
using theminProtein
parameter.
The response of this endpoint contains an array of product objects stored in the products
field. Here are some of the fields from the API response:
Response Fields
Name | Type | Description |
| Number | The ID of the product |
| String | The title name of the product |
| String | The URL of the product’s image |
| String | The image type of product's image |
The search products by UPC endpoint takes in the Universal Product Code (UPC) of a product as a path parameter and fetches detailed information for that product.
The UPC is something that can we can find printed on retail products to identify them. It is a combination of machine-readable bar codes and 12-digit number code that contains information like brand name, item name, color, and size.
The search products by UPC endpoint has a cost of quota point. The endpoint method and URL are as follows:
GET https://api.spoonacular.com/food/products/upc/{upc}
We can use one of the following product UPCs for the code widget in this section:
Product UPCs
Product Name | UPC |
Hershey’s Milk Chocolate Bar | 034000232581 |
Kellogg's Froot Loops | 643952402227 |
Swan Flour | 041631000564 |
Doritos Chips | 028400420730 |
Here is the request parameter we can use with the search products by UPC endpoint:
Request Parameters
Parameter | Type | Category | Description |
| Number | Required | The UPC of the product for which we’re fetching the detailed information. |
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 PRODUCT_UPC
.
// Define the UPC of any product hereconst upc = '{{PRODUCT_UPC}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.spoonacular.com/food/products/upc/${upc}`);// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Calling function to make API callsearchProductsByUPC(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 2: We set and store the UPC of a product that we want to use to fetch the product.
Line 5: We use the endpoint URL
/food/products/upc/{upc}
to fetch detailed information about the product using its UPC.
Here are some of the fields from the API response:
Response Fields
Name | Type | Description |
| Number | The ID of the product |
| String | The title name of the product |
| Number | The price of the product |
| String[ ] | The list of diets and intolerances associated with the product |
| Object[ ] | A list of nutrition objects related to the product |
| Object[ ] | A list of ingredient objects related to the product |
| String | The brand of the product |
The search products by ingredients endpoint
The search products by ingredients endpoint takes in a list of ingredients and servings as body parameters and maps them to grocery store products. The search products by ingredients endpoint has a cost of
POST https://api.spoonacular.com/food/ingredients/map
Here are some of the request body parameters we can use with the search products by ingredients endpoint:
Request Parameters
Parameter | Type | Category | Description |
| String[ ] | Required | The list of ingredients for which we want to map grocery products. We need to pass this parameter in the request body rather than as a query or path parameter. |
| Number | Required | The number of people that the product can serve. We need to pass this parameter in the request body rather than as a query or path parameter. |
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/food/ingredients/map');// Define Body Parameters hereconst bodyParameters = JSON.stringify({'ingredients': ['eggs','bread','milk',],'servings': 2,});// Setting API call optionsconst endpointOptions = {method: 'POST',headers: headerParameters,body: bodyParameters,};// Calling function to make API callsearchProductsByIngredients(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 2: We use the endpoint URL
/food/ingredients/map
to search for grocery products using a list of ingredients.Lines 5–12: We define the body parameters for the API call. We can modify or add parameters using the
bodyParameters
variable.
The response of this endpoint contains an array of queried ingredients in which we can find the array of product objects in the products
field. Here are some of the fields from the API response:
Response Fields
Name | Type | Description |
| String | The name of the ingredient used in the search query |
| Object[ ] | The list of product objects associated with the ingredient |
| Number | The ID of the product |
| String | The UPC of the product |
| String | The title name of the product |