Programming Meraki Dashboard API with Python

Posted on 11th February 2021

The Cisco Meraki Dashboard API is a RESTful API to communicate with the Cisco Meraki cloud platform and Meraki devices. Meraki Dashboard API enables you to write a program in any programming language to make an HTTPS request to an endpoint URL and get the response in JSON format. Some of the things you can do with Dashboard API are, you can programmatically,

  • add new organizations and admins
  • create and configure networks
  • add or remove devices on the network
  • create custom dashboards

This article explains how to interface with Meraki Dashboard API using Python.

Enable API access

No matter which method you use, the first step is to enable the API for your organization. To do this login to your Meraki dashboard and navigate to Organization → Settings → Dashboard API access. Verify the checkbox for Enable access to the Cisco Meraki Dashboard API is checked.

Generate API Key

After enabling API access, go to My Profile page from the upper-right corner of the Dashboard. Click the drop-down arrow next to the user name. Scroll down to API access section, and there you will find options to Generate new API key and Revoke existing keys.

Generate a new API key, then copy and save it in a safe place. The API key inherits all the permissions of the user that generated the key, therefore, sharing the API key is like sharing your username and password.

Using requests library with Dashboard API

The requests module in Python allows you to make HTTP requests to the Dashboard API simply and easily. You can use pip to install request

pip install requests

To make requests to Dashboard API in your Python script, begin by importing the requests module.

import requests

To exchange data between your Python script and the web service, make HTTPS requests to endpoint URLs. The endpoint URLs are in the format below.

https://api.meraki.com/api/v1/<resource>

where https://api.meraki.com/api/ is the Base URI, v1 is the API version and resources can be organizations, networks, devices, etc.,

For authorization purpose, you also need to set the API key in the request header as shown below

X-Cisco-Meraki-API-Key = <api key>

A request without the API key will return HTTP error 404.

Putting it all together in a Python script to get the list of organizations, we have the code below.

import requests
import json

# Set api key. The below api key is for the Meraki Sandbox
# Replace it with your API key
api_key = '6bec40cf957de430a6f1f2baa056b99a4fac9ea0'

# Set header parameters
headers = {
   "X-Cisco-Meraki-API-Key" : api_key
}

url = "https://api.meraki.com/api/v1/organizations" 

# Send request and get response
response = requests.request(
   "GET", 
   url,
   headers=headers
)

# Print organization id and name
my_orgs = json.loads(response.text)
for org in my_orgs:
    print(org["id"], org["name"])

In the above example, we uses the HTTP GET method to get information about a resource, ie, Organization. Similarly, use the POST method to send any data the webservice. The following script uses the POST method to create a new network in an organization.


Post a comment

Comments

Nothing yet..be the first to share wisdom.