How to use the Meraki API with Python 3.9

In this article, I will be demonstrating how to use the Cisco Meraki API with Python 3.9 This article is useful for network engineers who want to automate administrative tasks in the Cisco Meraki dashboard.

Necessary Items

To use the Meraki API with Python, you will need the following:

  • A Cisco Meraki account with full administrative access to your organization.

  • API access enabled for your organization.

  • API key.

  • Python 3.9 installed.

  • Meraki SDK Python library installed.

The next few subsections will provide you with steps to fulfill the necessary requirements. I am under assumption that your Meraki account has full administrative access to your organization. If you do not have administrative access, then you will not be able to perform the next steps.

Enabling API Access to your Organization

To enable API access to your Meraki organization, do the following:

Go to Organization > Configure > Settings

Scroll down to Dashboard API Access and ensure that “Enable access to the Cisco Meraki Dashboard API” is checked.

Generating API key

To generate your API key, do the following:

Go to My profile (found in the top right corner)

Scroll down to API access. Click “generate new API key”.

You will be presented with your API key. Copy your API key and store it in a safe place.

Note: do not try to use the key presented. It has been revoked. Do not share your key with people you don’t know. Doing so could allow a threat actor access to your organization.

Installing the Meraki SDK Library

To make API calls to the Meraki Dashboard using Python, you will need to install the Meraki SDK library. Follow these steps to install it via pip:

In your Python terminal, run one of the following commands:

pip3 install meraki

Or, if you're using a Python environment where pip refers to Python 3 by default:

pip install meraki

Basic Usage

When working with the Meraki API in Python, you'll need to use the Meraki SDK library. Here’s how to set it up:

Importing the Meraki SDK

To start using the Meraki SDK in your Python script, import it at the beginning of your file:

import meraki

Setting Up Your API Connection

To interact with the Meraki Dashboard API, initialize the DashboardAPI object with your API key:

import meraki

# Replace "your_api_key" with your actual Meraki Dashboard API key
dashboard = meraki.DashboardAPI("your_api_key")

If you haven't obtained your Meraki Dashboard API key yet, refer to the previous section for instructions on how to obtain it.


Next Sections

In the following subsections, we'll explore practical examples of:

  • Requesting Data from Meraki: Demonstrating how to retrieve information using the Meraki SDK.

  • Pushing Configurations to Meraki: Showing how to update settings and configurations using the SDK.

These examples will utilize the base code provided above to illustrate practical uses of the Meraki API in Python.


Requesting Data from Meraki

The Meraki Dashboard API offers various endpoints for retrieving information. You can explore a comprehensive list of available requests in the Cisco Meraki API documentation on Cisco DevNet.

Example: Fetching Organizations

Below is an example of how to retrieve a list of organizations from your Meraki account using the Meraki SDK in Python:

# Code written by Kaleb Mohr, CCNA. This code may be reused.
import meraki

# Replace "your_api_key" with your actual Meraki Dashboard API key
dashboard = meraki.DashboardAPI("your_api_key")

response = dashboard.organizations.getOrganizations()

print(response)

When executed, this script will output a JSON response containing details of the organizations associated with your Meraki account.

Formatting JSON Output

By default, the JSON output is printed in a compact format. To print it in a more readable format, you can use Python's built-in pprint library:

# Code written by Kaleb Mohr, CCNA. This code may be reused.
import meraki
from pprint import pprint # the pprint library to output json in a more readable format

dashboard = meraki.DashboardAPI("your_api_key")

response = dashboard.organizations.getOrganizations()

pprint(response)

Example Output

Here is an example of the formatted JSON output when using pprint:

2024-03-08 08:50:02       meraki:     INFO > Meraki dashboard API session initialized with these parameters: {'version': '1.43.0', 'api_key': '*********1234af', 'base_url': 'https://api.meraki.com/api/v1', 'single_request_timeout': 60, 'certificate_path': '', 'requests_proxy': '', 'wait_on_rate_limit': True, 'nginx_429_retry_wait_time': 60, 'action_batch_retry_wait_time': 60, 'network_delete_retry_wait_time': 240, 'retry_4xx_error': False, 'retry_4xx_error_wait_time': 60, 'maximum_retries': 2, 'simulate': False, 'be_geo_id': None, 'caller': None, 'use_iterator_for_get_pages': False}
2024-03-08 08:50:02       meraki:     INFO > GET https://api.meraki.com/api/v1/organizations
2024-03-08 08:50:03       meraki:     INFO > organizations, getOrganizations; page 1 - 200 OK
[
    {
        'api': {'enabled': True},
        'cloud': {'region': {'name': 'North America'}},
        'id': '122312',
        'licensing': {'model': 'co-term'},
        'management': {'details': []},
        'name': 'Company_HQ',
        'url': 'https://n77.meraki.com/o/aaef1234a/manage/organization/overview'
    },
    {
        'api': {'enabled': True},
        'cloud': {'region': {'name': 'North America'}},
        'id': '29111',
        'licensing': {'model': 'co-term'},
        'management': {'details': []},
        'name': 'Company_Branch_A',
        'url': 'https://n77.meraki.com/o/29111a/manage/organization/overview'
    },
    {
        'api': {'enabled': True},
        'cloud': {'region': {'name': 'North America'}},
        'id': '29112',
        'licensing': {'model': 'co-term'},
        'management': {'details': []},
        'name': 'Company_Branch_B',
        'url': 'https://n77.meraki.com/o/29112b/manage/organization/overview'
    }
]

