Add custom device 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

Screen_Shot_2020-05-22_at_11.37.33.png

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

3) Run the program.

4) When prompted enter your API Key.

import csv
import requests
import base64

encoded_api=str(base64.b64encode((("apikey:"+input("Enter your API Key: ")).encode("utf-8"))),"utf-8") # Ask and Encode API Key

head ={"Authorization":"Basic "+encoded_api} #header
filename = 'hologram_renamer.csv' #CSV file

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
orgid=[] #List where we will store the id of the Orgs where the devices are

with open (filename,'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] # set up query
r= requests.get(get_device_url,headers=head) #get query to get current device information
if str(r) == "<Response [200]>": #Pass if successful
pass
else:
print ("Record Failed ") #Flag if Failed and print failure
print(r)
print(r.text)

jdata = r.json() #format JSON
deviceids.append(jdata['data'][0]['deviceid']) #Add device id to list of device IDs
orgid.append(jdata['data'][0]['orgid']) #Add org to list of orgids

#Rename
rename_device_url = "https://dashboard.hologram.io/api/1/devices/" + str(deviceids[lineCount-1]) +"?orgid=" +str(orgid[lineCount-1]) # set up query 2
r2 = requests.put(rename_device_url, data = {'name': names[lineCount-1]}, headers=head) #put query to modify the device name

if str(r) == "<Response [200]>": # Success status
print ("Finished Record " + str(lineCount-1) +" SIM " + iccids[lineCount-1] + " is now called: "+ names[lineCount-1])#Print out progress
else: #Fail status
print ("Record Failed " + str(lineCount-1) +" SIM " + iccids[lineCount-1] + " is NOT called: "+ names[lineCount-1])#Print out progress
print(r)
print(r.text)


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