In this article, we will be looking into how we can get the Products in Magento 2 using its API.
- Get Product by SKU
- Get Multiple Products using SearchCriteria
- Get Single Product by product ID using SearchCriteria
Anonymouse APIs Restriction
The REST endpoints to get the stores’ information falls under the anonymous APIs category. However, in the recent Magento version, those API endpoints are restricted. They need “admin” or “integration” token to authenticate.
If you need/want to bypass the token authentication for those APIs, you can disable this feature by going to:
Magento Admin > STORES > Settings > Configuration > Services > Magento Web API > Web API Security > Allow Anonymous Guest Access = Yes.
NOTE:
- We will be passing the admin bearer token to authenticate the APIs.
- This article shows how we can get the admin token in Magento: Magento 2 API: Get Admin Token
Get Product by SKU
API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/productssku#operation/GetV1ProductsSku
Endpoint:
GET <host>/rest/V1/products/{sku}
Authentication Bearer Token:
<admin-token>
Response:
{
"id": 1,
"sku": "24-MB01",
"name": "Joust Duffle Bag",
"attribute_set_id": 15,
"price": 34,
"status": 1,
"visibility": 4,
"type_id": "simple",
"created_at": "2022-11-24 23:51:57",
"updated_at": "2022-11-24 23:51:57",
"extension_attributes": {
...
...
Get Product by SKU using cURL Request
curl --location --request GET 'https://app.your-project.test/rest/V1/products/24-MB01' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'
Get Multiple Products using SearchCriteria
API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/products#operation/GetV1Products
Endpoint:
GET <host>/rest/V1/products?searchCriteria[pageSize]={pageSize}
Authentication Bearer Token:
<admin-token>
Response:
{
"items": [
{
"id": 1,
"sku": "24-MB01",
"name": "Joust Duffle Bag",
"attribute_set_id": 15,
"price": 34,
"status": 1,
"visibility": 4,
"type_id": "simple",
"created_at": "2022-11-24 23:51:57",
"updated_at": "2022-11-24 23:51:57",
"extension_attributes": {
...
...
],
"search_criteria": {
"filter_groups": [],
"page_size": 3
},
"total_count": 2040
}
Get Multiple Products using cURL Request
curl --location --request GET 'https://app.your-project.test/rest/V1/products?searchCriteria[pageSize]=3' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'
Get Single Product by product ID using SearchCriteria
API Doc:
https://adobe-commerce.redoc.ly/2.4.5-admin/tag/products#operation/GetV1Products
Endpoint:
GET <host>/rest/V1/products
Params:
In the example below, we are searching for a product with the product ID equals to 1.
SearchCriteria Doc: https://developer.adobe.com/commerce/webapi/rest/use-rest/performing-searches/
searchCriteria[filterGroups][0][filters][0][conditionType]. eq
searchCriteria[filterGroups][0][filters][0][field] entity_id
searchCriteria[filterGroups][0][filters][0][value] 1
Authentication Bearer Token:
<admin-token>
Response:
{
"items": [
{
"id": 1,
"sku": "24-MB01",
"name": "Joust Duffle Bag",
"attribute_set_id": 15,
"price": 34,
"status": 1,
"visibility": 4,
"type_id": "simple",
"created_at": "2022-11-24 23:51:57",
"updated_at": "2022-11-24 23:51:57",
"extension_attributes": {
...
...
],
"search_criteria": {
"filter_groups": [
{
"filters": [
{
"field": "entity_id",
"value": "1",
"condition_type": "eq"
}
]
}
]
},
"total_count": 1
}
Get Single Product using cURL Request
curl --location --request GET 'https://app.your-project.test/rest/V1/products?searchCriteria[filterGroups][0][filters][0][conditionType]=eq&searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][value]=1' \
--header 'Authorization: Bearer eyJraWQiOiIxIiwiYWxnIjoiSFMyNxxxxx'
Hope this helps. Thanks.
Magento REST API Reference: https://developer.adobe.com/commerce/webapi/rest/quick-reference/