Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > API Endpoints >

Error number 14 when creating invoice

Started by Lau Hon -   in API Endpoints

I need some help in creating new invoice in C#.

For your information, I run the following sample code that I found in github and it works (I was able to see the number of outstanding invoices).

var outstandingInvoices = await _accountingApi.GetInvoicesAsync(accessToken,

int i = outstandingInvoices._Invoices.Count;

But, if I tried to create new invoice, the following error message will appear:

"ErrorNumber": 14,
"Type": "PostDataInvalidException",
"Message": "Invalid Json data"

Here is my code that is creating the invoice:

Invoice inv = new Invoice();
inv.Date = DateTime.Now.Date;
inv.DueDate = DateTime.Now.AddDays(100).Date;
inv.Type = Invoice.TypeEnum.ACCREC;
inv.Reference = "Test invoice 001";
inv.Status = Invoice.StatusEnum.DRAFT;
inv.LineAmountTypes = LineAmountTypes.NoTax;

inv.Contact = new Contact()
ContactID = new Guid("ce91dae6-af3f-4f52-84a8-f02bd7d0997d")

inv.LineItems = new List<LineItem>();
LineItem item = new LineItem();

item.AccountCode = "206C";
item.Description = "Sales - Cafe";
item.Quantity = 1;
item.UnitAmount = (double)-0.55;
item.LineAmount = (double)-0.55;

item.TaxType = "NONE";
item.TaxAmount = 0;
Invoices inv_list = await _accountingApi.CreateInvoiceAsync(accessToken, tenantId, inv);

Since the calling CreateInvoiceAsync() will raise an exception, I tried clone one of the outstanding invoice, changed the invoice status to Draft, removed the InvoiceID value and call CreateInvoiceAsync(). The same error "PostDataInvalidException" appear.

The tenant ID is 82e1916e-9540-47d6-a679-7c9c61fe58cd. Let me know if you need any other information.

Any help is much appreciate.

Hi there,

It looks like your body is being serialised as XML, but your request is implying you're sending JSON. You'll need to align your request headers and body to either both be XML or both be JSON for requests to succeed.



Russell Dear (Xero Staff)  

Hi Russell,

Thanks for prompt response and appreciate your help. We are able to create invoice now.

Just to share what we have done - I downloaded Xero.NetStandard.OAuth2 source code from github and migrated it to .Net Framework 4.5.2 and re-add the NuGet packages. Then, we accidentally change one line without knowing that it caused the issue.

The details:

- Class: Xero.NetStandard.OAuth2.Client.ApiClient
- Method : private RestRequest newRequest()
- Isssue: AddBody() is obsolete in RestSharp, Version=

And the hint in RestSharp was misleading and I followed the hint blindly that cause the invalidte Json data... :(

Obsolete("Use AddXmlBody")
public IRestRequest AddBody(object obj);

The correct method to interface with Xero should be AddJsonBody().

Lau Hon