How to add line items via API

How would I update an existing invoice with an additional line item?

In order to edit the balance of the invoice, we want to be able to append negative line items to the invoice using a webhook.

Hi,

There’s an example for clients in the docs but invoices is basically the same. You need to pass in all of the fields so we recommend loading the record, changing it and then submitting it.

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

These are ALL of the fields that come back on the get request. Most of them don’t have any values in them. Do I need to explicitly assign every single parameter even if it doesn’t have a value? Or are there certain fields that you have to call out?

Id
data
account_key
is_owner
id
amount
balance
client_id
invoice_status_id
updated_at
archived_at
invoice_number
discount
po_number
invoice_date
due_date
terms
public_notes
private_notes
is_deleted
invoice_type_id
is_recurring
frequency_id
start_date
end_date
last_sent_date
recurring_invoice_id
tax_name1
tax_rate1
tax_name2
tax_rate2
is_amount_discount
false
invoice_footer
partial
partial_due_date
has_tasks
auto_bill
auto_bill_id
custom_value1
custom_value2
custom_taxes1
custom_taxes2
has_expenses
quote_invoice_id
custom_text_value1
custom_text_value2
is_quote
is_public
filename
invoice_design_id
account_key
is_owner
product_key
updated_at
archived_at
notes
qty
tax_name1
tax_rate1
tax_name2
tax_rate2
invoice_item_type_id
custom_value1
custom_value2
discount

If it’s blank you can probably leave it blank but you may want to test it to confirm

Hi there,

I’ve been at this for a few days now and I just can’t figure out what I’m doing wrong. I run the following code and continue to get a 500 error.

This is me just trying to GET an invoice, and then do a PUT right after without changing anything. I just want to get the request to go through but keep getting a 500 response.

import requests

headers = {"Content-Type": "application/json",'X-Ninja-Token':'[TOKEN]',"Accept": "application/json"}

url = 'https://app.invoiceninja.com/api/v1/invoices/11'

r= requests.get(url,headers=headers)
results = r.json()

put_url = 'https://app.invoiceninja.com/api/v1/invoices'
r= requests.put(put_url,data =results, headers=headers)
final_results = r.json
r

I’ve tried so many variations of this, and always come back to the same error. You said that I have to send back all of the fields to the invoice, which I think I’m doing, but still just getting the error.

Is there any way at all that you can help with this?

Is this possible in Python? I’m not very familiar with CURL but it looks like all the docs seem to explain things with that.

It’s a REST API, you can use any language to access it.

Another option is to use the PHP SDK

Hi there,

I’m still having major issues with this topic.

At this point, I can settle for a simple put request to change the invoice number (so that I can delete and recreate a new invoice with same number)

Can you review my code above and tell me what is wrong with it?

Please check you’re sending back just the invoice not the ‘data’ index

r= requests.put(put_url,data =results, headers=headers)

Should probably be:

r= requests.put(put_url,data =results['data'], headers=headers)

@zmccarty56, I’m experiencing the same issue.

I can list (GET) and create (POST) invoices but I cannot update (PUT) invoices.
I’m also using python.

Did you get it working and if so do you have a sample bit of code I could look at?

Thanks,

JJ

@wavesailor so I did a bit of a wonky workaround for it…

Because I couldn’t figure out how to edit the existing invoice, I instead tried just deleting the invoice and creating a new one with the same number. Since you can’t create duplicate invoice numbers just change the number of the old invoice, delete it, and then recreate a new invoice that is updated

url = https://app.invoiceninja.com/api/v1/invoices/[invoice_id]

headers = {}
headers[Content-Type] = ‘application/json’
headers[X-Ninja-Token] = [API Token]

data = {“invoice_number”:“DELETE-[Invoice Number]-[Random Number]”, “invoice_items”:[{“product_key”: “DELETE”,“notes”: “DELETE”,“cost”: 1,“qty”: 1}]}

I know this is likely not the solution you were looking for, but it’s been working for me!

Thanks @zmccarty56 for your solution

I really would like to just update the invoice via the API if I can. The documentation is so thin.

PS. I see you put “delete” numbers in because invoice-ninja only soft deletes the records :wink:

I’m fighting with the same issue with invoice ninja 4, I guess I could make ninja 5 work since I can see the requests in the browser and replicate it.
But I’m stuck with ninja 4 because invoice ninja 5 can not print the page number on the pdf and I’ve wasted 2 hours to try to figure out why the put request does not work.
Even if I get the invoice record, and just send it back as is via PUT request I still get a 500 Error :frowning:

deleting and recreating a new invoice isn’t really a nice solution and I would love to be able to just do a PUT to add a new line item.

@LcX_at @wavesailor I’ve actually figured out how to do this, let me share you.

This is how you need to structure the data

{“client_id”:“25”,“invoice_number”:“5811-Revised”,“invoice_date”:“2022-02-05”, “due_date”:“2022-02-20”,“invoice_items”:[{“notes”: “blah”, “qty”: “1”, “cost”: “26”, “product_key”: “blah”, “custom_value1”: “2022-02-05”}]}

Headers:

X-Ninja-Token: token

Content-Type: application/json

X-Requested-With: XMLHttpRequest

1 Like