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