Creating a new customer Rest API

SDock
Tourist
6 0 2

I am trying to create a new customer and continue to get 'Bad Request' error.
I don't know if I am missing required fields as I cannot find that information.

This is my JSON string: 
"{\"customer\":{\"first_name\":\"Maria\",\"last_name\":\"Dollinger\",\"email\":\"kzzs28cg3lzwpzw@marketplace.amazon.com\",\"phone\":\"+17632259462\",\"verified_email\":true,\"addresses\":{\"address1\":\"710 Lindbergh Ave\",\"city\":\"Stevens Point\",\"province\":\"WI\",\"phone\":\"+17632259462\",\"zip\":\"54481\",\"last_name\":\"Dollinger\",\"first_name\":\"Maria\",\"country\":\"US\"}}}"

JSON view:

SDock_0-1632765484164.png

Here is my URL:
https://apiKey:apiPassword@frezzornewzealand.myshopify.com/admin/customers.json

 

This is my POST:
public string Post(string resource, string json)
{
string url = BuildResourceUrl(resource);
return _wc.UploadString(url, "POST", json);
}

private string BuildResourceUrl(string query)
{
const string urlFormat = "https://{0}:{1}@{2}/admin/{3}";
return string.Format(urlFormat, _apiKey, _apiPassword, _hostName, query);
}

Could somebody please give me a hand with this

Replies 7 (7)

Keyurp
Excursionist
27 0 4

Hello @SDock 

Please refer to below step for creating a customer. I have used by my credential and now customer is creating properly.

I suggest please try first on postman and then try to implement via coding. You will get 100% result.

Host URL
https://apiKey:apiPassword@frezzornewzealand.myshopify.com/admin/api/2021-07/customers.json

Headers:
Content-Type: application/json
Accept: application/json
X-Shopify-Access-Token: apiPassword

Use Body request:

{
"customer":{
"first_name":"Maria",
"last_name":"Dollinger",
"email":"kzzs28cg3lzwpzw@marketplace.amazon.com",
"phone":"+17632259462",
"verified_email":true,
"addresses":[{
"address1":"710 Lindbergh Ave",
"city":"Stevens Point",
"province":"WI",
"phone":"+17632259462",
"zip":"54481",
"last_name":"Dollinger",
"first_name":"Maria",
"country":"US"
}]
}
}


SDock
Tourist
6 0 2

Thank you for your reply. I am not sure I understand 'first try on postman'. I am not sure what that means.
Were you able to create a customer using my sample code and json?
If so, then I must have something structured wrong somewhere else.

This is my complete code to create the json and call the Post api method

public void AddAmazonCustomer(string FirstName, string LastName, string Email, string Phone,
string Address1, string City, string State, string Zip, string Country)
{
var address = new Dictionary<string, object>();
address["address1"] = Address1;
address["city"] = City;
address["province"] = State;
address["phone"] = Phone;
address["zip"] = Zip;
address["last_name"] = LastName;
address["first_name"] = FirstName;
address["country"] = Country;

var customer = new Dictionary<string, object>();
customer["first_name"] = FirstName;
customer["last_name"] = LastName;
customer["email"] = Email;
customer["phone"] = Phone;
customer["verified_email"] = true;
customer["addresses"] = address;
var request = new Dictionary<string, object>();

request["customer"] = customer;
string json = _serializer.Serialize(request);
var response = _api.Post(string.Format("customers.json"), json);}

public string Post(string resource, string json)
{
string url = BuildResourceUrl(resource);
return _wc.UploadString(url, "POST", json);       -- this is where I am getting my error ('Bad Request')
}

private string BuildResourceUrl(string query)
{
const string urlFormat = "https://{0}:{1}@{2}/admin/{3}";
return string.Format(urlFormat, _apiKey, _apiPassword, _hostName, query);
}

 

My Get works fine and I am able to PUT (Update) an order, I am just having trouble with Customer object and don't see what I am missing.

Thanks for your help on this.

Keyurp
Excursionist
27 0 4

Hello @SDock 


