Course contents

Unit 10: Automation and observabilityLesson 10.1 (1 of 2 in this unit)49 of 63 in the Advanced Cisco SD-WAN course

SD-WAN Manager REST API structure

How the Manager API is built: the session and token, the parts of a call, read and write calls, a first read-only script and what the HTTP status and the body say when a call fails.

Advanced · 16 min read

You'll learn to describe how the Manager REST API is organised, make a first authenticated read call, tell read calls from write calls, and use the HTTP status and response body to diagnose a failing call.

Technical definition

The SD-WAN Manager REST API is the set of HTTPS calls that the Manager exposes for everything its own web interface does. A client logs in, receives a session, sends requests to paths under /dataservice with a method such as GET or POST, and receives JSON. Scripts and automation tools use the same calls, which is why the Manager is the single point of entry to the fabric.

Everything the interface does is a call

The Manager web interface is a client of the Manager API. Anything you can click, such as listing devices, reading alarms or attaching a template, is a request to a path under /dataservice that returns JSON. Automation uses the same calls, so learning the shape of the API also explains what the interface does for you.

Diagram
Life of an API session
Life of an API session

The client logs in and receives a session cookie, fetches a token for calls that change things, sends requests, reads the JSON replies, and logs out.

  1. Log incredentials sent,cookie returned
  2. Tokenfetched forwrite calls
  3. Requestmethod, path,headers, body
  4. Responsestatus codeand JSON
  5. Log outend thesession
The session and the token are separate. The cookie proves who you are; the token protects calls that change the fabric.

The parts of a call

Every call has the same parts. The method says what to do, the path says to what, the headers carry the session and the data format, and a body carries the data for calls that create or change something. Reading a call by its parts makes an unfamiliar one easy to follow.

Diagram
Anatomy of an API call
Anatomy of an API call
PartExampleNotes
MethodGET, POST, PUT, DELETEGET only reads; the others change state
Addresshttps://manager.example.comThe Manager, over HTTPS with a trusted certificate
Path/dataservice/deviceNames the thing; differs between releases
SessionCookie with the session idIssued at login; expires after a period
TokenX-XSRF-TOKEN headerRequired by many releases for write calls
Body and formatJSON with Content-Type: application/jsonOnly for calls that send data
Use the Manager's own API documentation for the exact paths and fields in your release.

Read calls and write calls

A GET call changes nothing, so it is the right place to start and the right tool for inventory and reports. POST, PUT and DELETE calls change the fabric, and a single one can affect many devices, for example by attaching a template or activating a policy. Treat the two groups differently in every script.

Diagram
Read calls and write calls
Read calls and write calls

Read calls use GET and only return data, so they are safe to repeat. Write calls use POST, PUT or DELETE and change the fabric, so they need a lab test, a backup and a check of the response.

Read (GET)Returns data onlySafe to repeatStart here
Write (POST, PUT, DELETE)Changes the fabricCan reach many devicesTest in a lab first
Scripts should be written read-first. Add a write call only when its effect is understood.
Diagram
Before the first write call
Before the first write call

Read the current state, test the call on a lab Manager, take a backup, send one call, and check the response and the result.

  1. Readcurrent statesaved to a file
  2. Labsame call on alab Manager
  3. BackupManager backupand a rollback
  4. One calla single call,not a loop
  5. Checkstatus, bodyand the result
Each step is cheap compared with undoing a change across many devices.

A first script: log in and read the inventory

Three steps are enough for a first read. Log in and keep the session cookie, fetch a token if your release uses one, and call the device inventory. The commands below keep the credentials in environment variables rather than in the command or the file, and the address is a placeholder.

curl -s -c cookies.txt -X POST https://manager.example.com/j_security_check -d "j_username=$SDWAN_USER" -d "j_password=$SDWAN_PASS"

Logs in and stores the session cookie in cookies.txt. Use a trusted certificate on the Manager; an option that skips certificate checks is for a lab only. Check the release's guide, since the login path and the cookie handling can change.

curl -s -b cookies.txt https://manager.example.com/dataservice/client/token

Fetches the token that many releases require in the X-XSRF-TOKEN header for write calls. A read call does not need it, so skip this step for a read-only script.

curl -s -b cookies.txt https://manager.example.com/dataservice/device

Reads the device inventory as JSON. Pipe it through a JSON formatter to read it, and keep the file as a record of the fabric on that date.

