Mist – API Call within a Python Script

In a first video, I explain everything you need to create your first API call to the Mist system dashboard. In order to make this API call, I use an application called Postman. Postman allows you to build your API calls in a very easy way. This is useful to test the API calls, make sure they work and analyze the results we get from the Mist dashboard.

Then, in a second video, I explain how to include the same API call within a Python script. I then go on and go over a script used to modify the name of an access point using an API PUT call.

Here is the script used to send the GET API call and list the access points available in my lab:

import time
import json
import requests
 
def main():
org_id = '22bfc07a-...-92593049db0d'
site_id = '1a13f6c2-...-016fa4ac9ee7'
token = 'o2g...8kh'
mist_url = 'https://api.mist.com/api/v1/'
headers = {'Content-Type': 'application/json',
'Authorization': 'Token {}'.format(token)}
 
api_url = '{0}sites/{1}/devices'.format(mist_url,site_id)
response = requests.get(api_url, headers=headers)
 
if response.status_code == 200:
devices = json.loads(response.content.decode('utf-8'))
 
print('--------------')
for device in devices:
print('AP Name : {}'.format(device['name']))
print('AP Model : {}'.format(device['model']))
print('AP MAC Adress: {}'.format(device['mac']))
print('--------------')
 
if __name__ == '__main__':
start_time = time.time()
main()
run_time = time.time() - start_time
print("** Time to run: %s sec" % round(run_time,2))

Here is the script used to change the name of an access point based on its MAC address:

from optparse import OptionParser
import time
import json
import requests
 
def main():
usage = "usage: %prog AP_MAC_Address AP_New_Name"
parser = OptionParser(usage)
(options, args) = parser.parse_args()
if len(args) == 2:
ap_mac = args[0]
ap_new_name = args[1]
else:
print('Wrong set of arguments! Please see usage below:\n\t{0}'.format(usage))
return
 
org_id = '22bfc07a-...-92593049db0d'
site_id = '1a13f6c2-...-016fa4ac9ee7'
token = 'o2g...8kh'
mist_url = 'https://api.mist.com/api/v1/'
headers = {'Content-Type': 'application/json',
'Authorization': 'Token {}'.format(token)}
 
# Retreiving the list of AP deployed at one site through an API GET call
api_url = '{0}sites/{1}/devices'.format(mist_url,site_id)
response = requests.get(api_url, headers=headers)
 
if response.status_code == 200:
devices = json.loads(response.content.decode('utf-8'))
 
# Looping on these APs until we find the one we want to update
for device in devices:
if device['type'] == 'ap': # Making sure that we are modifying an AP object
if device['mac'] == ap_mac: # Finding the AP we want to modify based on its MAC Address
old_name = device['name']
device['name'] = ap_new_name # Update the name of the AP
device_id = device['id']
data_put = json.dumps(device)
 
# Building and Sending an API call to update the AP object
api_url = '{0}sites/{1}/devices/{2}'.format(mist_url,site_id,device_id)
response = requests.put(api_url, data=data_put, headers=headers)
 
if response.status_code == 200:
print('{0} AP name changed from "{1}" to "{2}"'.format(device['mac'],old_name,device['name']))
else:
print('Something went wrong: {}'.format(response.status_code))
 
else:
print('Something went wrong: {}'.format(response.status_code))
 
if __name__ == '__main__':
start_time = time.time()
main()
run_time = time.time() - start_time
print("** Time to run: %s sec" % round(run_time,2))

I hope that these scripts will be useful to you!

guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Xavier Toefield

Love the videos. While working with the “GET API call and list the access points available” calling api_url didn’t work for me and it would just return the time it took to run. I had to change it to call mist_url and it worked for me.