Intro

A few weeks ago I delivered an Ask The Experts session at my work about using postman with Cisco DNA Center. The session presented how to get a token from DNAC to start consuming APIs.

When working with APIs in DNAC the first you need to do is to authenticate yourself. You send your credentials to DNAC and DNAC will send you back a token.

This token has to be used every time you want to do a request to DNAC, hence you can see that using variables in Postman is quite handy when testing APIs.

postman variables

You can find how to use variables in general at the postman documentation for variables.

Generally speaking variables are defined as {{ variable_name }}, where the content that is inside the double brackets will be replaced by the content of the variable.

You can use variables directly on the URL of your APIs. For example in the case of my session, I defined my API as:

DNAC API
https://{{dnac}}/dna/system/api/v1/auth/token

postman environments

To start using variables, you need environments, this are super handy if you calls different systems, for example in the API call above, I can re-use the same call for multiple DNAC, since all of them expose the same APIs, the only part that changes are credentials.

Take a look at the postman documentation for environments since that's the best place to start learning.

how to dynamically update variables

Now the interesting part. Initially you set the variables in your environment manually, which is good when you want to re-use the same values, like IP address, but you can also update the variables in your environment in an automatic way from the answer of your API calls.

In my example, this means that I can save, the token DNAC sent in response to my authentication request, on an environment variable, and I can I use this token on the subsequently requests. This is super useful, otherwise I need to copy and paste the token on those requests. Which may not be an issue on two or five requests, but what if you need twenty, a hundred, then you can see the value.

The magic code to do it is this one:

Postman script
let jsonData = JSON.parse(responseBody);
pm.environment.set("token", jsonData.Token);
console.log("this is the DNAC token: ", jsonData.token);

This code is added under a Test on postman for your individual API call.

On this example, first we are defining a javascript variable called jsonData. This jsonData variable gets the response body of my API call in a json format.

Then I'm saving the content jsonData.Token variable into a environmental variable call token. You must define this variable previously on your environment.

Finally, this token variable can reference by any other API request on your environment using {{ token }} in our example.

If you are wondering from where this jsonData.Token variable comes, this is part of the response body from our authentication request to the DNAC. So, basically is a key, after we formatted the response to json. This means you should look at your response body to know how your json will look like and what keys and values you can expect to use.

If you are more interested in examples on how to use this snapshot code, take a look at this code I put together, you can import this json config directly to postman as a file or as a raw text, so you can play around.