{
"customer":{
"first_name":"Maria",
"last_name":"Dollinger",
"email":"kzzs28cg3lzwpzw@marketplace.amazon.com",
"phone":"+17632259462",
"verified_email":true,
"addresses":[{
"address1":"710 Lindbergh Ave",
"city":"Stevens Point",
"province":"WI",
"phone":"+17632259462",
"zip":"54481",
"last_name":"Dollinger",
"first_name":"Maria",
"country":"US"
}]
}
}

Please see above the request body. In the request body, there are multiple addresses of a single customer. So you will have to pass the address object as multiple.


Here is the address object. please refer to the below address object.

"addresses":[{
"address1":"710 Lindbergh Ave",
"city":"Stevens Point",
"province":"WI",
"phone":"+17632259462",
"zip":"54481",
"last_name":"Dollinger",
"first_name":"Maria",
"country":"US"
}]

Thanks & Regards
Keyur Prajapati

SDock
Tourist
6 0 2

Keyurp

I am sorry, but I do not see any difference between what I sent and what you are recommending. If there is a difference could you please bold your changes so that I can see them. 

SouthtownAaron
Tourist
6 1 1

Hi,

Aaron from Southtown Commerce here.

What Keyurp is saying is address is actually an array of address objects. You currently have address set up as just an object, not an array of objects.

Look closely at the addresss section in the JSON of what Keyurp posted and what you posted. You will see that in Keyurp's it is actually an array[] of JSON address objects.

You need to change this line to be a List then add your address object to that List:

customer["addresses"] = address;

 

SDock
Tourist
6 0 2

Southtown Aaron

Thank you for pointing that out....I have now changed my code and this is what my JSON looks like:

{"customer":{"first_name":"Maria","last_name":"Dollinger","email":"kzzs28cg3lzwpzw@marketplace.amazon.com","phone":"+17632259462","verified_email":true,"addresses":[{"address1":"710 Lindbergh Ave","city":"Stevens Point","province":"WI","phone":"+17632259462","zip":"54481","last_name":"Dollinger","first_name":"Maria","country":"US"}]}}

This is how I made my correction:
customer["addresses"] = new List<Dictionary<string, object>> { address };

This is my url:
"https://APIKey:APIPassword@frezzornewzealand.myshopify.com/admin/customers.json"

This all seems correct, however I am still getting the same 'Bad Request' error. My credentials are correct as I am able to retrieve data using GET methods, so I am at a loss and not sure where to turn. I get the same error trying to update (PUT) a customer note.

Are the following methods correct for a POST using  the following as my call:
var response = _api.Post(string.Format("customers.json"), json);

public string Post(string resource, string json)
{
string url = BuildResourceUrl(resource);      // this hits the following method and works fine
return _wc.UploadString(url, "POST", json);   **** is this line correct ****
}

private string BuildResourceUrl(string query)
{
const string urlFormat = "https://{0}:{1}@{2}/admin/{3}";
return string.Format(urlFormat, _apiKey, _apiPassword, _hostName, query);
}

Keyurp
Excursionist
27 0 4

Hello @SDock 

Thanks for the reply but you have not seen my previous thread. I have mentioned that in Shopify REST API you will have to pass the API URL as below here. In this API is compulsory to pass the API version and in the headers pass 'X-Shopify-Access-Token' otherwise you will not getting a proper response. I request to you first check on postman for a chrome extension(Ref:- https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en). then continue on the system that you have used.

Host URL
https://apiKey:apiPassword@frezzornewzealand.myshopify.com/admin/api/2021-07/customers.json

Headers:
Content-Type: application/json
Accept: application/json
X-Shopify-Access-Token: apiPassword

Request body to pass below data:

{"customer":{"first_name":"Maria","last_name":"Dollinger","email":"kzzs28cg3lzwpzw@marketplace.amazon.com","phone":"+17632259462","verified_email":true,"addresses":[{"address1":"710 Lindbergh Ave","city":"Stevens Point","province":"WI","phone":"+17632259462","zip":"54481","last_name":"Dollinger","first_name":"Maria","country":"US"}]}}