How to update multiple products using Magento 2 REST API

Updated: Aug 20

We can able to update multiple products in the single Magento REST API request using bulk endpoints. Also, before using the Bulk API to process messages, you must install and configure RabbitMQ, which is the default message broker. See RabbitMQ.


How bulk API endpoints work?

We need to form the bulk API URL from the normal rest API URL and create a request array/json which holding multiple product information. When Magento identifies it is bulk API URL, it will put the array/json into the RabbitMQ queue, queue will run by either cron or manual command and update the information.


Installation and configuration of RabbitMQ

sudo apt install -y rabbitmq-server
sudo rabbitmq-plugins enable rabbitmq_management
sudo service rabbitmq-server restart


You can change the listening port and host details below if it is needed

sudo vi /etc/rabbitmq/rabbitmq-env.conf
sudo service rabbitmq-server restart 

If everything installed correctly, you can see the listening ports

Also, we can access the RabitMQ management UI in the browser by using the below link

http://127.0.0.1:15672/

RabbitMQ default user name and password is guest/guest. We can create a separate user as well using the below command

rabbitmqctl add_user username password
rabbitmqctl set_user_tags username administrator
rabbitmqctl set_permissions -p / username ".*" ".*" ".*" 

After that, we need to configure the created RabbitMQ user in the Magento app/etc/env.php file. Remove the already existing queue entry from env.php and add the below queue configuration.

'queue' => [
'amqp' => [
  'host' => '127.0.0.1',
  'port' => '5672',
  'user' => 'username',
  'password' => 'password',
  'virtualhost' => '/'
 ],
] 


Construct Bulk API URL (Routes)


It is simple to covert normal API URL to bulk API URL, we need to add async/bulk to before /V1 and rename the input parameter :sku to bySku.


Normal REST API URL:

http://127.0.0.1/m234/rest/all/V1/products/:sku


Bulk REST API URL :


http://127.0.0.1/m234/rest/all/async/bulk/V1/products/bySku


Update products by using PUT method

PUT http://127.0.0.1/m234/rest/all/async/bulk/V1/products/bySku

Request data

[
{
  "product": {
    "sku": "sku1",
    "name": "Product1",
    "attribute_set_id": 4,
    "status": 1,
    "visibility": 4,
    "price":10,
    "type_id": "simple",
    "extension_attributes": {
      "stock_item": {
      "item_id": 3072,
      "product_id": 3073,
      "stock_id": 1,
      "qty": 20,
      "is_in_stock": true
      }
    },
    "custom_attributes": [
      {
        "attribute_code": "part_number",
        "value": "1234"
      }
    ]    
  }
},
{
  "product": {
    "sku": "3483",
    "name": "Product2",
    "attribute_set_id": 4,
    "status": 1,
    "visibility": 4,
    "price":20,
    "type_id": "simple",
    "extension_attributes": {
      "stock_item": {
        "item_id": 3071,
        "product_id": 3072,
        "stock_id": 1,
        "qty": 10,
        "is_in_stock": true
      }
    },
    "custom_attributes": [
      {
        "attribute_code": "part_number",
        "value": "1234"
      }
    ]
  }
}
] 

Response data

{
    "bulk_uuid": "eda7899f-d00b-4d0a-b6f9-77fea3296f4d",
    "request_items": [
        {
            "id": 0,
            "data_hash": "986765adeb0b00b03997159b84b627f5c5eda5e281b52dc34d61446cc496ebf3",
            "status": "accepted"
        },
        {
            "id": 1,
            "data_hash": "3ae299b09bd30686772ee24214b482d88776f49f68ce804b1e78dfc4c1d19802",
            "status": "accepted"
        }
    ],
    "errors": false
} 

If cron configured, it will automatically trigger otherwise you need to run the below command manually to trigger the queue

php bin/magento queue:consumers:start async.operations.all 

We can check this queue status in Magento admin -> System -> Bulk Action Log with the above response bulk_uuid (eda7899f-d00b-4d0a-b6f9-77fea3296f4d)

I hope it helps, if you have any questions please let me know in the comment section.

351 views

Recent Posts

See All

Follow Me

  • LinkedIn
  • Twitter
  • Facebook
  • YouTube
  • Instagram

©2020 by Bilal Usean. Proudly created with Wix.com