How to re-number entries?


I would like to re-number the system generated ID numbers for say “Expenses” based on the oldest to newest entry because right now it’s a hot mess.

e.g. oldest expense entry would be 0001 and the latest would be 7392

I assume the best and fastest way would be a script.

Hi There,

Yes, a script will do the trick here,

You’ll want to pull the expenses into the order that you need, so if you want to sort by date

curl -X GET 'http://ninja.test:8000/api/v1/expenses?sort=date|asc' \
-H "X-API-TOKEN:your_api_token" \
-H "X-Requested-With: XMLHttpRequest" \
-H "X-API-SECRET:password"

Then loop through them.

curl -X PUT 'http://ninja.test:8000/api/v1/expenses/wMvbmOeYAl' \
-H "X-API-TOKEN:your_api_token" \
-H "Content-Type:application/json" \
-d '{"number":"new_number_pattern"}' \
-H "X-Requested-With: XMLHttpRequest";

Just to note, if you are reusing existing numbers… you may want to do a blanket update first by appending a string to the number ie…


So that you do not get any issues of reusing an existing number when attempting the update.

Hey David,

Thank you so much for responding to my request and providing that code! I will try it out. Thanks, again

In case anyone else is interested, this is the code I created in Python:

#!/usr/bin/env python3

import httpx
import json

def update_number( type, status ):

    # --- HTTP REQUEST
    # the BASE URL for EXPENSES
    base_URL = '' + type

    headers = {
        'X-API-Token': YOUR_API_KEY_HERE
        ,'X-Requested-With': 'XMLHttpRequest'

    # URL parameters
    parameters = {
        'per_page': 1000
        ,'sort': 'date|asc'
        ,'status': status

    # connect to API with the HEADERS and PARAMETERS already declared
    response = httpx.get( base_URL, headers=headers, params=parameters )

    # convert data fron JSON to a Python array
    response = json.load( response )

    # copy and thereby decrease 1 level of the multi-dimensional array data
    expense_data = response['data']

    # count the quantity of rows/records retrieved in the previous API call
    count = len( expense_data )
    if count < 1:
        exit('No records were retrieved.')

    print( 'Quantity: ' + str(count) )

    # Loop through each record retrieved from the previous API call
    i = 1

    for x in expense_data:

        prefix = ''

        # --- NOW let's loop through each one and update the number
        if i < 10:
            new_number = prefix + '.000' + str(i)
        elif i < 100:
            new_number = prefix + '.00' + str(i)
        elif i < 1000:
            new_number = prefix + '.0' + str(i)
            new_number = prefix.str(i)

        URL = base_URL + '/' + x['id']

        data = {'number': new_number }

        response = httpx.put( URL, headers=headers, json=data )
        print( 'Post result: ' + response.text )

        i = i + 1
        # --- END of the UPDATE_NUMBER function

# function arguments
1st argument options:
clients, products, invoices, recurring_invoices, payments, quotes, credits,
projects, tasks, vendors, purchase_orders, expenses, recurring_expenses,
bank_transactions, reports, activities, companies, company_users,
expense_categories, subscriptions, system_logs

2nd function argument:
active|archived. NOTE: 'deleted' items cannot be modified.

# call the function and specify the type and status
update_number( 'expenses', 'archived' )
1 Like