Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > Getting Started >

Returning Error when contact name already exists

Started by Lewis Tabone -   in Getting Started

Hi everyone,

Im hoping that this query is not too basic, but I have spent hours looking at examples and documentation to no avail.

This is pretty simple, when using the Xero API, if a call is made to create a "Customer" and the unique field of contact name is already in use, the post fails as you would expect, what im trying to do is return the specific error that is related to the failure rather than a generic failure.

I cant tell if my method is so bizarre that i cant call on a specific exception list or if im just not seeing something obvious. What i have so far is as follows:
public string CreateContact(/FromBody/JObject value)
var Value = value.ToObject<Xero.Api.Core.Model.Contact>();
var a = _api.Contacts.CreateAsync(Value).Result;
return a.Id.ToString();
catch (Exception)
var response = "There was an Error.";
return response.ToString();

This successfully returns the GUID when added successfully and returns the error response when an error (one of which may or may not be duplicate contact name) when it errors.

What im trying to do is call the error message from the response from Xero and submit it as a string as the return.

For context, this response is supposed to be given to another program, specifically Zoho CRM.

Im new to C# and the various frameworks in use here so be gentle.

Thanks in advance for your assistance.