Simple way to retrieve and update Magento2 product stock via REST API:
- Fetch the authorization token:
curl -X POST \ https://my.magento2store.com/rest/default/V1/integration/admin/token \ -H 'cache-control: no-cache' \ -H 'content-type: application/json' \ -d '{ "username": "username", "password": "password" }'
It should return a string token as result:
"0m4l40jicu4luxr2npf7nucr4wslb75s"
- Fetch live product stock:
curl -X GET \ https://my.magento2store.com/rest/V1/stockItems/SKU-0001 \ -H 'authorization: Bearer <auth_token>' \ -H 'cache-control: no-cache' \ -H 'content-type: application/json'
Result:
{ "item_id": 101, "product_id": 123, "stock_id": 1, "qty": 33, "is_in_stock": true, "is_qty_decimal": false, "show_default_notification_message": false, "use_config_min_qty": true, "min_qty": 0, "use_config_min_sale_qty": 1, "min_sale_qty": 1, "use_config_max_sale_qty": true, "max_sale_qty": 10000, "use_config_backorders": true, "backorders": 0, "use_config_notify_stock_qty": true, "notify_stock_qty": 1, "use_config_qty_increments": true, "qty_increments": 0, "use_config_enable_qty_inc": true, "enable_qty_increments": false, "use_config_manage_stock": true, "manage_stock": true, "low_stock_date": null, "is_decimal_divided": false, "stock_status_changed_auto": 0 }
- Update live stock:
curl -X PUT \ https://my.magento2store.com/rest/V1/products/SKU-0001/stockItems/1 \ -H 'authorization: Bearer <auth_token>' \ -H 'cache-control: no-cache' \ -H 'content-type: application/json' \ -d '{"stockItem":{"qty":100, "is_in_stock": true}}'
Top comments (0)