422 - Unprocessable Entity for invoices

We have been calling the invoices endpoint for many months without issue. But last night we received the following error:

status: 422,
statusText: ‘Unprocessable Entity’,
headers: {
server: ‘nginx’,
date: ‘Wed, 01 Sep 2021 02:00:01 GMT’,
‘content-type’: ‘application/json; charset=utf-8’,
location: ‘https://api.freeagent.com/v2/invoices’,
‘x-request-id’: ‘70b2851fde96493f47625d863e9c7a61’,
‘x-protected-by’: ‘Sqreen’,

We have tried combinations of decimal and quoted parameters in tests via postman this morning and they worked fine. But we need to know why our production failed last night.

Any pointers extremely welcome.

Hello @ben007!

Thanks for getting in touch, we’re currently investigating and we’ll let you know once we know more.

All the best,

Hi @ben007,

We’ve had a look at the calls being made to that endpoint from your company, and we noticed that the ones that failed with a 422 very closely follow successful requests (201s), so much so that the initial request doesn’t completely finish generating a new invoice by the time the new one starts. We think that this has led to a reference clash between the two invoices, preventing the latter one from being created as two invoices can’t use the same reference number.
We’ll have a look on our end to see if there’s anything we can do to make the process more efficient, but in the meantime, we think there might be a couple of workarounds:

  1. Providing an invoice reference number with your parameters rather than relying on the generation on our end should should prevent the issue.
  2. Otherwise, if you’re submitting these requests in parallel, it’s possible that submitting them sequentially might solve the problem going forward.
    I hope this helps; Please let me know if this doesn’t solve the issue or if you have any further questions!

All the best,

Many thanks Claudiu

We came to that conclusion and have since throttled the api calls which seems to have worked - and is consistent with your suggestion. Thanks again!