DEV Community

mayconheerdt
mayconheerdt

Posted on

Magento2 stock update via REST API

Simple way to retrieve and update Magento2 product stock via REST API:

  1. 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"
}'
Enter fullscreen mode Exit fullscreen mode

It should return a string token as result:

"0m4l40jicu4luxr2npf7nucr4wslb75s"
Enter fullscreen mode Exit fullscreen mode
  1. 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'
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode
  1. 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}}'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)