Fetch Grocery Products
Learn how to fetch grocery products and also get related product suggestions using the Spoonacular API.
We'll cover the following
We can use the Spoonacular API to get detailed information for products or get a list of similar products in case the product we want is unavailable. To do so, we can use the following endpoints based on our use case:
- product information
- similar products
The product information endpoint
The product information endpoint takes in the ID of a product as a path parameter and fetches detailed information for that product. The product information endpoint has a cost of
GET https://api.spoonacular.com/food/products/{id}
We can use one of the following product IDs for the code widget in this section:
Product IDs
Product Name | ID |
Hershey’s Milk Chocolate Bar | 4596062 |
Papettis Peeled Hard Cooked Eggs | 5241240 |
Lupina Lupin Flour | 595371 |
Nando's Peri Peri Spicy Hot Sauce | 3501926 |
Pita Breads | 4679182 |
Here is the request parameter we can use with the product information endpoint:
Request Parameters
Parameter | Type | Category | Description |
| Number | Required | The identifier of the grocery 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 to provide the value for PRODUCT_ID
.
// Importing libraries hereimport fetch from 'node-fetch';// Define the ID of any product hereconst id = '{{PRODUCT_ID}}';// Define endpoint URL hereconst endpointUrl = new URL(`https://api.spoonacular.com/food/products/${id}`);// Define API key hereconst apiKey = '{{API_KEY}}';// Define Header Parameters hereconst headerParameters = {'x-api-key': apiKey,'content-type': 'application/json',};// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Function to make API callasync function fetchProductInformation(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);}}// Calling function to make API callfetchProductInformation(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 5: We set and store the ID of a product for which we want to fetch the information.
Line 8: We use the endpoint URL
/food/products/{id}
to fetch detailed information about the product.Line 11: We assign the API key to the
apiKey
variable.Lines 14–17: We define the header parameters for the API call. We can modify or add parameters using the
headerParameters
variable.Line 15: We pass the
apiKey
variable to thex-api-key
header parameter.
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 URL of the product’s image |
| String | The image type of the product’s image |
| Object[ ] | A list of nutrition objects related to the product |
| String | The UPC of the product |
| String | The brand of the product |
The similar products endpoint
The similar products endpoint takes in the UPC
GET https://api.spoonacular.com/food/products/upc/{upc}/comparable
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 Cereal | 643952402227 |
Swan Flour | 041631000564 |
Doritos Chips | 028400420730 |
Here is the request parameter we can use with the similar products endpoint:
Request Parameters
Parameter | Type | Category | Description |
| Number | Required | The UPC of the product for which we’re fetching similar products. |
Let’s see how to call the API endpoint. Click the “Run” button to execute the code. Please provide 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}/comparable`);// Setting API call optionsconst endpointOptions = {method: 'GET',headers: headerParameters,};// Calling function to make API callfetchSimilarProducts(endpointUrl, endpointOptions);
Take a look at the following explanation of the code above:
Line 2: We set and store the UPC of a product for which we want to fetch similar products.
Line 5: We use the endpoint URL
/food/products/upc/{upc}/comparable
to fetch similar product suggestions.
The response of this endpoint contains the comparable product object stored in the comparableProducts
field. Here are some of the fields from that response field:
Response Fields
Name | Type | Description |
| Object[ ] | A list of price objects for each comparable product |
| Object[ ] | A list of sugar objects for each comparable product |
| Object[ ] | A list of protein objects for each comparable product |
| Object[ ] | A list of calories objects for each comparable product |