skip to Main Content

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.

3

Answers


  1. Chosen as BEST ANSWER

    This query was used to successfully query whether the product_idexists

    http://example.com/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][condition_type]=eq&searchCriteria[filterGroups][0][filters][0][value]=[product_id]
    

    Go to the API Docs: and then to product to learn more.

    enter image description here


  2. 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.

    Login or Signup to reply.
  3. Search endpoint can be used to achieve what you need.
    Example request URL would look like this:

    http://localhost/rest/all/V1/products?searchCriteria[filterGroups][0][filters][0][field]=entity_id&searchCriteria[filterGroups][0][filters][0][value]=<id>
    

    Note that the store code (all) might differ.

    See Magento docs for more info.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search