Error: 422 Client Error: Unprocessable Entity for url

We use a Python script to import (create) invoices. This has worked well for years, but has just started failing with the following error:

Error: 422 Client Error: Unprocessable Entity for url: https://api.freeagent.com/v2/invoices

Have there been changes on the FreeAgent API?

    faInvoice = {
        "invoice": {
            "contact":"https://api.freeagent.com/v2/contacts/" + faContactId,
            "dated_on":invoice["date"],
            "payment_terms_in_days":int(invoice_due_in_days),
            "reference":invoice["invoiceNumber"],
            "client_contact_name":invoice["attention"] if "attention" in invoice else None,
            "payment_terms":invoice["billingTerms"]["name"],
            "comments":"%s" % (faInvoiceComment),
            'send_reminder_emails': True,
            "payment_methods": {
                "gocardless_preauth":True,
                "stripe":True
                },
            "invoice_items": 
                [
                    {
                    "description":invoiceText,
                    "price":invoice["subtotal"],
                    "quantity":1,
                    "sales_tax_rate":20,
                    "category":nominalCode
                    }
                ]
            }
        }

    

    # Post to FA

    print("Posting invoice " + invoice["invoiceNumber"] + " for " + invoice["company"]["name"]+" to FreeAgent...")

    try:
        response = fa.post("invoices",faInvoice)
    except:   
        print("*FreeAgent-InvoiceSync* could not post invoice to FreeAgent.\n```%s```" % (faInvoice))
        raise

Hi there Oliver,

Just to let you know I’ve raised this for the relevant developer team to investigate further and one of us should be able to get back to you with an answer shortly.

Thank you for your patience!

As a follow up, would it be possible for you to share the full body text of the error returned when you make this request?

If you’re worried about sensitive information in the request, you can send it to me as a direct message here instead.

Hi Wil,

Please see below…

Traceback (most recent call last):
File “C:\ … \FreeAgent\freeagent-invoicesync.py”, line 240, in < module >
response = fa.post(“invoices”,faInvoice)
File “C:\ … \FreeAgent\freeagentmodule.py”, line 50, in post
r.raise_for_status()
File “C:\Python27\lib\site-packages\requests\models.py”, line 893, in raise_for_status
raise HTTPError(http_error_msg, response=self)
HTTPError: 422 Client Error: Unprocessable Entity for url: https://api.freeagent.com/v2/invoices

Hi Wil,

Any update? Do you need any more information?

Hi Oliver,

Thank you for your patience, from looking at the payload you used, I note that you’ve both go_cardless_preauth and stripe as true. We’ve recently changed how our invoicing works that only one of these can be true for a given invoice - you can read more here about the new additional GoCardless functionality that has caused us to make this change.

However, you should have received an error in the body of the 422 response that informs you this is the reason for the request failing, I’ll going to flag that to the relevant team to investigate further.

Thanks,

Wil

Hi Wil,

Thanks for that - spot on. I have changed: “stripe”:True to “stripe”:False and the script now works again.

Kind regards,

Oliver

1 Like