Magento2 API Stock Update – How to Manage Stock with JSON

apijsonmagento2manage-stock

Using Magento 2.2.2, I was trying to add stock updates to a pre-existing setup using the official defined endpoint:

rest/V1/products/testsku/stockItems/1

with

{ 
  "stock_item": {
    "qty": 1234,
    "is_in_stock": true
  }
}

which wouldn't work. Searching around, I found an example here which essentially strips the product create/update JSON right back to the bare minimum and just include the stock data:

rest/V1/products/testsku

.

{
  "product": {
    "sku": "testsku",
    "extensionattributes": {
      "stockitem": {
        "qty": 1234,
        "isinstock": true,
      }
    }
  },
 "saveOptions": true
}

I changed my stock update process to call this instead, and it works.

However, I noticed afterwards that the extensionattributes (and it's sub fields) is missing the underscore, according to the official swagger API documentation. My product create call already includes the underscore fields when creating the item to set an initial stock quantity, yet don't seem to be working.

What's the difference – underscore or no underscore? Which is correct? Is this a bug or an undocumented change, or are both field names valid?

Best Answer

Using this documentation : Magento Commerce for B2B v2.2, I was able successfully create API calls for the stockItem.

This is a PUT operation, make sure that you get the correct item_id for that product.

/V1/products/{productSku}/stockItems/{itemId}

item_id from GET Product API:

"stock_item": { "item_id": 4721, "qty": 1234, "is_in_stock": true }

Also, regarding the underscores, I think it would be safer to follow what is in the documentation, but based on my experience, both works:

enter image description here

PS. I use Postman for testing the APIs.

Related Topic