Unable to update invoices through REST API

Hello everyone,
Our company is using the hosted of Invoice Ninja.
The normal workflow of our company is: whenever a client made a purchase on our website, we send the data to a Zapier hook, and we use this data to create an invoice (with the Zapier - Invoice Ninja integration). The problem here is, sometimes, us developers or people in the marketing team have the need to make some “fake purchases” (locally or on prod) in order to test the process, and therefore a new real invoice will be generated, it created lots of trouble for people who worked in administration, since they will have to manually change the paid amount of all these “test invoice” to 0.
Lately we came up with the idea of creating a script based on the RESTful API of Invoice Ninja, so whenever we detected an invoice is generated through “fake purchases”, we’d retrieve the id of this invoice and use the PUT method to update its related data accordingly.
So far I have no problems of retrieving the data of a specific invoice with the url https://app.invoiceninja.com/api/v1/invoices/{id}
But whenever I tried to update the invoice, the only response I got back is a 500 Internal Server Error

my code:

const response = await fetch(
${NINJA_API_ROOT_URL}/invoices/${invoiceId},
{
method: ‘PUT’,
headers: {
‘X-Ninja-Token’: process.env.NINJA_TOKEN,
‘X-Requested-With’: ‘XMLHttpRequest’,
Accept: ‘application/json’,
‘Content-Type’: ‘application/json’,
},
body: JSON.stringify({
private_notes: ‘test’,
amount: 0,
balance: 0,
discount: 0,
}),
},
);

So here I would like to ask,

  1. is it possible to update an invoice with a hosted API? or we will have to have a self-hosted in order to handle the problem I described above?
  2. If it wouldn’t work with the hosted API, is there any other way that we could solve the “testing invoices” problems?

Thanks for reading this post, any kind of advices would be appreciated.

Hi,

The API supports updating the invoice but you need to set the line items array, you can’t set the amount directly.

Thank you for your answer! is there more detailed documentation that I could find on how to set the line items array?
this link Swagger UI seems stopped working
and this one API — Invoice Ninja 4.5.21 documentation didn’t mention anything about it :frowning:

There’s example here of creating an invoice with line items:

https://docs.invoiceninja.com/api.html#creating-data