Adding Custom Names 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. This is especially true if you have a data management system that gives your hardware a specific name. In this guide we will use the Hologram REST API and Python 3 to upload a CSV and automatically rename our devices.

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

1) First we need to make the CSV. This is a simple document with two columns one for the Name we want to give the SIMs and the other with the SIM number  (ICCID)  of the SIM we want to receive this new name. 

It is important to name this CSV document hologram_renamer.csv  and to make sure the top row is used as a header and does not include data as our program skips it. You will also want to make sure that the program and the CSV file are on the same path to make your life easier.

2) Now we move on to the code

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

3) Replace the green YOUR_API_KEY text with your API Key, make sure to leave the "  on either side.

4) Run the program.

import csv
import requests
apikey = "YOUR_API_KEY" #Enter your API Key https://dashboard.io/account/api

names=[] #List where we will store the device names we pull from the CSV
iccids=[] #List where we will store the device iccids we pull from the CSV
deviceids=[] #List where we will store the device IDs we look up


with open ('hologram_renamer.csv','r') as file: #Open the CSV with the new names and SIMs to rename.
    reader = csv.reader(file,delimiter=',')
    lineCount=0 #Counter to be used to get specific list values
    for line in reader: #Iterate through all the rows in the CSV
        if lineCount==0:#Skip header row
            lineCount = lineCount + 1
        else:
            #Populate list of names
            names.append(line[0])
            #Populate list of iccids
            iccids.append(line[1])

            #Populate list of deviceids
            get_device_url ="https://dashboard.hologram.io/api/1/links/cellular?sim="+iccids[lineCount-1]+"&apikey=" + apikey # set up query
            r= requests.get(get_device_url) #get query to get current device information
            jdata = r.json() #format JSON
            deviceids.append(jdata['data'][0]['deviceid'])

            #Rename
            rename_device_url = "https://dashboard.hologram.io/api/1/devices/" + str(deviceids[lineCount-1]) + "?apikey=" + apikey #set up query
            r2 = requests.put(rename_device_url, data = {'name': names[lineCount-1]}) #put query to modify the device name
           
            print ("Finished Record " + str(lineCount-1) +" SIM " + iccids[lineCount-1] + " is now called: "+ names[lineCount-1])#Print out progress
            lineCount = lineCount + 1 #Increase counter

    print("Renaming Complete") #Let us know the program ran to completion
Was this article helpful?
0 out of 0 found this helpful