Magento 2 : how to create customer using REST API?
Create New Customer
$userData = array("username" => "admin", "password" => "admin123");
$ch = curl_init("http://magento213/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$customerData = [
'customer' => [
"email" => "[email protected]",
"firstname" => "John",
"lastname" => "Doe",
"storeId" => 1,
"websiteId" => 1
],
"password" => "Demo1234"
];
$ch = curl_init("http://magento213/index.php/rest/V1/customers");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo '<pre>';print_r($result);
For Update Customer
$customerData = [
'customer' => [
'id' => 10,
"email" => "[email protected]",
"firstname" => "John2",
"lastname" => "Doe2",
"storeId" => 1,
"websiteId" => 1
],
"password" => "Demo1234"
];
$ch = curl_init("http://magento213/index.php/rest/V1/customers/10");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo '<pre>';print_r($result);
API LIST
Adding more to the @BornCoder answer, if you want to add addresses as well for the new customer, you can find this example helpful. Also setting custom customer attribute woo_customer_id
in the example.
$customerData = [
'customer' => [
"email" => "[email protected]",
"firstname" => "ABC",
"lastname" => "XYZ",
"createdAt" => "4/26/2017 2:31:22 PM",
"storeId" => 1,
"websiteId" => 1,
"groupId" => 1,
"customAttributes" => [
[
"attributeCode" => "woo_customer_id",
"value" => "444445"
]
],
"addresses" => [
[
"firstname" => "ABC",
"lastname" => "XYZ",
"countryId" => "MY",
"street" => [
"No 545 Jalan balau 27/13",
"sinar link Taman rinting"
],
"company" => "Mahnazfood",
"telephone" => "04040404040404",
"fax" => "01010101101010101",
"postcode" => "81750",
"city" => "Masai",
"defaultBilling" => true
],
[
"firstname" => "ABC",
"lastname" => "XYZ",
"countryId" => "MY",
"street" => [
"Colony#3 Block#126/F",
"Address 2 "
],
"company" => "Ashtech",
"telephone" => "090078601",
"fax" => "00000000000000",
"postcode" => "45000",
"city" => "Lahore",
"defaultShipping" => true
]
]
],
"password" => "Demo12345"
];
/*
* Post customer in magento
*/
$ch = curl_init("http://localhost/smagemahnaz/index.php/rest/V1/customers");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/json", "Authorization: Bearer " . json_decode($_COOKIE["magento_token"]))
);
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo '<pre>';print_r($result); echo '</pre>';