Magento – Magento 2 : Customer creation failed using customer API

customermagento2.2.3rest api

I am trying to create customer using API in magento 2.2.3.

Below api is working well and customer can created successfully but issue only arrive while adding address.

If i send address field with customer magento show me error like below.

Here is the data i am sending to api

Array
(
    [customer] => Array
        (
            [website_id] => 1
            [store_id] => 1
            [group_id] => 1
            [firstname] => name
            [lastname] => lastname
            [custom_attributes] => Array
                (
                    [customer_code] => 3654256542156954
                )

            [email] => example@gmail.com
            [addresses] => Array
                (
                    [region] => 67
                    [country_id] => CA
                    [countryId] => CA
                    [street] => 3652 Rohnstand Road  
                    [firstname] => name
                    [lastname] => lastname
                    [company] => 3654256542156954
                    [telephone] => 999-999-9999
                    [city] => city
                    [postcode] => pincode
                )

        )

    [password] => password
)

This is how api calling.

    $data = $this->mapData($data);
    $ch = curl_init("http://127.0.0.1/project/index.php/rest/V1/customers");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($this->_token)));

    $result = curl_exec($ch);

Response i got from API

Array
(
    [message] => One or more input exceptions have occurred.
    [errors] => Array
        (
            [0] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => firstname
                        )

                )

            [1] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => lastname
                        )

                )

            [2] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => street
                        )

                )

            [3] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => city
                        )

                )

            [4] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => telephone
                        )

                )

            [5] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => postcode
                        )

                )

            [6] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => countryId
                        )

                )

)

[trace] => #0 /var/www/html/project/vendor/magento/module-customer/Model/AccountManagement.php(789): Magento\Customer\Model\ResourceModel\AddressRepository->save(Object(Magento\Customer\Model\Data\Address))
#1 /var/www/html/project/generated/code/Magento/Customer/Model/AccountManagement/Interceptor.php(141): Magento\Customer\Model\AccountManagement->createAccountWithPasswordHash(Object(Magento\Customer\Model\Data\Customer), 'b54a5a70013dd9b...', '')
#2 /var/www/html/project/vendor/magento/module-customer/Model/AccountManagement.php(724): Magento\Customer\Model\AccountManagement\Interceptor->createAccountWithPasswordHash(Object(Magento\Customer\Model\Data\Customer), 'b54a5a70013dd9b...', '')
#3 /var/www/html/project/generated/code/Magento/Customer/Model/AccountManagement/Interceptor.php(128): Magento\Customer\Model\AccountManagement->createAccount(Object(Magento\Customer\Model\Data\Customer), '$000002$', '')
#4 [internal function]: Magento\Customer\Model\AccountManagement\Interceptor->createAccount(Object(Magento\Customer\Model\Data\Customer), '$000002$', '')
#5 /var/www/html/project/vendor/magento/module-webapi/Controller/Rest.php(330): call_user_func_array(Array, Array)
#6 /var/www/html/project/vendor/magento/module-webapi/Controller/Rest.php(239): Magento\Webapi\Controller\Rest->processApiRequest()
#7 /var/www/html/project/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#8 /var/www/html/project/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Webapi\Controller\Rest\Interceptor->___callParent('dispatch', Array)
#9 /var/www/html/project/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Webapi\Controller\Rest\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#10 /var/www/html/project/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(39): Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch', Array, Array)
#11 /var/www/html/project/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#12 /var/www/html/project/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch()
#13 /var/www/html/project/vendor/magento/framework/App/Bootstrap.php(256): Magento\Framework\App\Http\Interceptor->launch()
#14 /var/www/html/project/index.php(40): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#15 {main}
)

Best Answer

Register Customer using API

webapi.xml

<route url="/V1/customers" method="POST">
<service class="Magento\Customer\Api\AccountManagementInterface" method="createAccount"/>
<resources>
    <resource ref="anonymous"/>
</resources>
</route>

url http://<magento-host>/rest/V1/customers

url action :: POST

Body > raw > JSON(application/json)

{
"customer":{
    "email":"Test@example.com",
    "firstname":"Aditya",
    "lastname":"Shah",
    "websiteId":"1",
    "addresses":[
        {
            "customer_id":"1",
            "firstname":"Aditya",
            "lastname":"Shah",
            "company":"ABC Manufacturing",
            "telephone":"555-555-5555",
            "city":"Boston",
            "region":"Massachusetts",
            "postcode":"02115",
            "country_id":"US",
            "street":[
                "404 Main Street",
                "PO Box 321"
            ]
        }
    ]
},
 "password": "Account@123"
}
Related Topic