Skip to main content

How to rename SIMs using the REST API

You can set device names programmatically using the Devices endpoint. Below are two approaches: upload a CSV of ICCIDs and names, or apply a sequential prefix across a list of device IDs.
Note: You can also rename SIMs in your dashboard.

Update device names using a CSV

1

Prepare a CSV with Name and ICCID columns

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.
NameICCID
New Name 18901000000000000001
New Name 28901000000000000002
New Name 38901000000000000003
Caution: 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.
For smaller batches, you can also use Dashboard bulk actions to rename SIMs.
2

Use a helper script to perform updates

Now we move on to the code. Use the code below in your favorite text editor or Python’s IDLE.
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
3

Run the program

Run the program and when prompted enter your API Key.
Note: Always test with a few devices before applying bulk changes.

Add sequential names with a shared prefix

1

Use a helper script to add sequential names with a shared prefix

Use the code below in your favorite text editor or Python’s IDLE.
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
2

Enter your new name prefix

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

Enter the list of device IDs to update

Replace DEVICE_1,DEVICE_2,DEVICE_N with a comma separated list of device IDs 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

Enter your API Key

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

Run the script

Run the script to rename the devices.