Changing Invoice Number API

Hello there,

I am trying to update an invoice number through a simple webhook request using Python.

Here is my code…

import requests

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

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

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

results['data']['invoice_number'] ='0017-DELETED'

r= requests.put(url,data =results, headers=headers)
final_results =r.json
r.status_code

I’m really struggling to understand what I’m doing wrong considering that all I’m doing is running a get request, changing on value, and then trying to send it right back.

I am continuously getting a 500 response error. Please help!

Copy/paste…

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)

Note: please try to just post to one place, makes my life easier…

First and foremost let me just say that I appreciate all the support you are giving me. I apologize for posting in multiple places and will be sure to keep things concise moving forward.

Here is the updated code I have, still getting an Internal Service Error.

import requests

headers = {"Content-Type": "application/json",'X-Ninja-Token':'siyri5z1bvpbwfcogyrzrwbhcixcctbf'}

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

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

results['data']['invoice_number'] ='0017-DELETED'

put_data =results['data']

put_data

r= requests.put(url,data=put_data, headers=headers)
final_results =r.json
r.status_code

Am I sending back too many fields? Here is everything within put_data

{'account_key': 'b8wbv85qkonicc8euotfvflisfv9klhx',
 'is_owner': True,
 'id': 17,
 'amount': 0.51,
 'balance': 0,
 'client_id': 89,
 'invoice_status_id': 6,
 'updated_at': 1604293497,
 'archived_at': None,
 'invoice_number': '0017-DELETED',
 'discount': 0,
 'po_number': '',
 'invoice_date': '2020-11-02',
 'due_date': '',
 'terms': '',
 'public_notes': '',
 'private_notes': '',
 'is_deleted': False,
 'invoice_type_id': 1,
 'is_recurring': False,
 'frequency_id': 0,
 'start_date': '',
 'end_date': '',
 'last_sent_date': '2020-11-02',
 'recurring_invoice_id': 0,
 'tax_name1': '',
 'tax_rate1': 0,
 'tax_name2': '',
 'tax_rate2': 0,
 'is_amount_discount': False,
 'invoice_footer': '',
 'partial': 0,
 'partial_due_date': '',
 'has_tasks': False,
 'auto_bill': False,
 'auto_bill_id': 0,
 'custom_value1': 0,
 'custom_value2': 0,
 'custom_taxes1': False,
 'custom_taxes2': False,
 'has_expenses': False,
 'quote_invoice_id': 0,
 'custom_text_value1': '',
 'custom_text_value2': '',
 'is_quote': False,
 'is_public': True,
 'filename': 'Invoice_0017.pdf',
 'invoice_design_id': 11,
 'invoice_items': [{'account_key': 'b8wbv85qkonicc8euotfvflisfv9klhx',
   'is_owner': True,
   'id': 254,
   'product_key': 'L2 Installer',
   'updated_at': 1604293477,
   'archived_at': None,
   'notes': 'Alan Dominguez: WF - Rose Garden I- 135786-130363 (6G8H Sat & Sun; 8G8H Mon- Fri)',
   'cost': 0.51,
   'qty': 1,
   'tax_name1': '',
   'tax_rate1': 0,
   'tax_name2': '',
   'tax_rate2': 0,
   'invoice_item_type_id': 1,
   'custom_value1': '',
   'custom_value2': '',
   'discount': 0},
  {'account_key': 'b8wbv85qkonicc8euotfvflisfv9klhx',
   'is_owner': True,
   'id': 255,
   'product_key': 'L2 Installer',
   'updated_at': 1604293477,
   'archived_at': None,
   'notes': 'Bruce Jalbert: WF - Rose Garden I- 135786-130363 (6G8H Sat & Sun; 8G8H Mon- Fri)',
   'cost': 0,
   'qty': 7,
   'tax_name1': '',
   'tax_rate1': 0,
   'tax_name2': '',
   'tax_rate2': 0,
   'invoice_item_type_id': 1,
   'custom_value1': '',
   'custom_value2': '',
   'discount': 0},
  {'account_key': 'b8wbv85qkonicc8euotfvflisfv9klhx',
   'is_owner': True,
   'id': 256,
   'product_key': 'L2 Installer',
   'updated_at': 1604293477,
   'archived_at': None,
   'notes': 'Sean Pride: WF - Rose Garden I- 135786-130363 (6G8H Sat & Sun; 8G8H Mon- Fri)',
   'cost': 0,
   'qty': 7,
   'tax_name1': '',
   'tax_rate1': 0,
   'tax_name2': '',
   'tax_rate2': 0,
   'invoice_item_type_id': 1,
   'custom_value1': '',
   'custom_value2': '',
   'discount': 0},
  {'account_key': 'b8wbv85qkonicc8euotfvflisfv9klhx',
   'is_owner': True,
   'id': 257,
   'product_key': 'L2 Installer',
   'updated_at': 1604293477,
   'archived_at': None,
   'notes': 'Erick Diaz: WF - Rose Garden I- 135786-130363 (6G8H Sat & Sun; 8G8H Mon- Fri)',
   'cost': 0,
   'qty': 7,
   'tax_name1': '',
   'tax_rate1': 0,
   'tax_name2': '',
   'tax_rate2': 0,
   'invoice_item_type_id': 1,
   'custom_value1': '',
   'custom_value2': '',
   'discount': 0},
  {'account_key': 'b8wbv85qkonicc8euotfvflisfv9klhx',
   'is_owner': True,
   'id': 258,
   'product_key': 'L2 Installer',
   'updated_at': 1604293478,
   'archived_at': None,
   'notes': 'Damien Toomer: WF - Rose Garden I- 135786-130363 (6G8H Sat & Sun; 8G8H Mon- Fri)',
   'cost': 0,
   'qty': 7,
   'tax_name1': '',
   'tax_rate1': 0,
   'tax_name2': '',
   'tax_rate2': 0,
   'invoice_item_type_id': 1,
   'custom_value1': '',
   'custom_value2': '',
   'discount': 0},
  {'account_key': 'b8wbv85qkonicc8euotfvflisfv9klhx',
   'is_owner': True,
   'id': 259,
   'product_key': 'L3 Installer',
   'updated_at': 1604293478,
   'archived_at': None,
   'notes': 'Michael Quintero: WF - Rose Garden I- 135786-130363 (6G8H Sat & Sun; 8G8H Mon- Fri)',
   'cost': 0,
   'qty': 7,
   'tax_name1': '',
   'tax_rate1': 0,
   'tax_name2': '',
   'tax_rate2': 0,
   'invoice_item_type_id': 1,
   'custom_value1': '',
   'custom_value2': '',
   'discount': 0}]}

