my cheat sheet on Meraki API

Home

1 Meraki APIs

Meraki has five primary APIs, all of the REST APIs:

  1. Meraki Dashboard API
  2. Scanning API
  3. mV Sense API
  4. External Captive Portal API –> was this the "Meraki Location API" ??
  5. Wireless Health API

1.1 Meraki Dashboard API

The Dashboard API is a modern, RESTful API using HTTPS requests to a URL and JSON as a human-readable format. For config and monitor Meraki networks.

1.2 Scanning API

Enables Cisco Meraki users to detect and aggregate =real-time= data for custom applications. can be used to detect Wi-Fi and Bluetooth Low Energy (BLE) devices in real-time. The elements are exported via an HTTP POST of JSON data to a specified destination server, for analysis (i.e. Customer analytics back-end)

Using signal strength, the wireless client's location is estimated, trianulated.

1.3 mV Sense API

Provides a collection of endpoints to interact with Meraki cameras, zone, and analytics.

1.4 External Captive Portal API

Extends the power of the built-in Meraki splash page functionality by providing complete control of the content and authentication process offering to redirect login and authentication to client-provided servers, using your own authentication, authorization, and accounting (AAA) servers as well.

1.5 Wireless Health API

Allows you to retrieve wireless health information such as connection health, connection failures, and network latency.

2 Accessing the Meraki API documentation.

First, login to your meraki dashboard account. There click help followed by API docs You will find yourself building API requests by always referring back to the API docs. See https://developer.cisco.com/meraki/api-v1/

4 Meraki API Authentication

First, you follow the instructions in documentation.meraki.com which directs you to first enable the API for you account, then generate & retrieve the API token for your account. so:

  1. Enable API for you account by navigating to:
  2. Organization > Settings > Dashboard API access
  3. Click "Generate API Key"

Copy that key into an environment variable:

  • export MERAKI_DASHBOARD_API_KEY=093b24e85df15a3e66f1fc359f4c48493eaa1b73

Or paste it into env_user_zp.py and then encrypt.

Uses tokens as per https://developer.cisco.com/meraki/api-v1/#!authorization Suggestion is to use environment variables (they are safer and do not get saved in text format in any script files.

4.1 JSON

{ "X-Cisco-Meraki-API-Key": <MerakiAPIKey> }

4.2 CURL

curl https://api.meraki.com/api/v1/organizations \ -H 'X-Cisco-Meraki-API-Key: {MERAKI-API-KEY}'

4.3 302 or 307 REST API status error codes

If you get a 302 Error code as a response to a GET,

That is informational, and tells you that you will get better response times if you go to the meraki cloud cluster n252 directly next time.

If you get a 307 (for Postman) or 308 (some other client) Error code as a response to a DELETE, POST, PUT, it really says the same thing. Do it!

That is informational, and tells you that you will need to change your URL to go to the meraki cloud cluster n252 directly next time.

5 My postman

Given the API documentation, you can build a Postman API request manually by filling in the fields manually. These include the type (GET/POST/PUT/ DELETE), the URL, and the headers. But there are easier ways of doing that.

5.1 Download the Postman API collection from Meraki:

Cisco developed Postman collections can be downloaded by starting at https://developer.cisco.com/. Login, then find it.

Meraki developed Postman collections can be downloaded by starting at https://developer.meraki.com/ which gets forwarded (after login) to: https://developer.cisco.com/meraki/

Click Resources followed by Postman Collection. That gives you good docs on postman with Meraki, but really you should click on Run in Postman in the top right corner. That will download the Meraki dashboard API to your postman collection. As of Jan 2021 it included 450 requests and was at version Meraki Dashboard API - v1.5.0

With collection, you will also get environments, that you can then edit and fill in with you own Meraki API key and Organization ID etc. See below for how to get the organization key.

5.2 Get request for meraki organization ID

Using the Meraki Dashboard API, find the "List the Organizations" request and run that. You can fill in the API key manually the first time you do it, but really you should save your API key in a variable that is stored in the Meraki Dashboard Environment, accessible in the top right, by 1st selecting the environment, then clicking the eyeball, then quick view, then edit.

For example the API key is X-Cisco-Meraki-API-Key which is referenced in a postman request using double curly braces: {{X-Cisco-Meraki-API-Key}}

Meraki-environment-variables.png

Figure 1: Accessing Postman Variables

Postman-Meraki-list-organization-id.png

Figure 2: Postman build a Meraki Get Organization request.

The equivalent curl command:

  curl --location --request GET 'https://api.meraki.com/api/v1/organizations' \
--header 'X-Cisco-Meraki-API-Key: 18b62c93fe80f9918d73c5d2180f5e221527cb4e'

Once we have the organization ID, we should update the environment variables to include this field. The above image, Postman-Meraki-list-organization-id.png shows this organization already included in the Meraki variables.

5.3 Get request for networks within an organization ID

5.3.1 Code:

Note that your API key has to be retrieved properly first, from the Meraki Dashboard. Login, organization,

 curl --location --request GET 'https://api.meraki.com/api/v1/organizations/158264/networks' \
--header 'X-Cisco-Meraki-API-Key: 6811632a938e526995bc2365a7170b55a3257c7b8f' \

After the first authentication has occured, subsequent requests will have a session-id cookie set so the code would be:

    curl --location --request GET 'https://api.meraki.com/api/v1/organizations/158264/networks' \
