Cisco Platforms and Development · Topic 1 of 8
Using a Cisco Python SDK
Learn what an SDK is, why it beats raw REST calls, and how to read SDK documentation and write Python scripts that use the Meraki and Catalyst Center SDKs.
- 5 min
- An SDK wraps a platform API in functions for a programming language.
- The usual steps are install, import, create a client, call a method and read the result.
- The Meraki library is installed as
merakiand is created withmeraki.DashboardAPI.
What it is
A software development kit, or SDK, is a library that wraps a platform's API in ready made functions for a programming language.
Instead of building URLs, headers and JSON bodies yourself, you create one client object and call methods on it. The SDK sends the HTTP requests for you.
Cisco and the community publish SDKs for several platforms, mostly in Python. You need to be able to read the SDK documentation and turn it into working code.
How it works
Using an SDK follows the same four steps each time.
The four steps
- Install the package with
pip. - Import it and create a client object with your credentials.
- Call the method that matches the API operation.
- Read the result, which is normally a list or dictionary.
SDK compared with raw REST
| Point | Raw requests | SDK |
|---|---|---|
| URLs and headers | You build them | The SDK builds them |
| Authentication | You handle it | Often handled by the client object |
| Paging and retries | You write the loop | Some SDKs do this for you |
| Method names | None. You use paths | One method per API operation |
| Flexibility | Any endpoint | Only what the SDK supports |
| Updates | Always current | Depends on the SDK version |
The Meraki Python library
The Meraki Dashboard API has an official Python library. You install it with pip install meraki. It takes your API key and gives you an object with a group of methods for each area of the API.
import os
import meraki
API_KEY = os.environ["MERAKI_API_KEY"]
dashboard = meraki.DashboardAPI(API_KEY, suppress_logging=True)
orgs = dashboard.organizations.getOrganizations()
for org in orgs:
print(org["id"], org["name"])
The method name matches the operation name in the Meraki API documentation. The call getOrganizations reads the organisations that the key can see. Results come back as Python lists of dictionaries.
Each operation takes its path parameters as normal arguments.
org_id = orgs[0]["id"]
networks = dashboard.organizations.getOrganizationNetworks(org_id)
for net in networks:
print(net["id"], net["name"])
devices = dashboard.networks.getNetworkDevices(networks[0]["id"])
The Catalyst Center SDK
The dnacentersdk package wraps the Cisco Catalyst Center API. It logs in for you and keeps the token.
import os
from dnacentersdk import api
PASSWORD = os.environ["DNAC_PASSWORD"]
BASE_URL = os.environ["DNAC_URL"]
dnac = api.DNACenterAPI(
username="admin",
password=PASSWORD,
base_url=BASE_URL,
verify=False,
)
devices = dnac.devices.get_device_list()
for device in devices.response:
print(device.hostname, device.managementIpAddress)
The client object handles the token request, so you do not call the authentication endpoint yourself. Results are objects that allow attribute access such as device.hostname.
The verify=False setting suits a lab with a self signed certificate. Avoid it in production.
Other SDKs
Similar packages exist for other platforms. They include Webex, Cisco UCS Manager and Intersight. They all follow the same pattern: install, create a client, call a method, read the result.
Reading SDK documentation
Look for these items in the documentation:
- The install command and the Python versions that are supported.
- How to create the client and which credentials it needs.
- The list of methods, grouped by area.
- The arguments for each method and the shape of the return value.
- Example scripts, which often come close to your task.
When the documentation lists a method such as getNetworkClients, it matches an API operation with the same name. The arguments are the path and query parameters of that operation.
Error handling
An SDK raises an exception when the API returns an error. The Meraki library raises meraki.APIError. It carries the status and the message from the response.
try:
orgs = dashboard.organizations.getOrganizations()
except meraki.APIError as err:
print(err)
To write SDK code from documentation, find the operation, copy its method name and fill in its arguments. Then print the result to see its structure.
What to remember for the exam
Six facts cover this topic.
- An SDK wraps a platform API in functions for a programming language.
- The usual steps are install, import, create a client, call a method and read the result.
- The Meraki library is installed as
merakiand is created withmeraki.DashboardAPI. - Catalyst Center has the
dnacentersdkpackage, which handles token authentication. - Method names follow the API operation names in the documentation.
- SDKs save effort but support only the endpoints that the library has wrapped.
Common mistakes
Four mistakes are common.
Hard coding credentials. Read the key or password from the environment.
Assuming the SDK has every endpoint. A new API operation may not be in the library yet. You can still call it with requests.
Guessing the shape of the result. Print it first. Some SDKs return dictionaries and others return objects.
Mixing up package and import names. Check the documentation for both, because they can differ.
Do not leave verify=False in code that talks to a production system. It turns off certificate checking.
Check yourself
Work through these three questions.
What does an SDK do for you compared with raw REST calls?
It wraps the API in functions, so the library builds the requests and parses the replies.
Where do SDK method names usually come from?
They follow the operation names in the platform's API documentation.
What can you do if the SDK lacks a method for a new API operation?
You can call the endpoint directly with
requests.
Topic 1 of 8 in Cisco Platforms and Development
Test yourself on Cisco Platforms and Development
The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.