When activating tens, hundreds, thousands or even tens of thousands of SIMs at once, giving the SIMs specific names will help you keep track of your devices. This is especially true if you have a data management system that gives your hardware a specific name.
In this guide we will walk through two methods of adding custom names to SIMs using the Hologram REST API and Python 3. First by uploading a CSV
with custom names for each SIM (link), then by sequentially adding names using a single prefix (link).
Add custom names using a CSV
- 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 an ICCID from the SIM we want to receive this new name.
Note: It is important to name thisCSV
documenthologram_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 theCSV
file are on the same path to make your life easier.
- Now we move on to the code. Use the code pictured below (full copy/paste-able text is at the bottom of this guide) in your favorite text editor or Python's IDLE.
- Run the program.
- When prompted enter your API Key.
Code example
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
Add sequential names with the same prefix
- Use the code pictured above (full copy paste-able text below) in your favorite text editor or Python's IDLE.
- 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. - 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 theCSV
export tool at the bottom left-hand side of the Dashboard's Devices section. - Replace the green
YOUR_API_KEY
text with your API Key, make sure to leave the"
on either side. - 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