Search code examples
postmagentomagento2

How Do I Get the Magento API 2 Product By ID (Not SKU)?


Use-case:

When creating a product, it is my understanding that a key/value pair of id is required for all product types for the HTTP POST request into the Magento API Endpoint /pub/rest/default/V1/products.

Issue:

However, as I've experienced, I can overwrite products when using the same id. Thusly, I would like to check whether the id exists in Magento before using it.

Request:

Which endpoint can I use to query to see whether an id exists? Looking at the Docs I couldn't see anywhere to support this requirement.


Solution

  • In the docs, an id of 0 is used. If an id is not provided, it will be created and returned in the response.

    The id should not be specified for a new product, though, since it's an auto-generated field. Your unique identifier for a product should be sku. To confirm that a SKU doesn't exist, you could call https://adobe-commerce.redoc.ly/2.4.5-admin/tag/productssku#operation/GetV1ProductsSku. This endpoint will also return the id for the product.

    Having a way to uniquely identify your product before adding/updating is pretty important, but if you want to do indiscriminate adds, you can just call that endpoint you reference but either use an id of 0 or don't include id at all.