This example demonstrates how to fetch organization data from Meraki using the SDK and how to format the JSON output for clarity.

For more API requests and detailed documentation, visit the Cisco Meraki API page on Cisco DevNet.

Pushing Configurations to Meraki - Access Point Configuration Example

Cisco Meraki provides extensive configurability through its API. For more details, refer to the Cisco Meraki API documentation.

Example: Configuring an Access Point

In this example, we'll configure a Cisco Meraki access point (AP) with specific settings including static IP, DNS, default gateway, name, address, notes, and tags.

JSON Configuration

Here's the JSON representing the IP configuration we'll apply to the AP:

{
    "wan1": {
        "usingStaticIp": true,
        "staticIp": "192.168.141.12",
        "staticSubnetMask": "255.255.255.0",
        "staticGatewayIp": "192.168.141.254",
        "vlan": null,
        "staticDns": [
            "192.168.141.252",
            "10.88.96.14"
        ]
    }
}

Python Script

# Code written by Kaleb Mohr, CCNA. This code may be reused.
import meraki
from pprint import pprint

# Initialize Meraki Dashboard API with your API key
dashboard = meraki.DashboardAPI('example_api_key')

# Access Point serial number
ap_serial = 'Q3ZZ-ZZZZ-ZZZZ'

# Update IP configuration
set_ap_ip = dashboard.devices.updateDeviceManagementInterface(
    ap_serial,
    wan1={
        'usingStaticIp': True,
        'staticIp': '192.168.141.12',
        'staticSubnetMask': '255.255.255.0',
        'staticGatewayIp': '192.168.141.254',
        'vlan': None,
        'staticDns': ['192.168.141.252', '10.88.96.14']
    }
)

# Update AP details: name, address, notes, and tags
set_ap_details = dashboard.devices.updateDevice(
    ap_serial,
    name="SAMPLE-COMPANY-AP1",
    address="123 Easy Street, Tampa, FL 33592",
    notes="Example access point configuration",
    tags=['florida_branch', 'access_points']
)

# Print API responses for verification
pprint(set_ap_ip)
pprint(set_ap_details)

Execution and Output

Execute the script to apply the configuration changes:

kalebmohr@Kalebs-MBP General Work % python3 example.py

Upon successful execution, the Meraki API will confirm the update, and you can verify the changes in the Cisco Meraki dashboard.

Output Example

Upon successful execution, you might see an output similar to this, confirming the updated details:

{
    "name": "SAMPLE-COMPANY-AP1",
    "address": "123 Easy Street, Tampa, FL 33592",
    "notes": "Example access point configuration",
    "tags": ["florida_branch", "access_points"],
    "networkId": "123456",
    "serial": "Q3ZZ-ZZZZ-ZZZZ",
    "model": "MR44",
    "mac": "00:11:22:33:44:55",
    "lanIp": "192.168.141.12",
    "firmware": "wireless-25-14",
    "floorPlanId": "g_2176982374"
}

This output confirms that the configuration changes have been successfully applied to the specified access point.

Taking a look in the Cisco Meraki dashboard, you will notice that the access point's configuration applied successfully.


Conclusion

The Cisco Meraki API is a powerful python library you can use to automate administrative tasks in the Cisco Meraki dashboard. By writing python scripts to automate Cisco Meraki tasks, you can spend less time clicking around in the dashboard and rather updating your infrastructure configurations within seconds by writing a simple python script.

To learn more about the Cisco Meraki API, you should check out the official documentation from Cisco Developer:

Cisco Developer - Meraki API Documentation


Thanks for reading this article. I hope you found this article helpful and entertaining. Feel free to leave feedback in the comments.

P.S., I know that I've been on a hiatus from writing content. I've been very busy with work and school life. I'm trying to get back into publishing more content. Expect to see some more articles in the future!