- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Magento 2 how to add product by rest api?
<? //Authentication rest API magento2.Please change url accordingly your url $adminUrl='http://localhost/magento/index.php/rest/V1/integration/admin/token'; $ch = curl_init(); $data = array("username" => "", "password" => ""); $data_string = json_encode($data); $ch = curl_init($adminUrl); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type=> application/json', 'Content-Length=> ' . strlen($data_string)) ); $token = curl_exec($ch); $token= json_decode($token); //Use above token into header $headers = array('Authorization=> Bearer $token'); $requestUrl='http://127.0.0.1/magento/index.php/rest/V1/products'; //Please note 24-MB01 is sku $ch = curl_init(); $ch = curl_init($requestUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); $post ='{ "product": { "sku": "MY_SKU", "name": "My Product", "attributeSetId": "4", "price": 20, "status": 1, "visibility": 4, "typeId": "virtual", "weight": 0, "extensionAttributes": { "stockItem": { "stockId": 1, "qty": 20, "isInStock": true, "isQtyDecimal": false, "useConfigMinQty": true, "minQty": 0, "useConfigMinSaleQty": 0, "minSaleQty": 0, "useConfigMaxSaleQty": true, "maxSaleQty": 0, "useConfigBackorders": false, "backorders": 0, "useConfigNotifyStockQty": true, "notifyStockQty": 20, "useConfigQtyIncrements": false, "qtyIncrements": 0, "useConfigEnableQtyInc": false, "enableQtyIncrements": false, "useConfigManageStock": true, "manageStock": true, "lowStockDate": "string", "isDecimalDivided": true, "stockStatusChangedAuto": 0, "extensionAttributes": {} } }, "options": [], "tierPrices": [], "customAttributes": [ ] }, "saveOptions": true }'; $options = array( CURLOPT_URL=>$toURL, CURLOPT_HTTPHEADER=>array( 'Content-Type: application/json', 'Content-Length: ' . strlen($post)), CURLOPT_VERBOSE=>0, CURLOPT_RETURNTRANSFER=>true, CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible;)", CURLOPT_POST=>true, CURLOPT_POSTFIELDS=>$post, ); curl_setopt_array($ch, $options); $result = curl_exec($ch); $result= json_decode($result); print_r($result); ?>
I have try above code ,but can not add a product , anyone know how to do?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 how to add product by rest api?
i can't find where is your input for username and password ???
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 how to add product by rest api?
Hello Boajay,
I have the same requirement adding the product using default Magento2 REST API.
Step1. Generate admin token:
I am using token for authorization, so create an admin token using this URL Http://{baseurl}/rest/V1/integration/admin/token
Step2. Add product :
For adding the product, I am using following URL http://magentogit.com/rest/V1/products/{SKU} , this is magento2 default API using put method.
For example:
http://baseurl/rest/V1/products/B201-SKU header: Content-Type - application/json Authorization - Bearer token Body: { "product": { "sku": "B201-SKU", "name": "B202", "price": 30.00, "status": 1, "type_id": "simple", "attribute_set_id":4, "weight": 1 } }
You can add more data for the product after this call chek your product from admin.
You are done.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 how to add product by rest api?
how to add multiple products?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 how to add product by rest api?
You have to make multiple calls. There is not an API that will take an array of product objects.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 how to add product by rest api?
not working for me get this error
( [message] => Consumer is not authorized to access %resources [parameters] => stdClass Object ( [resources] => Magento_Catalog:roducts ) )
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 how to add product by rest api?
https://domain.com/index.php/rest/V1/integration/admin/token
Send Post Request in Raw JSON like this
{
"username":"yourstoreusername",
"password":"yourstorepassword"
}
You will get API Token in Response, Use that API Token in Bearer Header for sending all Rest API Requests.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 how to add product by rest api?
@ipragmatech what is the the functionality behind using "saveOptions" in create product REST API?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Magento 2 how to add product by rest api?
you have an error in this line
$headers = array('Authorization=> Bearer '.$token);