--header 'X-Cisco-Meraki-API-Key: curl --location --request GET 'https://api.meraki.com/api/v1/organizations/158264/networks' \
--header 'X-Cisco-Meraki-API-Key: 6811632a938e526995bc2365a7170b55a3257c7b8f' \
--header 'Cookie: _session_id=c867f264da6beacbc2676a7c6bf602f5'

Postman-Meraki-list-networks.png

Figure 3: Postman build a Meraki Get List of Networks

6 Postman variables

In a collection, you can have certain environments "active" In each of those environments you can "quick view" then "edit"

Postman is also able to show you code equivalents for any REST API request it sends. For example, I got this code (right side, click on </> to expand the code

import requests

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

payload={}
headers = {
  'X-Cisco-Meraki-API-Key': '18b62c93fe80f9918d73c5d2180f5e221527cb4e'
}

response = requests.request("GET", url, headers=headers, data=payload, timeout=400)

print(response.text)

See file:///Users/zintis/bin/python/bin/meraki/meraki-rest-api.py

7 Meraki pagination

The meraki API docs, on pagination shows that the API accepts three query parameters

  1. perPage
  2. startingAfter
  3. endingBefore

The values can be either integer IDs, or timestamps, dependig on the API endpoint (i.e. the URL that you are requesting) The integer IDs are simply counts of records when the returned page has many records.

When sending a query, the actual records queried are specified in the perPage value. Look for the response, specifically a custom header named link. The link header is a comma separated list of up to 4 links:

  1. first
  2. prev
  3. next
  4. last

Each of these returned links have the requestpath + appropriate values of the perPage, startingAFter and endingBefore values

For example: GET https://api.meraki.com/api/v0/networks/N_1234/bluetoothClients?perPage=5

8 Tests to set dynamic environment variables

You can take the output of a successful Get request, which should be in JSON format, and set an environment variable from this response automatically, so that you don't have to cut and paste tokens into the environment variable manually. This saves time.

In the Tests tab, you can write some JavaScript code to capture a variable and store it in a Postman variable. Postman uses this syntax:

8.1 Setting environment variables

  • pm.environment.set("Token", jsonData["access-token"]); # use [] if key has a hyphen
  • pm.environment.set("Token", jsonData"token");
  • postman.setEnvironmentVariable is the equivalent, but deprecated syntax

Details can be found in the postman-sandbox-api-reference

To set the variable with the specifie name and value in the active environment:

  • pm.environment.set(variableName:String, variableValue:*):function
  • pm.environment.set(myOrgID:String, variableValue:*):function

8.2 Reading variables from previous responses

  • pm.response.code:Number # for the response status code
  • pm.response.headers:HeaderList # for a list of the response headers
  • pm.response.json():Function -> Object # you can drill down into the

8.3 did not work (Meraki API)

  • pm.environment.set("orgID", pm.response.json().id)
  • pm.environment.set("uzvards", pm.response.json().name)

They set the variable ok, but the value was "null".

8.4 this also did not work (Meraki API)

  • var jsonData = JSON.parse(responseBody);
  • pm.environment.set("uzvards", jsonData.name);
  • pm.environment.set("OrgID", jsonData.id);

Same thing happened

8.5 try this:

  • var jsonData = pm.response.json();

Nope…

8.6 Why this did not work and background

For the record, the above snippets may have worked had the response been different from what the Meraki response actually was.

For example here is a postman test script that would work with the corresponding response data.

cont myjsonData = pm.response.json();
pm.environment.set("mytick", myjsonData.response.serviceTicket);

If the response code was:

{
    "response": {
        "serviceTicket": "TQG=15lj6k6c-bkl--6a136326XQ-l8v526h-91lh",
	"idleTimeout": 1800,
	"sessionTimeout": 21600
    }
}

In a new request, you could reference the serviceTicket with {{mytick}}

8.7 this worked!!

const jsonData = pm.response.json();

  • pm.environment.set("uzvards", jsonData[0].name)
  • pm.environment.set("organizationId", jsonData[0].id)

This worked because the response I got from meraki was not just the json I expected, but rather a list [] with 1 element in it that had the json in it.

My response was this:

[
   {
	"id": "158264",
	"name": "Pērkons",
	"url": "https://n77.meraki.com/o/Oozq4a/manage/organization/overview"
    }
 ]

For which you can see I need to specify that I need the first element of the list, which is element [0], hence jsonData[0] was needed.

But I was expecting this:

{
     "id": "158264",
     "name": "Pērkons",
     "url": "https://n77.meraki.com/o/Oozq4a/manage/organization/overview"
 }
Notice the missing outer []

For which case my code would have worked.

9 PYTHON Meraki SDK (using ENIRONMENT VARIABLES)

The Meraki SDK is still in Beta (as of Feb 2021) so this is just FYI for now.

import meraki

# Defining your API key as a variable in source code is not recommended
API_KEY = '6bec40cf957de430a6f1f2baa056b99a4fac9ea0'
dashboard = meraki.DashboardAPI(API_KEY)

# Instead, use an environment variable, for example:
# export MERAKI_DASHBOARD_API_KEY=6bec40cf957de430a6f1f2baa056b99a4fac9ea0
dashboard = meraki.DashboardAPI()

9.1 Home