Skip to content

Lab 4 - Meraki Python SDK

Python is a popular programming language due to its simplicity, readability, and vast ecosystem of libraries. It is widely used in network automation, data analysis, and web development, making it a versatile choice for many applications.

Information

This lab focuses on using the Meraki Dashboard API Python SDK. If you're new to Python programming, don't worry—just follow the outlined steps.

If you'd like to learn more about Python, there are many excellent free courses available online, such as the freeCodeCamp Youtube video, Google's Python Class, and many more. In today's AI era, feel free to leverage any Large Language Model (LLM) of your choice to help with code generation, review, and testing.

Steps

  1. Install Python on your local computer.

Windows: You can install Python using the full installer available on the Python website. Choose the installer that matches your system architecture (32-bit or 64-bit) and follow the installation steps. Ensure you add python.exe to your PATH during installation.

Python installer for Windows

macOS: Use Homebrew to install Python by running brew install python3 in the terminal. This method simplifies the installation process and manages dependencies effectively.

!!! tip "Hint" After installation, use the commands python --version or python3 --version to verify your Python version.

  1. It's recommended to use Python virtual environment to manage dependencies and avoid conflicts. Depending on your operating system, create and activate a virtual environment for this lab with following commands:
Command Prompt (CMD) on Windows Terminal on macOS
python -m venv jumpstartlab python3 -m venv jumpstartlab
jumpstartlab\Scripts\activate source jumpstartlab/bin/activate
cd jumpstartlab cd jumpstartlab
  1. Run the command pip install meraki to install the Meraki Dashboard API Python SDK.

!!! tip "Hint" After running the pip install meraki command, you can use pip list to see the installed packages. You'll find meraki along with its dependencies, such as requests, which is used for making REST API calls.

  1. Navigate to your local virtual environment folder (jumpstartlab), then go to lib/pythonx.x/site-packages/meraki/api. (Note: The exact path may vary depending on your operating system and Python version.) Explore the structure of the Meraki package—resources are organized per platform or per product, similar to how they're listed in the Meraki Dashboard API documentation.

Meraki package structure

  1. Your Python environment is now ready. Download the jumpstart.py script from the GitHub repository and place it in your virtual environment folder named jumpstartlab.

jumpstart.py in folder

  1. Use your preferred editor to open jumpstart.py and update it by replacing the API key and organization ID with your own values. Be sure to enclose both values in single quotes. As a reminder, for this lab you should only use the provided lab organization.

Editing API key and org ID

!!! info "Information" Popular editors for Python include VS Code, known for its extensions and ease of use, and PyCharm, a powerful IDE with advanced features. Lightweight options like Sublime Text and Thonny are also great, especially for beginners.

!!! danger "Important" Always store your API key securely. In production environments, avoid hardcoding credentials or API keys directly in your scripts—instead, use environment variables or a secret manager to keep them safe.

  1. Next, update the DashboardAPI session instance by adding the following parameters after the apikey=API_KEY line in constructor and save the changes.
caller = 'Jumpstart/1.0 Python',
simulate = True,
retry_4xx_error = True,
retry_4xx_error_wait_time = 5,
output_log = False,

DashboardAPI session parameters

!!! info "Information" The Meraki Python SDK provides a convenient way to interact with the Meraki Dashboard API through a persistent API session. With features like automatic rate-limit handling, customizable logging, retry logic for common API errors, and proxy support, it simplifies robust and secure API integration. This session also supports simulation mode for safe testing and can be configured to return paginated responses as iterators for better performance with large datasets.

  1. Make sure your virtual environment is activated (the terminal prompt should start with the environment name). With the simulation enabled, it's safe to test the script by running it. Run python jumpstart.py

Script run with simulation

  1. Simulation mode is a great way to test your code safely without any changes to the Meraki Dashboard. However, because simulation is enabled, the update to the campus network name did not take effect. In your script, change the simulate parameter to False and save the change.

Setting simulate to False

  1. Run the script again. This time, this script should successfully rename the campus network to 'Python-Campus' as well as rename the switch to 'Campus-switch'. However, the subsequent API operation to update switch port #2 failed due to 400 Bad Request. According to the API error message, the failure is due to missing required port configuration options.

    400 error on port update

    Note

    In step#7, you set retry_4xx_error=True and retry_4xx_error_wait_time=5. That's why, when a 4xx error occurred (in this case, a 400 Bad Request), the script retried twice before eventually failing and existing. This feature helps improve your code's resilience in handling certain edge-case errors. Additionally, there are dedicated parameters in the SDK specifically for handling HTTP 429 rate limit errors.

  2. To fix this, open your script and locate the line port_config = {}. Replace it with the following and save the change.

    port_config = {'name': 'To-Campus-AP', 'allowedVlans': '10-200', 'tags': ['wireless','meetingroom']}
    

    Fixed port_config

  3. Run the script one more time. The script will wrap up by completing the following configuration for the campus site. With Python loops and other programming features, the Meraki Python SDK enables highly efficient and automated network operations in just a few lines of code.

    • Rename the wireless AP(s) using the last 4 digits of their MAC address
    • Configure the Jumpstart-corp SSID with Meraki Authentication
    • Configure the Jumpstart-guest SSID using PSK

    Script completion Script output

  4. Navigate to the Python-Campus network in the Meraki Dashboard and verify that all configurations automated by your Python script have been successfully applied. For reference, the following flowchart outlines the sequence of user and API operations performed in this Meraki Python SDK lab.

    Python SDK lab flowchart

  5. As you conclude the Meraki Dashboard API Jumpstart lab, it's the time to review your API usage footprint. Navigate to Organization > API & Webhooks in the Dashboard to view the details.

    API usage analytics

    Note

    You can easily identify applications by their User-Agent and review their performance, even if the API requests were made from the same Dashboard account using the same API key.