published

get-started-dna-center-api

Example code on how to perform API calls to Cisco DNA Center using Python 3 and Postman.
The files included on this repository contain:

  • Python 3 example code on how to generate the necessary JWT and subsequently do API calls to Cisco DNA Center. This would be the base code for any Python 3 script that would integrate with DNA Center.

  • Postman collection to interact with DNA Center (ready to use with sandboxdnac.cisco.com!). One file for the JWT generation and examples of API calls.

Instructions

The script should be run on the terminal on a device that has python3 and the required python packages installed.
You can find the packages needed listed on the file requirement.txt
The Postman collection requires Postman application to be installed on the computer where the API calls are to be performed, You can download the application at: https://www.postman.com/

Setting Up to Run the script

Clone and Prep the Environment
Clone the code repo
git clone https://github.com/LeCoderCat/get-started-dna-center-api.git
cd get-started-dna-center-api

Setup Python Virtual Environment

1. MacOS or Linux
python3.6 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
    > Note: If on Linux, you will need to install the Python3.6 development files. On CentOS this is done with yum install -y python36u-devel
2. Windows - recommendation to use git-bash terminal
py -3 -m venv venv
source venv/Scripts/activate
pip install -r requirements-win.txt
    > Note: Creation and activation of a venv in Windows is slightly different.

Infrastructure Resources needed to run the script

In order to have access to Cisco DNA Center platform, you can use one of sandboxes available at Cisco's DEVNET site. These sandboxes are preconfigured labs with all resources dedicated to you for the duration of the reservation. You have the ability to install your own application and share the lab with other users.

To start using the content of this repo on Cisco DNA Center sandboxes, click here.

User: devnetuser
Password: Cisco123!

Or to directly test API calls on DNA Center, click here

How to run the script

Here you can find examples of the output of the script running. After running, insert the needed data in order to first retrieve the JWT and then to perform API call. Every input stream is followed by text that will have a suggestion on what field is required and an example of what can be input in the field. The script is able run several API calls until interrupted.

API_DNAC.py

MAC:ssh_scripts user$ python3 API_DNAC.py
Enter the IP of the DNAC (10.48.90.165): 10.48.90.165
Enter the username of the DNAC GUI (admin): admin	
Enter the DNAC GUI password (cisco!123): 
Enter the API [GET]URL (dna/intent/api/v1/topology/site-topology): dna/intent/api/v1/site
Status Code is: 200
Success!
Response is:
 {
    "response": [
        {
            "additionalInfo": [],
            "id": "c06f98d8-e8f1-4da5-9666-8610d38b1e48",
            "instanceTenantId": "SYS0",
            "name": "Global",
            "siteHierarchy": "c06f98d8-e8f1-4da5-9666-8610d38b1e48",
            "siteNameHierarchy": "Global"
        }
    ]
}
MAC:ssh_scripts user$ 

Postman collection

This collection by default queries sandboxdnac.cisco.com. In that case, DNA Center PKI is not natively recognized by the station where Postman is installed. To be able to interacti wht DNA Center API, please disable the certificate verification on Postman's settings:

Postman settings

After that, do an API call to generate the JWT (token) to authenticate (using GUI credentials, on the Authorization tab next to Params) and perform API calls:

Postman Token Gen

Use the generated token to perform API calls:

Postman API call

View code on GitHub
  • Owner

  • Contributors

    +2Github contributors
  • Categories

  • Products

    Catalyst Center
  • Programming Languages

    Python
  • License

    BSD 3-Clause "New" or "Revised" License

Code Exchange Community

Get help, share code, and collaborate with other developers in the Code Exchange community.View Community
Disclaimer:
Cisco provides Code Exchange for convenience and informational purposes only, with no support of any kind. This page contains information and links from third-party websites that are governed by their own separate terms. Reference to a project or contributor on this page does not imply any affiliation with or endorsement by Cisco.