Getting started with Meraki API using Python: Part I

Arun_TE
Meraki Employee

Getting started with Meraki API using Python: Part I

Getting started with Meraki API using Python: Part I

 

Hi Everyone! This post is in response to a large demand from the Meraki community to have something for people to get started on the Meraki API using Python. Here we have Part 1 of a series of Community posts focused on helping you navigate the basics of leveraging the Meraki API using Python. We'll share a new post every month. You can subscribe to the label "Getting Started" to receive an email notification for the next part in this series.

 

Now before we get started with Python, let's cover a few basics, namely what is an API.

 

Application Programming Interface

 

An API is basically a way for two pieces of software to talk to each other. 

 

Arun_TE_0-1660174540891.png

 

REST API

 

There are many different types of APIs and one of them is the REST API. REST stands for Representational State Transfer. What it means is that it involves transferring the representation of the state of information between applications. It is not a protocol like TCP or UDP, it is an architecture, hence as with any architecture it is defined by a set of constraints. A few of those relevant for us here are listed below:

  • It is Server-Client architecture
  • It is Stateless
  • Cacheable
  • Uniform Interface
  • Layered

 

We won't go through all the constraints in detail as we want to keep this simple for now, maybe for another post! 

 

REST uses HTTP as the transaction method and JSON format for data. This makes life easy as we don't have to learn a whole new protocol for APIs. It uses the same HTTP that is used on the Internet. 

 

Arun_TE_1-1660174540885.png

 

 

Therefore interacting with an API server such as the Meraki Dashboard would involve just using HTTP verbs such as :

  • GET
  • PUT
  • POST
  • DELETE

 

Of course, there are more actions available, but we are just focusing on the most common ones for now.

 

Arun_TE_2-1660174540925.png

 

 

As you noticed PUT and POST are both used for writing to the Dashboard, to put it simply you would use POST to create data and PUT to update or edit existing data. For example we use a POST to create a Routed Interface on the switch and a PUT to update an existing one.

Arun_TE_3-1660174540887.png

 

 

Java Script Object Notation or JSON, is the format in which data is sent between your machine and the Dashboard.

Arun_TE_4-1660174540923.png

 



What does JSON data look like? Let’s take a look at an example. Here we can see a dashboard organization retrieved from a dashboard using a GET.




Arun_TE_5-1660174541133.png

 

The attributes of an organization are described through the use of key-value pairs. In this example we can see the organization’s id, name, url etc.

 

Using cURL to understand the Meraki Dashboard API

 

Ok! Now that we know what REST APIs are all about, let's get hands on and try a few requests to the Dashboard!

 

For this one tool that can be used in cURL, another Postman but let's use cURL for now.

 

curl -L --request GET \

--url https://api.meraki.com/api/v1/organizations \

--header 'Content-Type: application/json' \

--header 'Accept: application/json' \

--header 'X-Cisco-Meraki-API-Key: *****'



As you can see, we issued a GET request using cURL using the above command. We will learn about processing the output later but for now let’s break the cURL request down!

 

Arun_TE_6-1660174540921.png

 

For the headers we set to accept and send JSON data and we also send an API key (we will get to this in a bit) for authentication. The url points to api.meraki.com which is where you would send your requests to. Note the v1 in the URL, Meraki APIs are currently on v1 and v0 is deprecated. 



Arun_TE_7-1660174540883.png

 

 

Now let’s use the -v, which is for verbose with the same cURL command and check the output.

 

First we see the connection to api.meraki.com (yes our Dashboard supports IPv6!):

 

Arun_TE_8-1660174541059.png

 

 

Next, we see a 302!!

 

Arun_TE_9-1660174540943.png

 

 

So what is a 302? It is a redirect! Why do we get a redirect? That is because we are being redirected to our Dashboard instance based on the API key we used. api.meraki.com is an API server url we use so we don’t have to remember our individual dashboard instance url. If you notice, on the 5th line from the bottom you can see the redirect location to the specific dashboard url.

 

So here are some common responses you will see.

 

Arun_TE_10-1660174540890.png

 

 

  • If the response is in the 2xx range such as 201 or 200 then the request was successful. We will get to the specifics on the differences between 201 and 200 later on, but as always let’s keep it simple for now!
  • If the response is in the 3xx range as we saw in our example it is a redirect.
  • If the response is in the 4xx range then the error is on the client side, we would need to check our request, for example an incorrect API key.

Arun_TE_11-1660174540905.png

 

  • If the response is in the 5xx range then the issue is on the server side, in our case the dashboard side.

 

Alright, I think we now have some understanding of the API interaction happening with the Meraki Dashboard. We will sign off for now but stay tuned for Part 2 where we shall continue our API journey further!


In the meantime, please don't forget to subscribe to "Getting Started" to receive a notification when Part 2 goes live and also please leave your thoughts/feedback/comments below so that we can improve the posts to focus on the needs of the community.

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

@Arun_TE : Awesome thanks 

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

Nice work @Arun_TE!  🙌

Thanks @Chris_Skees!

Meraki - To do something with soul, creativity or love.
PacerX
Getting noticed

Yassss!! Gimme more of the good stuff. I just started playing around with the API to see what kinds of extra information I could get. Thank you!!

The Wandering Pacer
"Not all who wander are lost" - J. R. R. Tolkien
www.thewanderingpacer.com
Arun_TE
Meraki Employee

Thanks @PacerX , hopefully you have noticed that Parts 2 and 3 are out already and also subscribed to "Getting Started"

Meraki - To do something with soul, creativity or love.
PacerX
Getting noticed

I have seen that the other parts are out and thank you for sharing! Unfortunately programming and python are not my strong areas so I am working through things slowly but I will get there. 😄

The Wandering Pacer
"Not all who wander are lost" - J. R. R. Tolkien
www.thewanderingpacer.com
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.