How to add product in Magento using REST API?












5















I want to add a product using the REST API in Magento 2.



How can I achieve this?










share|improve this question

























  • Possible duplicate of magento.stackexchange.com/questions/103438/…

    – Prashant Valanda
    Jun 13 '16 at 7:33






  • 2





    Thanks @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
    Jun 13 '16 at 7:57
















5















I want to add a product using the REST API in Magento 2.



How can I achieve this?










share|improve this question

























  • Possible duplicate of magento.stackexchange.com/questions/103438/…

    – Prashant Valanda
    Jun 13 '16 at 7:33






  • 2





    Thanks @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
    Jun 13 '16 at 7:57














5












5








5


4






I want to add a product using the REST API in Magento 2.



How can I achieve this?










share|improve this question
















I want to add a product using the REST API in Magento 2.



How can I achieve this?







magento2 product api rest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago







Manish

















asked Jun 13 '16 at 6:44









ManishManish

1,91431839




1,91431839













  • Possible duplicate of magento.stackexchange.com/questions/103438/…

    – Prashant Valanda
    Jun 13 '16 at 7:33






  • 2





    Thanks @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
    Jun 13 '16 at 7:57



















  • Possible duplicate of magento.stackexchange.com/questions/103438/…

    – Prashant Valanda
    Jun 13 '16 at 7:33






  • 2





    Thanks @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
    Jun 13 '16 at 7:57

















Possible duplicate of magento.stackexchange.com/questions/103438/…

– Prashant Valanda
Jun 13 '16 at 7:33





Possible duplicate of magento.stackexchange.com/questions/103438/…

– Prashant Valanda
Jun 13 '16 at 7:33




2




2





Thanks @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
Jun 13 '16 at 7:57





Thanks @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
Jun 13 '16 at 7:57










3 Answers
3






active

oldest

votes


















3














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;
}





share|improve this answer
























  • How to set product images?

    – Rakesh Jesadiya
    Jan 2 '17 at 12:39





















2














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
}
}





share|improve this answer
























  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:07











  • Let me check and will let you know shortly...

    – Manish
    Feb 3 '17 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
    Nov 16 '17 at 8:44





















1














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;
}





share|improve this answer
























  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:11






  • 1





    Magento does not have batch update api. You can implement it by self or run code in loop.

    – KAndy
    Feb 2 '17 at 16:09













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f120570%2fhow-to-add-product-in-magento-using-rest-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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;
}





share|improve this answer
























  • How to set product images?

    – Rakesh Jesadiya
    Jan 2 '17 at 12:39


















3














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;
}





share|improve this answer
























  • How to set product images?

    – Rakesh Jesadiya
    Jan 2 '17 at 12:39
















3












3








3







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;
}





share|improve this answer













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;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 13 '16 at 8:02









KAndyKAndy

15.4k23043




15.4k23043













  • How to set product images?

    – Rakesh Jesadiya
    Jan 2 '17 at 12:39





















  • How to set product images?

    – Rakesh Jesadiya
    Jan 2 '17 at 12:39



















How to set product images?

– Rakesh Jesadiya
Jan 2 '17 at 12:39







How to set product images?

– Rakesh Jesadiya
Jan 2 '17 at 12:39















2














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
}
}





share|improve this answer
























  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:07











  • Let me check and will let you know shortly...

    – Manish
    Feb 3 '17 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
    Nov 16 '17 at 8:44


















2














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
}
}





share|improve this answer
























  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:07











  • Let me check and will let you know shortly...

    – Manish
    Feb 3 '17 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
    Nov 16 '17 at 8:44
















2












2








2







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
}
}





share|improve this answer













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
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 13 '16 at 6:47









ManishManish

1,91431839




1,91431839













  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:07











  • Let me check and will let you know shortly...

    – Manish
    Feb 3 '17 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
    Nov 16 '17 at 8:44





















  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:07











  • Let me check and will let you know shortly...

    – Manish
    Feb 3 '17 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
    Nov 16 '17 at 8:44



















how to add multiple products?

– prasad maganti
Feb 2 '17 at 7:07





how to add multiple products?

– prasad maganti
Feb 2 '17 at 7:07













Let me check and will let you know shortly...

– Manish
Feb 3 '17 at 5:13





Let me check and will let you know shortly...

– Manish
Feb 3 '17 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
Nov 16 '17 at 8:44







@MagePsycho I wrote my own endpoint to add multiple products by creating a custom extension. Sorry I can't paste complete code here.

– Manish
Nov 16 '17 at 8:44













1














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;
}





share|improve this answer
























  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:11






  • 1





    Magento does not have batch update api. You can implement it by self or run code in loop.

    – KAndy
    Feb 2 '17 at 16:09


















1














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;
}





share|improve this answer
























  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:11






  • 1





    Magento does not have batch update api. You can implement it by self or run code in loop.

    – KAndy
    Feb 2 '17 at 16:09
















1












1








1







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;
}





share|improve this answer













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;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 13 '16 at 8:05









KAndyKAndy

15.4k23043




15.4k23043













  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:11






  • 1





    Magento does not have batch update api. You can implement it by self or run code in loop.

    – KAndy
    Feb 2 '17 at 16:09





















  • how to add multiple products?

    – prasad maganti
    Feb 2 '17 at 7:11






  • 1





    Magento does not have batch update api. You can implement it by self or run code in loop.

    – KAndy
    Feb 2 '17 at 16:09



















how to add multiple products?

– prasad maganti
Feb 2 '17 at 7:11





how to add multiple products?

– prasad maganti
Feb 2 '17 at 7:11




1




1





Magento does not have batch update api. You can implement it by self or run code in loop.

– KAndy
Feb 2 '17 at 16:09







Magento does not have batch update api. You can implement it by self or run code in loop.

– KAndy
Feb 2 '17 at 16:09




















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f120570%2fhow-to-add-product-in-magento-using-rest-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1300-talet

1300-talet

Has there ever been an instance of an active nuclear power plant within or near a war zone?