Customer Create API

ADGMars
New Member
2 0 0

Hello Everyone

I am trying to create customer using API. Create a custom popup with email field and try to create customer using email But API returns an error 


{"errors":{"customer":"Required parameter missing or invalid"}}

Code written in php call API using POST curl .. find my dummy example

$addData = [
"customer" => array(
"first_name" => 'Guest',
"last_name" => 'User',
"email" => 'testedone@gmail.com'  
)
];
$data_json = json_encode($addData);
$url = API_URL . '/admin/api/2021-07/customers.json';
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data_json));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
echo $error_msg; die;
}
echo "<pre>"; print_r($response);die;
curl_close($ch);


Any solution. Please suggest.

Thanks 
0 Likes
awwdam
Shopify Staff
Shopify Staff
92 10 4

Hey @ADGMars,

Took a closer look and noticed the parameters being passed as strings are formatted like this: 'data ', when generally our json format would expect this: "data". Does updating these fields for "first_name", "last_name",  and "email"result in the expected outcome? I also pulled up an example of a PHP cURL request to create a customer:

 

 

php
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://{{api_key}}:{{api_password}}@{{store_name}}.myshopify.com/admin/api/{{api_version}}/customers.json',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
  "customer": {
    "first_name": "First",
    "last_name": "Lastson",
    "email": "steve.lastson@exampletest.com"
    ],
    "send_email_invite": true
  }
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
  ),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

 



If you are still facing this issue after testing the suggestions above, please provide me with an example x-request-id and I would be happy to dig a bit deeper! - Cheers.

 

 

awwdam | API Support @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

0 Likes