How to Create & Update Product Using REST API in Magento 2
End-to-end Magento integration
Integrate any app or service. Consult our API development experts.
Are you following the ERP model of business for Magento 2 store?
Ever had the requirement to manage the changes in ERP to directly reflect in the Magento 2 online store?
If yes, you can consider this post as the starting point for you where I am giving the solution to create & update product using API in Magento 2.
You can also refer to the official document for starters,
“A software interface that lets third-party applications read and write to a system using programming language constructs or statements.”
Magento supports and provides REST and SOAP.
Magento uses Swagger to display REST APIs for all installed products and allows you to try out the APIs.
The Magento 2 REST API identifies various functions to perform requests and receive responses. One can perform these interactions by using the HTTP protocol.
Steps to create & update product using API in Magento 2:
Method to create product using API in Magento 2:
First, you need to generate an authentication key and follow the below steps for the same:
- Navigate to System > Extensions > Integration
- Click Add New Integration button
- Enter your Name and admin Password
- Click save and active button
- Click Allow. Now the Authentication key is generated
New API:
New Auth:
In order to create and update product in Magento 2, you may require to generate admin access token first. Read here how to get admin token in Magento 2 API.
Open Postman and enter URL to create the product:
- <YourbaseURL>/rest//V1/products eg. http://127.0.0.1/magento/rest/V1/products
- Select Method Post
- Header part :Authorization: Bearer <Hear Enter generated Authentication key>
Content-Type: application/json - Enter payload data into body section
1234567891011121314151617181920212223242526272829303132333435363738{"product": {"sku": "samsung-a50","name": "Samsung A50 ","attribute_set_id": 4,"price": 150000,"status": 1,"visibility": 1,"type_id": "Default","weight": "1","extension_attributes": {"category_links": [{"position": 0,"category_id": "6"}],"stock_item": {"qty": "10","is_in_stock": true}},"custom_attributes": [{"attribute_code": "pattern","value": "1960"},{"attribute_code": "color","value": "45"},{"attribute_code": "size","value": "168"}]}}
Related reads:
- How to Get Total Segment Using Magento 2 Rest API
- How to Get Value of Custom Attribute on Magento 2 Rest API
- How to Get Magento 2 API Keys?
Method to Update Product Using API in Magento 2:
URL: <BaseURL>/rest/V1/products/{SKU} eg. http://127.0.0.1/magento/rest/V1/products/samsung-a50
Select Method : PUT
Body
1 2 3 4 5 6 7 8 9 10 11 |
{ "product": { "price": 20000, "custom_attributes": [ { "attribute_code": "color", "value": "47" } ] } } |
First, you need to open swagger like <YourBaseURL>/Swagger/# or open list of API https://devdocs.magento.com/swagger/#/
That’s it.
Any doubts about the topic? If so, mention them in the Comments section below. I’ll help you out.
It would be great if you could share the post with Magento 2 developers via social media.
Thank you.
Integrate any app/service with Magento 2 store. Consult our API experts.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
16 Comments
By using this rest/V1/products endpoint, will it create wrong scope entries in the database?
Hey,
No it will effect on default store view,
If you want to update product in multi website than you need to pass website_id in body part.
Hi, I am trying to create, update products via REST API. But getting error in assigning categories. Can you please help. I can share the code on email. Any help would be much appreciated. Thanks
Yes, please do share the code.
Hi, I am unable to share code in comments. It block my comments for security reasons. Can you please share your email address to share the code. Thanks
Here is our Mail ID [email protected]
Hi, I am trying to import products using REST API from 1 store to another Magento 2.4 store. In the below code have issue to assign category to the product.
$categories = $item[‘extension_attributes’][‘category_links’];
$catArray = array();
foreach($categories as $category){
$category_id = $category[‘category_id’];
foreach($localCategories as $lcat){
if($category_id == $lcat[‘baseId’]){
array_push($catArray, $lcat[‘id’]);
}
}
}
$product[‘categories’] = $catArray;
The above code leaves the category blank. If I remove foreach($categories as $category), product is assigned to all the categories.
$category = $item[‘extension_attributes’][‘category_links’];
$catArray = array();
$category_id = $category[‘category_id’];
foreach($localCategories as $lcat){
if($category_id == $lcat[‘baseId’]){
array_push($catArray, $lcat[‘id’]);
}
}
$product[‘category’] = $catArray;
Any help would be much appreciated. Thanks
Hey Preet,
Can you please share the error,
we will check and provide the solution according to that.
Hello Sanjay,
First of all thank you for the helpfull article.
There is 1 problem I’m facing and that is that the stock_item qty is never stored. It always is set to 0, even when I use your exact sample code. I have no idea what is going on, no error, everything is stored, but the return value shows 0 in the qty field and also the database stores 0.
Have you ever run into this problem? Any idea what is causing this?
Hello Bas,
This issue seems new as we have not faced such an issue.
You can try using the rest API which is available for product stock
URL: /V1/products/{ProductSku}/stockItems/{ItemId}
Body (josn type) :
{
“stock_item”: {
“qty”: 100,
“is_in_stock”: true
}
}
Thank You
Hi! You ‘ve helped me a lot ! but now I’m facing an Issue and I have no idea about how to solve it.
I need to create a grouped Product with simple Products via Rest API, I already created empty grouped products, but now I’m not getting succeded in creating and adding simple products to the grouped one
Hello Rodrigo,
The below solution might be helpful for you in this case:
https://devdocs.magento.com/guides/v2.4/rest/tutorials/grouped-product/create-and-manage-grouped-products.html
Thank You
Hello, I am calling the update API to update the status to enable or disable the product, but after updating the status to 2, the page can still see the product
Hello,
Set enable= 1 or disable = 0 and flush/clean the cache
Thank You.
Hello I am facing this error code following this procedure, and follow https://community.magento.com/t5/Magento-2-x-Technical-Issues/REST-API-The-consumer-isn-t-authorized-to-access-resources-2-3-1/td-p/133545 but unable to find solution, could you help me
{
“message”: “The consumer isn’t authorized to access %resources.”,
“parameters”: {
“resources”: “Magento_Catalog::products”
},
Hello Mukesh,
You will have to pass the admin’s token barrier as a token for this issue.
Thank you.