4

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.

1
  • Are you want to create simple customer using API? Commented Jan 12, 2017 at 10:39

3 Answers 3

14

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

4
  • How can I post an profile image ?.. please check my question magento.stackexchange.com/questions/186036/…
    – Jsparo30
    Commented Jul 23, 2017 at 22:20
  • 1
    Life saving code.I was breaking my head against this for two days. Thanks, _/_ _/_ _/_ _/_ _/_ _/_ Commented Dec 14, 2017 at 11:43
  • with addresses? Commented Jul 27, 2018 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
    Commented Aug 23, 2018 at 16:58
3

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>';
1

Endpoint: POST /rest/default/V1/customers

Scope: default store view

Final Url: http://www.mag2.loc/rest/default/V1/customers

Headers:Content-Type: application/json

Authorization: Bearer <admin_token>

plz check screen shot :

enter image description here enter image description here

Payload:

{
   "customer" : {
      "lastname" : "Doe",
      "firstname" : "Jane",
      "email" : "[email protected]",
      "addresses" : [
         {
            "defaultBilling" : true,
            "defaultShipping" : true,
            "firstname" : "Jane",
            "lastname" : "Doe",
            "region" : {
               "regionCode" : "NY",
               "regionId" : 43,
               "region" : "New York"
            },
            "countryId" : "US",
            "postcode" : "11501",
            "city" : "Mineola",
            "street" : [
               "160 1st St."
            ],
            "telephone" : "516-555-1111"
         }
      ]
   },
   "password" : "Password1"
}
Response:

The customer id is 2.

{
    "id": 2,
    "group_id": 1,
    "default_billing": "2",
    "default_shipping": "2",
    "created_at": "2020-07-22 23:12:47",
    "updated_at": "2020-07-22 23:13:15",
    "created_in": "Default Store View",
    "email": "[email protected]",
    "firstname": "Jane",
    "lastname": "Doe",
    "store_id": 1,
    "website_id": 1,
    "addresses": [
        {
            "id": 2,
            "customer_id": 2,
            "region": {
                "region_code": "NY",
                "region": "New York",
                "region_id": 43
            },
            "region_id": 43,
            "country_id": "US",
            "street": [
                "160 1st St."
            ],
            "telephone": "516-555-1111",
            "postcode": "11501",
            "city": "Mineola",
            "firstname": "Jane",
            "lastname": "Doe",
            "default_shipping": true,
            "default_billing": true
        }
    ],
    "disable_auto_group_change": 0,
    "extension_attributes": {
        "is_subscribed": false
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.