Follow me:

Introduction to APIs

Application Programming Interface: communication protocol or interface used to interact between different pieces of software.

In networking, you could have an API available to interact between a Wireless Controller and a script coded by the network admin.

APIs make it very easy to interact with our networking equipment to configure, operate and monitor them.

In the networking world, these API communications are often established over the HTTPS protocol and are categorized as RESTful APIs. It stands for REpresentational State Transfer. It is a very popular API framework for web services built on HTTP.

REST is popular because it is fast, simple, scalable and easy to work with.

Why use an API?

  • Custom dashboards
  • Automation
    • Installs
    • Configuration
  • Testing
  • Troubleshooting

How can I use an API?

Vendors usually provide extensive API documentation that will tell you how to use them and which data to expect. Sometimes, you have to enable this access (Meraki).

You can then use applications such as Postman that will help you to design your API calls. 

Communications are secured and will require some sort of authorization (using a token or API Key). This key will be referenced in every request via a request header.

Methods (API Calls)

  • GET: returns the value of a resource or a list of resources (Ex: get a list of online APs)
  • PUT: adds a new resource (Ex: create a new site)
  • POST: updates a resource (EX: updating the name of an AP)
  • DELETE: remove a resource (Ex: removing a user)

Responses – Status and Error Codes

You will get a response from your API calls. These responses use the standard HTTP status codes:

  • 400: Bad Request
  • 401: Unauthorized
  • 403: Forbidden
  • 404: Not found
  • 429: Too Many Requests
  • 500: Internal Server Error
  • 503: Service Unavailable

If everything goes well, you should receive a 200 status code as well as the response to your query (if it was a GET query). This response is generally organized with the JSON format. It becomes easier to read and parse it in a script.

This format can also be specified in the headers: Content-Type: application/json

Important Things to consider

  • Limitation of number of calls
    • 5 calls per second for Meraki
    • 5000 calls per hour for Mist (to validate)

Sample Scripts

Rowell’s Script

import json
import requests
site_id = '<your-site-id>'
url = "https://api.mist.com/api/v1/sites/{}/wlans".format(site_id)
headers = {
  'Authorization': 'Basic <your-token>'
}
response = requests.request("GET", url, headers=headers)
r = json.loads(response.text)
for wlans in r:
    wlan_name = wlans['ssid']
    wlan_enabled = str(wlans['enabled'])
    wlan_vlan = str(wlans['vlan_id'])
    print("SSID: " + wlan_name, "\t Enabled: " + wlan_enabled, "\t VLAN: " + wlan_vlan )

Sample Output

% python3 get_wlans.py
SSID: D-NET  Enabled: True 	 VLAN: 2
SSID: CTS 	 Enabled: True 	 VLAN: 2

François’ Script

#!/usr/bin/env python3
import time
import json
import requests


def main():
    org_id = '<org-id>'
    site_id = '<site-id>'
    token = '<your-token>'
    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 Address: {}'.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))

Resources

Join Clear To Send

Come join the Clear To Send community.

Powered by ConvertKit
Hosted by
Rowell

Rowell, CWNE #210, is a network engineer in Higher-Ed. He enjoys working with wireless networking technologies and loves to share and engage with the community. You can connect with him on Twitter, LinkedIn, and Facebook.

Join the discussion

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from this show

Episode 208