Automation and Assurance · Topic 1 of 7

Programmable APIs and Reading a REST Script

Compare the programmable APIs for Cisco devices, such as NETCONF, RESTCONF, gNMI and REST, and learn to read a Python script that configures a device.

  • 5 min
In a nutshell
  • NETCONF uses SSH port 830 and XML. RESTCONF uses HTTPS with JSON or XML. gNMI uses gRPC.
  • NETCONF, RESTCONF and gNMI are model driven and use YANG.
  • GET reads, POST creates, PUT replaces, PATCH changes part, DELETE removes.

What it is

A programmable API lets software configure and read a device, instead of a person typing commands.

Cisco devices offer several APIs. Some work with data models, and some are plain REST calls. A script is a program that sends requests to one of these APIs, and you must be able to read it and say what it does.

ScriptPython programHTTPS requestDevice APIRESTCONF or RESTchangesConfigurationrunning config

How it works

First the main APIs, then the parts of a REST request, then a worked script.

The main APIs

APITransportData formatNotes
CLI and SNMPSSH, UDPText, MIBThe legacy methods
NETCONFSSH, port 830XMLModel driven, uses YANG
RESTCONFHTTPSJSON or XMLModel driven, uses HTTP methods
gNMIgRPC over HTTP/2Protocol buffersUsed for configuration and telemetry
REST APIHTTPSUsually JSONProduct specific, such as a controller

NETCONF, RESTCONF and gNMI are model driven. They use YANG models to describe the data, so a script can ask for exactly what it needs. The next pages cover each one in detail.

REST basics

REST is an HTTP based style. Each thing you manage is a resource with its own URI, and the HTTP method says what to do. The server keeps no memory between requests, so every request carries its own credentials.

MethodMeaning
GETRead a resource
POSTCreate a resource
PUTReplace a resource
PATCHChange part of a resource
DELETERemove a resource

The response has a status code. The common ones are 200 for success, 201 for created, 204 for success with no content, 400 for a bad request, 401 for failed authentication, 403 for no permission, 404 for not found and 409 for a conflict.

Reading a script

When you read a script, find five things: the target, the credentials, the headers, the body and the result handling. This script creates a loopback on an IOS XE device using RESTCONF.

import requests
from urllib.parse import urlunsplit

host = "192.0.2.1"
path = "/restconf/data/Cisco-IOS-XE-native:native/interface"
url = urlunsplit(("https", host, path, "", ""))

headers = {
    "Accept": "application/yang-data+json",
    "Content-Type": "application/yang-data+json",
}

body = {
    "Cisco-IOS-XE-native:Loopback": {
        "name": 100,
        "description": "created by script",
        "ip": {
            "address": {
                "primary": {
                    "address": "10.255.0.1",
                    "mask": "255.255.255.255",
                }
            }
        },
    }
}

response = requests.post(
    url,
    headers=headers,
    json=body,
    auth=("admin", "PASSWORD"),
    verify=False,
)
print(response.status_code)

Read it step by step:

  • The URL is built from the host and a RESTCONF path. The path walks down the YANG model, to the interface container.
  • The headers say that the request and the response both use JSON in the YANG data format.
  • The body names a new Loopback list entry, number 100, with an address and mask.
  • requests.post creates the entry, because POST means create.
  • auth sends the user name and password, and verify=False turns off certificate checking, which is acceptable only in a lab.
  • A status code of 201 means the loopback was created. A 409 would mean it already exists.

The result on the device is the same as typing these commands:

Router(config)# interface Loopback100
Router(config-if)# description created by script
Router(config-if)# ip address 10.255.0.1 255.255.255.255

What to look for in other scripts

Change the method, and the meaning changes. A GET with the same URL and no body would read the interface list.

A PUT or PATCH to a specific entry would replace or change it. A DELETE to that entry would remove it.

Also look for a loop over many devices, which is how automation scales, and for error handling that checks the status code before moving on.

What to remember for the exam

Seven facts cover most questions on this topic.

  • NETCONF uses SSH port 830 and XML. RESTCONF uses HTTPS with JSON or XML. gNMI uses gRPC.
  • NETCONF, RESTCONF and gNMI are model driven and use YANG.
  • GET reads, POST creates, PUT replaces, PATCH changes part, DELETE removes.
  • 200 means success, 201 means created, 401 means authentication failed, 404 means not found.
  • REST is stateless, so each request carries its own credentials.
  • In a script, find the URL, credentials, headers, body and status handling.
  • verify=False skips certificate checks and is only for labs.
Exam tip

The HTTP method tells you the action before you read anything else. Look at it first.

Common mistakes

These four mistakes appear often.

Using POST to change an existing item. POST creates, and it can return a conflict if the entry exists.

Forgetting the content type header. The device may reject the body or read it in the wrong format.

Ignoring the status code. A script that does not check it can report success when the change failed.

Leaving certificate checks off in production. That exposes the credentials to interception.

Watch out

Do not put real passwords into scripts. Read them from the environment or a secrets store.

Check yourself

Work through these three questions.

  • Which HTTP method creates a new resource?

    POST creates a new resource, and a status code of 201 means it was created.

  • Why does a REST request carry credentials every time?

    REST is stateless, so the server keeps no memory between requests.

  • What does verify=False do in a Python requests call?

    It turns off certificate checking, which is only acceptable in a lab.

Topic 1 of 7 in Automation and Assurance

Test yourself on Automation and Assurance

The full Cisco CCNP 350-501 SPCOR question bank has 738 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.