I want to add a product using the REST API in Magento 2.
How can I achieve this?
- 
 Possible duplicate of magento.stackexchange.com/questions/103438/…Prashant Valanda– Prashant Valanda2016年06月13日 07:33:43 +00:00Commented Jun 13, 2016 at 7:33
- 
 2Thanks @PrashantValanda for your reference, I have gone through that but there I didn't get the steps to add products. Later I found and posted the answer here.Manish– Manish2016年06月13日 07:57:53 +00:00Commented Jun 13, 2016 at 7:57
4 Answers 4
Example script that create downloadable product
<?php
$curl = curl_init();
$URL = "http://magento.dev";
curl_setopt_array($curl, array(
 CURLOPT_URL => $URL . "/rest/V1/integration/admin/token",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_POSTFIELDS => "{\"username\":\"admin\", \"password\":\"123123q\"}",
 CURLOPT_HTTPHEADER => array(
 "accept: application/json",
 "cache-control: no-cache",
 "content-type: application/json",
 "postman-token: 654c3084-0e0a-b3a1-043f-0960e695e520"
 ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
 echo "cURL Error #:" . $err;
 die();
} else {
 $key = $response;
}
$data = [
 "product"=> [
 "sku"=> "DownloadableProduct_18sdsd5",
 "name"=> "DownloadableProduct_185",
 "attribute_set_id"=> 4,
 "price"=> "1",
 "status"=> 1,
 "visibility"=> 4,
 "type_id"=> "downloadable",
 "extension_attributes"=> [
 "stock_item"=> [
 "manage_stock"=> 1,
 "is_in_stock"=> 1,
 "qty"=> "10"
 ],
 "downloadable_product_samples"=> [[
 "title"=> "sample1185869143",
 "sort_order"=> "0",
 "sample_type"=> "url",
 "sample_url"=> "http://example.com"
 ]],
 "downloadable_product_links"=> [[
 "title"=> "link-1-185862143",
 "sort_order"=> "1",
 "is_shareable"=> 0,
 "price"=> 2.43,
 "number_of_downloads"=> "2",
 "link_type"=> "url",
 "link_url"=> "http://example.com",
 "sample_type"=> "url",
 "sample_url"=> "http://example.com"
 ]]
 ],
 "custom_attributes"=> [[
 "attribute_code"=> "tax_class_id",
 "value"=> 2
 ], [
 "attribute_code"=> "quantity_and_stock_status",
 "value"=> [
 "qty"=> "10",
 "is_in_stock"=> 1
 ]
 ], [
 "attribute_code"=> "is_virtual",
 "value"=> 1
 ], [
 "attribute_code"=> "url_key",
 "value"=> "downloadableproduct-185892143"
 ], [
 "attribute_code"=> "links_title",
 "value"=> "Links title 185862143"
 ], [
 "attribute_code"=> "links_purchased_separately",
 "value"=> 1
 ], [
 "attribute_code"=> "samples_title",
 "value"=> "Samples185692143"
 ], [
 "attribute_code"=> "links_exist",
 "value"=> 1
 ]]
 ]
];
$curl = curl_init();
curl_setopt_array($curl, array(
 CURLOPT_URL => $URL . "/rest/admin/V1/products/",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_POSTFIELDS => $data,
 CURLOPT_HTTPHEADER => array(
 "accept: application/json",
 "content-type: application/json",
 "authorization: Bearer " . $key,
 ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
 echo "cURL Error #:" . $err;
} else {
 echo $response;
}
- 
 How to set product images?Rakesh Jesadiya– Rakesh Jesadiya2017年01月02日 12:39:31 +00:00Commented Jan 2, 2017 at 12:39
- 
 @RakeshJesadiya Do you know how to set images? Please guide me. Thank you.David Duong– David Duong2019年08月24日 12:19:58 +00:00Commented Aug 24, 2019 at 12:19
Is this a good approach or have any other better solution please suggest me.
Currently, I am doing this :
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
 }
}
- 
 how to add multiple products?prasad maganti– prasad maganti2017年02月02日 07:07:12 +00:00Commented Feb 2, 2017 at 7:07
- 
 Let me check and will let you know shortly...Manish– Manish2017年02月03日 05:13:20 +00:00Commented Feb 3, 2017 at 5:13
- 
 @MagePsycho I wrote my own endpoint to add multiple products by creating a custom extension. Sorry I can't paste complete code here.Manish– Manish2017年11月16日 08:44:56 +00:00Commented Nov 16, 2017 at 8:44
Example script that create downloadable product
<?php
$curl = curl_init();
$URL = "http://magento.dev";
curl_setopt_array($curl, array(
 CURLOPT_URL => $URL . "/rest/V1/integration/admin/token",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_POSTFIELDS => json_encode(["username"=>"admin", "password"=>"123123q"]),
 CURLOPT_HTTPHEADER => array(
 "accept: application/json",
 "cache-control: no-cache",
 "content-type: application/json",
 "postman-token: 654c3084-0e0a-b3a1-043f-0960e695e520"
 ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
 echo "cURL Error #:" . $err;
 die();
} else {
 $key = $response;
}
$data = [
 "product"=> [
 "sku"=> "DownloadableProduct_18sdsd5",
 "name"=> "DownloadableProduct_185",
 "attribute_set_id"=> 4,
 "price"=> "1",
 "status"=> 1,
 "visibility"=> 4,
 "type_id"=> "downloadable",
 "extension_attributes"=> [
 "stock_item"=> [
 "manage_stock"=> 1,
 "is_in_stock"=> 1,
 "qty"=> "10"
 ],
 "downloadable_product_samples"=> [[
 "title"=> "sample1185869143",
 "sort_order"=> "0",
 "sample_type"=> "url",
 "sample_url"=> "http://example.com"
 ]],
 "downloadable_product_links"=> [[
 "title"=> "link-1-185862143",
 "sort_order"=> "1",
 "is_shareable"=> 0,
 "price"=> 2.43,
 "number_of_downloads"=> "2",
 "link_type"=> "url",
 "link_url"=> "http://example.com",
 "sample_type"=> "url",
 "sample_url"=> "http://example.com"
 ]]
 ],
 "custom_attributes"=> [[
 "attribute_code"=> "tax_class_id",
 "value"=> 2
 ], [
 "attribute_code"=> "quantity_and_stock_status",
 "value"=> [
 "qty"=> "10",
 "is_in_stock"=> 1
 ]
 ], [
 "attribute_code"=> "is_virtual",
 "value"=> 1
 ], [
 "attribute_code"=> "url_key",
 "value"=> "downloadableproduct-185892143"
 ], [
 "attribute_code"=> "links_title",
 "value"=> "Links title 185862143"
 ], [
 "attribute_code"=> "links_purchased_separately",
 "value"=> 1
 ], [
 "attribute_code"=> "samples_title",
 "value"=> "Samples185692143"
 ], [
 "attribute_code"=> "links_exist",
 "value"=> 1
 ]]
 ]
];
$curl = curl_init();
curl_setopt_array($curl, array(
 CURLOPT_URL => $URL . "/rest/admin/V1/products/",
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_ENCODING => "",
 CURLOPT_MAXREDIRS => 10,
 CURLOPT_TIMEOUT => 30,
 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
 CURLOPT_CUSTOMREQUEST => "POST",
 CURLOPT_POSTFIELDS => json_encode($data),
 CURLOPT_HTTPHEADER => array(
 "accept: application/json",
 "content-type: application/json",
 "authorization: Bearer " . $key,
 ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
 echo "cURL Error #:" . $err;
} else {
 echo $response;
}
- 
 how to add multiple products?prasad maganti– prasad maganti2017年02月02日 07:11:48 +00:00Commented Feb 2, 2017 at 7:11
- 
 1Magento does not have batch update api. You can implement it by self or run code in loop.KAndy– KAndy2017年02月02日 16:09:39 +00:00Commented Feb 2, 2017 at 16:09
In order to create product in Magento 2. I assume that you use POSTMAN.
Use this endpoint to generate the token :
http://mydomain.localhost/rest/V1/integration/admin/token
Then when you have your token, you are ready to use this endpoint :
http://mydomain.localhost/V1/products
Here is how a payload looks like :
{
 "product": {
 "sku": "test",
 "name": "test",
 "attribute_set_id": 4, //chose the attribute set wanted
 "price": 50,
 "status": 1,
 "visibility": 1,
 "type_id": "simple",
 "weight": "0.5",
 "extension_attributes": {
 "category_links": [],
 "stock_item": {
 "qty": "1000",
 "is_in_stock": true,
 "use_config_max_sale_qty": true
 }
 },
 "custom_attributes": [
 {
 "attribute_code": "tax_class_id",
 "value": "2"
 }
 ]
 }
}
Now, If you want to add multiple products, you should use this endpoint :
http://mydomain.localhost/rest/async/bulk/V1/products
Here is the payload :
[
 {
 "product": {
 "sku": "product-a",
 "name": "product-a",
 "attribute_set_id": 4,
 "price": 3.99,
 "status": 1,
 "visibility": 1,
 "type_id": "simple",
 "weight": "0.5",
 "extension_attributes": {
 "category_links": [],
 "stock_item": {
 "qty": "1000",
 "is_in_stock": true,
 "use_config_max_sale_qty": true
 }
 },
 "custom_attributes": [
 {
 "attribute_code": "tax_class_id",
 "value": "2"
 }
 ]
 }
 },
 {
 "product": {
 "sku": "product-b",
 "name": "product-b",
 "attribute_set_id": 4,
 "price": 3.99,
 "status": 1,
 "visibility": 1,
 "type_id": "simple",
 "weight": "0.5",
 "extension_attributes": {
 "category_links": [],
 "stock_item": {
 "qty": "1000",
 "is_in_stock": true,
 "use_config_max_sale_qty": true
 }
 },
 "custom_attributes": [
 {
 "attribute_code": "tax_class_id",
 "value": "2"
 }
 ]
 }
 }
]
In order to use the bulk import, you would need to run the queue.
bin/magento queue:consumers:start async.operations.all
If you need to do it in code, basically you need to take the right interface, check the data that the interface needs to work and pass the data
Hope it helps.