Example output written for this lesson, simplified; real responses have many more fields and differ between releases
$curl -s -b cookies.txt https://manager.example.com/dataservice/device
{
  "data": [
    {
      "host-name": "BR-01",
      "system-ip": "10.255.1.11",
      "reachability": "reachable"
    },
    {
      "host-name": "BR-02",
      "system-ip": "10.255.1.12",
      "reachability": "unreachable"
    }
  ]
}
The reply is a JSON object with a data list, one entry per device. A short script can walk the list and report the devices whose reachability is not reachable. Here BR-02 is the one to look at, which makes this call a cheap health check that can run on a schedule.
Diagram
Requests per hour: one inventory call or one call per device
Requests per hour: one inventory call or one call per device
DevicesOne inventory call every 5 minutesOne call per device every 5 minutesExtra requests
5012600588
200122,4002,388
500126,0005,988
Worked out by script: 12 polls an hour, times the number of devices for the per-device pattern. The Manager may limit request rates, so read the release guide. Prefer a call that returns every device in one reply.
Diagram
What the inventory read feeds
What the inventory read feeds

One authenticated inventory read can feed a reachability report, a software version report and a record of sites and system addresses.

Inventory read
  • Reachabilitydevices thatare not up
  • Versionssoftware bydevice
  • Sitesnames, IDs andsystem IPs
  • Recorddated filefor audits
Read-only reports are the safest first use of the API and are useful from the first day.

A worked case: status 200 and no JSON

A new script reports success, yet the report is empty. The device call returned status 200, so nothing looks wrong. The response body is the clue.

Example output written for this lesson, simplified; real responses have many more fields and differ between releases
$curl -s -b cookies.txt https://manager.example.com/dataservice/device | head -c 120
<html><head><title>Login</title></head><body><form action="/j_security_check" method="post">
The body is the login page, not JSON. The session cookie was not accepted, either because the login failed with a wrong password or because the session expired. The status was 200 because the login page itself was served successfully.
Diagram
Evidence from the case
Evidence from the case

The call returned status 200, the body is a login page, the cookie file holds no valid session, and the login step had been checked only by its status.

  1. Status200, which looks fineA login page is also a page
  2. BodyHTML login formNot the JSON that was expected
  3. SessionCookie rejected or expiredNo valid session to reuse
  4. FixCheck the login reply, not only its statusLog in again when a reply is not JSON
A status code alone does not prove a call worked. Check the content type or the first characters of the body.

Reading the HTTP status

Diagram
Status codes and the first check
Status codes and the first check
StatusMeaningFirst check
200The request was servedIs the body JSON? A login page is also served with 200
401 or 403Not authenticated, or not allowedSession cookie, token header and the account's role
404No such pathThe path for your release in the Manager's API documentation
400The request body or a field is wrongJSON syntax, field names and the Content-Type header
429 or 5xxToo many requests, or the Manager is busySlow the script, wait and retry with a delay
Handle every non-200 status in a script. A script that ignores errors can keep running on stale or empty data.
Diagram
A retry schedule that doubles the wait (example)
A retry schedule that doubles the wait (example)
RetryWait before itTotal waited so far
11 second1 second
22 seconds3 seconds
34 seconds7 seconds
48 seconds15 seconds
516 seconds31 seconds
Worked out by script. Doubling the wait gives a busy Manager room to recover, and a fixed number of retries stops a script from hammering it. The values are an example, and some releases state their own limits. Stop after the last retry and report the failure.

Learn more: SD-WAN Manager WorkflowsBackup and Lifecycle ManagementSD-WAN Root-Cause Framework

Test yourself

Predict · scenario 1

A script calls the device inventory, gets status 200, and the body starts with an HTML login form. What is wrong?

Predict · scenario 2

Which call is the safest first step for a new script?

Predict · scenario 3

Where should the account password for a script be kept?

Predict · scenario 4

A call returns 404. What is the first check?

Predict · scenario 5

A script polls 200 devices with one call per device every 5 minutes. How many requests does it send in an hour?

Predict · scenario 6

A script retries after a 429, waiting 1 second, then 2, 4, 8 and 16. How long has it waited in total after the fifth retry?

Predict · scenario 7

What comes first before a script sends a write call to a production Manager?

FAQ

Do scripts talk to the edges directly?
No. Scripts call the Manager, and the Manager holds the inventory, templates, policies and monitoring data for the whole fabric. One authenticated entry point is easier to control and to audit than a connection to every router.
Where is the list of calls for my release?
The Manager publishes its own API documentation for the release it runs, normally under the /apidocs path of the Manager address. It lists the paths, methods and fields, so it is more reliable than a call copied from an older example.
Why does a failed login return a normal-looking response?
On many releases the login endpoint answers with a login page and an HTTP 200 when the credentials are wrong, instead of an error status. A script that checks only the status code believes it is logged in, so check that the response is JSON or that a session cookie was issued.
Is it safe to try write calls against the production Manager?
Start with read calls, and test write calls on a lab Manager. A write call can attach a template or change a policy for many devices at once, so run it only after you know what it changes and have a backup and a rollback ready.