The app should ignore any unneeded fields. I’ve checked the logs but aren’t seeing an error, not sure why. You may want to try getting it working first with cURL and then code it.

I have absolutely no experience with working with CURL. I need to get it working with python so that I can place this script within a zapier code step.

Could you potentially send me what it would look like in a CURL request to update an invoice number?

There’s a sample here for updating a client, updating an invoice is very similar

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

Theoretically it SHOULD be similar but it’s not working out the same. I can update a client just fine with Python, but applying it the exact same way for invoices isn’t working.

And previously you have said that in order to update an invoice you have to use ALL fields but with clients you don’t. That is a very big difference right?

I’m not familiar with Python, if you can share a cURL example for updating an invoice I can try to help debug why it’s failing.

It’s best to set all fields for all requests, some fields support being left blank but it isn’t consistent. This was a mistake which has been corrected in v5.

Is there a way I can update to v5?

The Python request works just fine when I update clients. I have no idea why it won’t work for invoices.

I came across this thread where they were able to add a line item without including all fields, are you sure you have to include all fields?

You also keep saying that it has to be done in CURL, but also that you don’t have an example to show?

v5 is only available for selfhost users.

It’s possible you don’t need to send every field as explained above.

I don’t have a sample but if you share a curl request which is failing I’m happy to help you debug it.

Again…I don’t know cURL. Since you are the expert on the app, I would hope that you could provide me a working cURL example of updating an invoice and I could try and debug to Python. You have zero documentation anywhere on updating invoices.

curl -X PUT "https://app.invoiceninja.com/api/v1/invoices/INVOICE_ID" \
  -H "Content-Type:application/json" \
  -d '{"invoice_number":"Test", "invoice_items":[{"product_key": "Test","notes": "Test","cost": 10,"qty": 1}]}' \
  -H "X-Ninja-Token: TOKEN"