Adding Custom Prefixes in Bulk

When activating tens, hundreds, thousands or even tens of thousands of devices at once its always good to give them a specific name to make sure you can keep track of what device is what. In this guide we will use the Hologram REST API and Python 3 to automatically add a prefix to our devices. With a few small changes to the code this same concept can be used to add non sequential custom names to your entire fleet.

If you are interested in adding custom non-sequential names to your devices please visit this guide instead.

1) Use the code pictured above (full copy paste-able text below) in your favorite text editor or Python's IDLE.

2) Replace the green Prefix text with the prefix you'd like to add, but make sure to leave the "  on either side. A -# will be appended to the prefix text starting at 1 and increasing by 1 for every device that is run through this script.
3) Replace DEVICE_1,DEVICE_2,DEVICE_N  with a comma separated list of devices you want to rename. An easy way to get a list of devices is using the CSV export tool at the bottom left-hand side of the Dashboard's Devices section.
4) Replace the green YOUR_API_KEY text with your API Key, make sure to leave the "  on either side.
5) Run the program.

import requests
prefix = "Prefix" #Enter the Prefix you want to add to your device
deviceIDs = [DEVICE_1,DEVICE_2,DEVICE_N] #Enter list of your devices
apikey = "YOUR_API_KEY" #Enter your API Key https://dashboard.hologram.io/account/api
char_count= len(str(len(deviceIDs))) #count the number of characters in the list of devices to pad with zeros
i = 0
for ids in deviceIDs: #loop for all devices in deviceIDs list
    device = str(deviceIDs[i]) #get the device number from deviceID's at position i
    i = i+1 #increase counter since we don't want to start numbering at 0
    i_str = str(i) #save number as string
    print ("Working on record: " + i_str) #print the record we are working on
    url = "https://dashboard.hologram.io/api/1/devices/" + device +"?apikey=" + apikey # set up query
    r= requests.get(url) #get query to get current device information
    jdata = r.json() #format JSON
    name = jdata['data']['name'] #get the device's name
    name = name[-8:] #extract the last few digits of the SIM card automatically added to the device
    print ("device SIM end: " + name) #print the extracted information
    new_name = prefix + "-" + i_str.zfill(char_count) # format new prefix with device count
    new_name += name #format new prefix with SIM card numbers
    print ("new name: " + new_name) #print new name
    url2 = "https://dashboard.hologram.io/api/1/devices/" + device + "?apikey=" + apikey #set up query
    r2 = requests.put(url2, data = {'name': new_name}) #put query to modify the device name

 

 

Was this article helpful?
0 out of 0 found this helpful