Product Delivery API Basics
Product API Basics covers the admin API used for creating and managing products. To display product data in your storefront, you must use the Product Delivery API. Call the headless-commerce-delivery-catalog services to retrieve a single product from a channel or all products from a channel.
Get a Product from a Channel
Start a new Liferay DXP instance by running
docker run -it -m 8g -p 8080:8080 liferay/dxp:2024.q2.11
Sign in to Liferay at http://localhost:8080 using the email address test@liferay.com and the password test. When prompted, change the password to learn.
Then follow these steps:
-
Download and unzip Product Delivery API Basics.
curl https://resources.learn.liferay.com/commerce/latest/en/product-management/developer-guide/liferay-k9b9.zip -O
unzip liferay-k9b9.zip
-
By default, a product is visible across all channels. You can limit its visibility through configuration. See Configuring Product Visibility Using Channels for more information.
Find the channel ID by opening the Global Menu () and navigating to Commerce → Channels. Select a channel to see its
ID
.Find the product ID by opening the Global Menu () and navigating to Commerce → Products. Select a product to see its
ID
. -
Use the cURL script to retrieve a product from a channel. On the command line, navigate to the
curl
folder. Execute theProducts_GET_ById.sh
script with your channel ID and product ID as parameters. Replace1234
with the channel ID and5678
with the product ID../Products_GET_ById.sh 1234 5678
The JSON response shows the product:
{ "catalogName": "Minium", "createDate": "2024-11-11T13:15:37Z", "customFields": [], "description": "Product designed and manufactured to accommodate OEM applications. All\nproducts are tested and inspected in an ISO-9000 compliant environment", "expando": {}, "externalReferenceCode": "MIN93015minium-initializer", "id": 34309, "metaDescription": "", "metaKeyword": "", "metaTitle": "", "modifiedDate": "2024-11-11T13:35:41Z", "name": "ABS Sensor", "productConfiguration": { "allowBackOrder": true, "allowedOrderQuantities": [], "availabilityEstimateId": 34326, "availabilityEstimateName": "3-5 days", "inventoryEngine": "", "maxOrderQuantity": 10000, "minOrderQuantity": 1, "multipleOrderQuantity": 1 }, "productId": 34310, "productType": "simple", "shortDescription": "", "slug": "abs-sensor", "tags": [], "urlImage": "https://localhost:8080/o/commerce-media/default/?groupId=34197", "urls": { "en_US": "abs-sensor" } }
-
You can also call the REST service using the Java client. Navigate out of the
curl
folder and into thejava
folder. Compile the source files:javac -classpath .:* *.java
-
Run the
Products_GET_ById
class. Replace1234
with the channel ID and5678
with the product ID.java -classpath .:* -DchannelId=1234 -DproductId=5678 Products_GET_ById
Examine the cURL Command
The Products_GET_ById.sh
script calls the REST service with a cURL command.
curl \
"http://localhost:8080/o/headless-commerce-delivery-catalog/v1.0/channels/${1}/products/${2}" \
--user "test@liferay.com:learn"
Here are the command’s arguments:
Arguments | Description |
---|---|
"http://localhost:8080/o/headless-commerce-delivery-catalog/v1.0/channels/${1}/products/${2}" | The REST service endpoint |
-u "test@liferay.com:learn" | Basic authentication credentials |
Basic authentication is used here for demonstration purposes. For production, you should authorize users via OAuth2. See Using OAuth2 to Authorize Users for a sample React application that utilizes OAuth2.
Examine the Java Class
The Products_GET_ById.java
class retrieves a product from a channel by calling the product-related service.
public static void main(String[] args) throws Exception {
ProductResource.Builder builder = ProductResource.builder();
ProductResource productResource = builder.authentication(
"test@liferay.com", "learn"
).build();
System.out.println(
productResource.getChannelProduct(
Long.valueOf(System.getProperty("channelId")),
Long.valueOf(System.getProperty("productId")), null));
}
This class invokes the REST service using only three lines of code:
Line (abbreviated) | Description |
---|---|
ProductResource.Builder builder = ... | Gets a Builder for generating a ProductResource service instance. |
ProductResource productResource = builder.authentication(...).build(); | Specifies basic authentication and generates a ProductResource service instance. |
productResource.getChannelProduct(...); | Calls the productResource.getChannelProduct method and passes the parameters to retrieve a product. |
Note that the project includes the com.liferay.headless.commerce.delivery.catalog.client.jar
file as a dependency. You can find client JAR dependency information for all REST applications in the API explorer in your installation at /o/api
.
The main
method’s comment demonstrates running the class.
The other example Java class is similar to this one, but calls a different ProductResource
method.
See ProductResource for service details.
Below is another example of calling a Product
REST service using cURL and Java.
Get all Products from a Channel
You can retrieve all products from a channel by executing the following cURL or Java command.
Products_GET_FromChannels.sh
Command:
./Products_GET_FromChannels.sh
Code:
curl \
"http://localhost:8080/o/headless-commerce-delivery-catalog/v1.0/channels/${1}/products" \
--user "test@liferay.com:learn"
Products_GET_FromChannels.java
Command:
java -classpath .:* Products_GET_FromChannels
Code:
public static void main(String[] args) throws Exception {
ProductResource.Builder builder = ProductResource.builder();
ProductResource productResource = builder.authentication(
"test@liferay.com", "learn"
).build();
Page<Product> page = productResource.getChannelProductsPage(
Long.valueOf(System.getProperty("channelId")), null, null, null,
Pagination.of(1, 2), null);
System.out.println(page);
}
The Product
channel objects’ output is JSON.
This API also accepts parameters to filter, paginate, search, and sort the products. See the getProductsPage
method for more information. You can use the following Product fields in your queries to filter, search, and sort the results.
catalogId
categoryIds
categoryNames
createDate
customFields
Liferay DXP 2024.Q4+/GA129+modifiedDate
name
productType
statusCode
tags
Filter Query | Description |
---|---|
productType eq 'simple' | Product type equals simple |
contains(name, 'Bar') | Product name contains Bar |
customFields/sampleSize eq '100.0' | Custom field named sampleSize equals 100 |
Sort Query | Description |
---|---|
createDate:desc | Sort by createDate in the descending order |
name:asc | Sort by name in the ascending order |
createDate:desc,name:desc | Sort by createDate in the descending order first, then by name in the descending order |
Read API Query Parameters for more information.
Both the above endpoints also have an optional accountId
parameter. When a user is part of multiple accounts, you must pass the accountId
while invoking these APIs.