Fetch Grocery Products

Learn how to fetch grocery products and also get related product suggestions using the Spoonacular API.

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

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

id

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.

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define the ID of any product here
const id = '{{PRODUCT_ID}}';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.spoonacular.com/food/products/${id}`);
// Define API key here
const apiKey = '{{API_KEY}}';
// Define Header Parameters here
const headerParameters = {
'x-api-key': apiKey,
'content-type': 'application/json',
};
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function fetchProductInformation(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);
}
}
// Calling function to make API call
fetchProductInformation(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 the x-api-key header parameter.

Here are some of the fields from the API response:

Response Fields

Name

Type

Description

id

Number

The ID of the product

title

String

The title name of the product

price

Number

The price of the product

image

String

The URL of the product’s image

imageType

String

The image type of the product’s image

nutrition

Object[ ]

A list of nutrition objects related to the product

upc

String

The UPC of the product

brand

String

The brand of the product

The similar products endpoint

The similar products endpoint takes in the UPCUPC (Universal Product Code) 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. of an ingredient as a path parameter and fetches similar product suggestions. The similar products endpoint has a cost of 11quota point. The endpoint method and URL are as follows:

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

upc

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.

Press + to interact
// Define the UPC of any product here
const upc = '{{PRODUCT_UPC}}';
// Define endpoint URL here
const endpointUrl = new URL(`https://api.spoonacular.com/food/products/upc/${upc}/comparable`);
// Setting API call options
const endpointOptions = {
method: 'GET',
headers: headerParameters,
};
// Calling function to make API call
fetchSimilarProducts(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

price

Object[ ]

A list of price objects for each comparable product

sugar

Object[ ]

A list of sugar objects for each comparable product

protein

Object[ ]

A list of protein objects for each comparable product

calories

Object[ ]

A list of calories objects for each comparable product