2

I want to create customer using REST API in magento2. I am following this swagger link , but only method is given in this. How to pass parameters of customer in REST API?

Help me in this.

  • Are you want to create simple customer using API? – Rakesh Jesadiya Jan 12 '17 at 10:39
11

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" => "user@example.com",
            "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" => "user@example.com",
        "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

  • How can I post an profile image ?.. please check my question magento.stackexchange.com/questions/186036/… – Jsparo30 Jul 23 '17 at 22:20
  • 1
    Life saving code.I was breaking my head against this for two days. Thanks, _/_ _/_ _/_ _/_ _/_ _/_ – Alaksandar Jesus Gene Dec 14 '17 at 11:43
  • with addresses? – saiid Jul 27 '18 at 3:25
  • Is it possible to create the customer without a password? And either have the password auto generated or email the customer to create their password – DWils Aug 23 '18 at 16:58
1

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" => "abc@xyz.com",
        "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>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy