Getting started with Meraki API using Python Part 2: Python Setup & Intro to Meraki Library

Arun_TE
Meraki Employee

Getting started with Meraki API using Python Part 2: Python Setup & Intro to Meraki Library

Getting started with Meraki API using Python: Part 2

 

Hi everyone! Welcome to Part 2 of a series of posts to get you started with Meraki APIs using Python!

 

Hopefully you are here because you have read Part 1 and are continuing your journey. If you haven’t, please do have a look at Part 1 so you get an understanding of the basic behaviour of Meraki API.

 

Ok so let’s get started!

 

First of all, of course, you need to have Python installed. This series will not cover installing Python, mainly because it depends on the Operating System you want to use, and also because it is relatively easy to install.

 

You can head to Download Python and download it. 

 

Alternatively you can head to https://www.pythonanywhere.com/ and use their cloud bash shell.

 

If you will be using Python on your local machine, it is strongly recommended to use Python virtual environments when you are testing to prevent accidentally crashing your system.  I strongly recommend getting into the habit of creating virtual environments for testing code.

 

“But how do you create a virtual environment?”, I hear you ask. Let’s go through it!

 

First, you will use the python venv module to create a virtual environment.

 

Arun_TE_0-1661496234977.png

 



Python -m

This option is used to call a python module.

venv

In this case we call upon the venv module.

meraki_api_practice

This is the name we are giving for the virtual environment.



Now you will see a directory created with the name you provided. If you list the directory, you will see a bunch of files:

 

Arun_TE_1-1661496234906.png

 

 

We don’t need to worry too much about the files for now, but we do need to activate the environment.

 

Since I am already in the directory, I will use the following command:

 

Arun_TE_2-1661496234971.png

 

 

I can see that my prompt now shows that I am in my virtual environment, so whatever I do here will not affect my operating system!

 

Ok, now that my environment is ready, let us go through a few concepts before our first code.

 

To send requests to the Meraki Dashboard there are two ways:

 

  • Use the python meraki library
  • Use the python requests library



Arun_TE_3-1661496234973.png

 

 

The Meraki Library is installed using pip, whereas the requests library is native to Python. This is one of the reasons why Python is very popular when it comes to programming. There are a lot of libraries out there that can be installed and used, which means that one doesn't have to recreate code; just check if there is a library, install, and use it!

 

So, let’s install the Meraki library in our environment.

 

Arun_TE_4-1661496235259.png

 

 

Another pip package I like to install is IPython. While it’s not really necessary, IPython gives a nice interface to run Python code, which leads to another Python concept.

 

One of the cool things about Python is that you don't have to compile it before you run code. You can pretty much run it directly from the Python interpreter.

 

Arun_TE_5-1661496234978.png

 

 

Here I ran the Python command, and it took me to the interpreter which is identified by the 3 greater than signs in the prompt.

 

So, what code can I run here? This is where API docs come in handy.

 

Navigate to https://developer.cisco.com/meraki/api-v1/

 

Under API reference let’s navigate to API->Platform->configure->organizations->Get Organizations.

 

Arun_TE_6-1661496234940.png

 

Arun_TE_7-1661496234974.png

 

 

You should be here:

 

Arun_TE_8-1661496235076.png

 

 

Select Template on the right:

 

Arun_TE_9-1661496234914.png

 

 

Here you can see that you either copy the Meraki Python Library Code or the Python-Requests Library code.

 

Let us start with the Meraki Python Library Code.

 

Arun_TE_10-1661496235100.png

 

 

Now paste it into the terminal with the Python interpreter running.

 

Arun_TE_11-1661496235187.png

 

 

In the first box, you can see that immediately after dashboard = meraki.DashboardAPI(API_KEY), an API session is being established. We can see the base_url and the version of the Meraki Library being used.

 

In the second box, you can see two GET requests and then a 200 OK. If you have read Part 1, you would be familiar with why there are 2 GET requests and also the 200 response. 

 

You might notice that now the prompt is waiting for you at print(response).

 

If you hit Enter, you will see a huge text output (we will cover this later).

 

Arun_TE_12-1661496234975.png

 

 

If you have managed to get to this point, congratulations! 🎉You have just made your first API request to Meraki Dashboard!

 

So now you may be wondering, what is it that I have accomplished here? It’s simple, you have just fetched a list of organizations from a Meraki Dashboard.

 

Let’s break it down!

 

This is the code.

 

Arun_TE_13-1661496235175.png

 

 

Arun_TE_14-1661496235003.png

 

 

As you may have noticed, we do not recommend storing the API key in this manner. We are simply doing this here to make it easy to understand what is going on.

 

Why is this not recommended? Because if you share the code (which most of the time one does), then you have just shared your API key as well. 

 

What, then, is the recommended way, you ask? Well, there are many ways to secure the key. I will share the method I have used: OS env variables.

 

If I do directory listing and also include hidden files on my working directory:

 

Arun_TE_15-1661496234972.png

 

 

I can see there is a .env file. What is this file? Let's read it using cat.

 

Arun_TE_16-1661496234976.png

 

 

Ok, so I have stored the API key using a variable called API_KEY in a file called .env (the dot makes it a hidden file). By storing the API Key here, I don't have to put it in my code. Now when I do share it, I don’t have to worry about my API key being exposed.

 

Cool, so now how do I call on my API key within my script then?

 

Let’s see.

 

Arun_TE_17-1661496234977.png

 

 

Adding these few lines will allow my Python code to get the API key from the .env file. 

 

Let’s break this down!

 

Arun_TE_18-1661496234979.png

 



If you see the previous get org’s code, we used a variable API_KEY to set the key. It is the same, but now we read it from our .env file.

 

Now try the code again yourself, but this time use the API key from your environment variable.

 

Alright! We will sign off here now, but look out for Part 3 in this series to continue the journey further!


Please leave your comments/questions/feedback below to help us continue this further. Also don’t forget to subscribe to Getting Started to get notified on the next post!

Meraki - To do something with soul, creativity or love.
4 REPLIES 4
Inderdeep
Kind of a big deal

@Arun_TE : Thanks Arun for the update !

Regards/Inder
Cisco IT Blogs awarded in 2020 & 2021
www.thenetworkdna.com

Thanks @Inderdeep for follwing!

Meraki - To do something with soul, creativity or love.
PhilipDAth
Kind of a big deal

Big effort.  Well done!

Thanks mate!

Meraki - To do something with soul, creativity or love.
Get notified when there are additional replies to this discussion.
Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.