Cisco Platforms and Development · Topic 8 of 8
Coding Tasks with Platform APIs
Write Python against API reference docs to list devices on Meraki, Catalyst Center, ACI, SD-WAN and NSO, manage Webex spaces, and list network clients.
- 5 min
- Authenticate first, then call the endpoint, then loop over the list in the reply.
- Meraki lists devices with
/networks/NETWORK_ID/devicesand clients with/networks/NETWORK_ID/clients. - Catalyst Center returns lists under
responseand takes theX-Auth-Tokenheader.
What it is
A coding task gives you a goal and a piece of API reference, and you write the calls that reach the goal.
The outline lists three families: get a list of network devices, manage Webex spaces, participants and messages, and list the clients seen on a network. All follow the same pattern: authenticate, call an endpoint, read the JSON.
Every endpoint here is shown as a path. Your code adds the base address that the platform documents.
How it works
Start with how the platform authenticates, then choose the endpoint, then read the fields.
Meraki devices and clients
Authentication is the API key in a header. Devices belong to networks, which belong to an organisation.
import requests
headers = {"X-Cisco-Meraki-API-Key": API_KEY}
orgs = requests.get(BASE_URL + "/organizations", headers=headers).json()
org_id = orgs[0]["id"]
nets = requests.get(
BASE_URL + "/organizations/" + org_id + "/networks", headers=headers
).json()
net_id = nets[0]["id"]
devices = requests.get(
BASE_URL + "/networks/" + net_id + "/devices", headers=headers
).json()
clients = requests.get(
BASE_URL + "/networks/" + net_id + "/clients",
headers=headers,
params={"timespan": 86400},
).json()
The reply is a JSON list. Each device has fields such as name, model and serial.
Each client has fields such as description, ip and mac. The timespan is in seconds.
Catalyst Center devices and hosts
First get a token, then send it in X-Auth-Token.
auth = requests.post(
BASE_URL + "/dna/system/api/v1/auth/token", auth=("admin", PASSWORD)
)
headers = {"X-Auth-Token": auth.json()["Token"]}
devices = requests.get(
BASE_URL + "/dna/intent/api/v1/network-device", headers=headers
).json()["response"]
for d in devices:
print(d["hostname"], d["managementIpAddress"])
The list sits under the response key. For clients and hosts, Catalyst Center has lookups in the same Intent API. The exact path, filters and field names depend on the release, so read them from the documentation and then repeat the same pattern: send the token header and loop over response.
ACI devices
The APIC uses a login call with a JSON body. It then keeps the session in a cookie, so a requests.Session is handy.
session = requests.Session()
login = {"aaaUser": {"attributes": {"name": "admin", "pwd": PASSWORD}}}
session.post(BASE_URL + "/api/aaaLogin.json", json=login)
reply = session.get(BASE_URL + "/api/node/class/fabricNode.json").json()
for item in reply["imdata"]:
attrs = item["fabricNode"]["attributes"]
print(attrs["name"], attrs["role"])
The objects come back under imdata. Each one is wrapped in its class name, and the data sits under attributes.
SD-WAN devices
The manager uses a session login and then an extra token header.
session = requests.Session()
session.post(
BASE_URL + "/j_security_check",
data={"j_username": "admin", "j_password": PASSWORD},
)
token = session.get(BASE_URL + "/dataservice/client/token").text
session.headers["X-XSRF-TOKEN"] = token
devices = session.get(BASE_URL + "/dataservice/device").json()["data"]
for d in devices:
print(d["host-name"], d["system-ip"])
Login uses form data, so the script uses data= and not json=. Newer releases can also use other login methods, so follow the manager's documentation.
NSO devices
NSO exposes its device list through RESTCONF, with basic authentication.
headers = {"Accept": "application/yang-data+json"}
reply = requests.get(
BASE_URL + "/restconf/data/tailf-ncs:devices/device",
headers=headers,
auth=("admin", PASSWORD),
).json()
for d in reply["tailf-ncs:device"]:
print(d["name"], d["address"])
Webex spaces, participants and messages
Webex uses a bearer token. The API calls spaces rooms and participants memberships.
headers = {"Authorization": "Bearer " + TOKEN}
room = requests.post(
BASE_URL + "/v1/rooms", headers=headers, json={"title": "Ops"}
).json()
requests.post(
BASE_URL + "/v1/memberships",
headers=headers,
json={"roomId": room["id"], "personEmail": PERSON_EMAIL},
)
requests.post(
BASE_URL + "/v1/messages",
headers=headers,
json={"roomId": room["id"], "text": "Maintenance at 22:00"},
)
members = requests.get(
BASE_URL + "/v1/memberships",
headers=headers,
params={"roomId": room["id"]},
).json()["items"]
Lists come back under the items key. A GET on /v1/rooms lists spaces, and a GET on /v1/messages with a roomId lists messages.
Spotting the pattern
| Platform | Login style | List key in reply |
|---|---|---|
| Meraki | API key header | The list itself |
| Catalyst Center | Token header | response |
| ACI | Login call and cookie | imdata |
| SD-WAN | Login, cookie and token header | data |
| NSO | Basic authentication | Module prefixed list name |
| Webex | Bearer token | items |
Before you loop, print the whole reply once. The key that holds the list is the part that changes between platforms.
What to remember for the exam
Six facts cover this topic.
- Authenticate first, then call the endpoint, then loop over the list in the reply.
- Meraki lists devices with
/networks/NETWORK_ID/devicesand clients with/networks/NETWORK_ID/clients. - Catalyst Center returns lists under
responseand takes theX-Auth-Tokenheader. - ACI returns objects under
imdata, and its login path is/api/aaaLogin.json. - Webex creates spaces with POST to
/v1/rooms, adds people with POST to/v1/membershipsand sends text with POST to/v1/messages. - Webex uses
roomIdto tie memberships and messages to a space.
Common mistakes
Four mistakes are typical.
Looping over the whole reply. Many platforms wrap the list in a key, so index the key first.
Sending JSON where the login wants form data. SD-WAN login uses data=.
Forgetting the session. ACI and SD-WAN rely on cookies, which a Session keeps.
Mixing up names. Webex spaces are rooms in the API, and participants are memberships.
Print a few fields only. A device reply can carry details that you should not log, so avoid dumping credentials or tokens.
Check yourself
Work through these three questions.
Where does Catalyst Center put the list of devices in its reply?
Under the
responsekey.Which Webex path adds a person to a space?
A POST to
/v1/membershipswith theroomIdand the person.Why does the ACI script use a
requests.Session?ACI keeps the login in a cookie, and a session sends it on later calls.
Topic 8 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.