DNA Center SDK Documentation
DNA Center’s Python SDK is an easy-to-use solution that relieves you of
routine coding tasks regarding API authentication, data encoding,
pagination, rate limiting, and error handling. The SDK is community
supported and is shared from the PyPI project or GitHub from the
following URLs:
[Link]
[Link]
Understanding the foundational API resources is important and useful. It
is simplified in using tools like Postman; however, you will find that the
SDK provides even more benefits. Because the documentation is extensive
and accessible, SDK use is encouraged. You can find the authoritative docs
for the DNA Center SDK at [Link]
You can find additional helpful guidance on DevNet at
[Link]
pip-install.
For purposes of this example, we consider the latest version, 2.4.1,
released December 1, 2021. To get started, set up your Python environment
to a minimum of Python v3.6 (released December 2016). As of Fall 2021,
Python 3.10 is the latest release. Using a recent release is recommended,
but if you have a current environment at version 3.6 or higher, that is
sufficient. Also, consider using a virtual environment (venv) when setting
up your project.
The quick install method uses the PyPi project to obtain the dnacentersdk
package, as shown in Example 16-36.
Example 16-36 Creating a Cisco DNA Center SDK Project
Click here to view code image
myserver Python % mkdir MyDNACProject
myserver Python % cd MyDNACProject
myserver MyDNACProject % python3 -m venv .venv
myserver MyDNACProject % source .venv/bin/activate
(.venv) myserver MyDNACProject % pip install dnacentersdk
Collecting dnacentersdk
Downloading [Link] (2.4 MB)
2.4 MB 2.7 MB/s
Collecting requests<3.0.0,>=2.25.1
Using cached [Link] (62 kB)
Collecting requests-toolbelt<0.10.0,>=0.9.1
Downloading requests_toolbelt-[Link]
(54 kB)
54 kB 9.6 MB/s
Collecting fastjsonschema<3.0.0,>=2.14.5
Downloading [Link] (21 kB)
Collecting future<0.19.0,>=0.18.2
Downloading [Link] (829 kB)
829 kB 4.3 MB/s
Collecting idna<4,>=2.5
Using cached [Link] (61 kB)
Collecting urllib3<1.27,>=1.21.1
Using cached [Link] (138 kB)
Collecting certifi>=2017.4.17
Using cached [Link] (149
kB)
Collecting charset-normalizer~=2.0.0
Downloading charset_normalizer-[Link] (39
kB)
Using legacy '[Link] install' for future, since package
'wheel' is not installed.
Installing collected packages: urllib3, idna, charsetnormalizer,
certifi, requests,
requests-toolbelt, future, fastjsonschema, dnacentersdk
Running [Link] install for future ... done
Successfully installed certifi-2021.10.8 charset-normalizer-
2.0.9 dnacentersdk-2.4.1
fastjsonschema-2.15.1 future-0.18.2 idna-3.3 requests-2.26.0
requests-toolbelt-0.9.1
urllib3-1.26.7
WA
Now your environment is ready for the follow-on SDK examples. The next
section addresses authorization. If you haven’t already covered the DNA
Center REST API enablement steps, go back to the “Enabling API/SDK
Access to DNA Center” section. You must ensure that the feature is
enabled and that you have a user (with password) defined.
SDK Authorization
The DNA Center SDK enables you to obtain the username and password
credentials via environment variables, which is recommended over putting
your credentials inside your Python script. For example, if you’re using
the DevNet Always On Sandbox Lab for DNA Center, these would be the
applicable environment variables:
Click here to view code image
export DNA_CENTER_USERNAME=devnetuser
export DNA_CENTER_PASSWORD=Cisco123!
export DNA_CENTER_BASE_URL=[Link]
Now you can build a sample Python script that uses the SDK. You can
reference the API docs from the community portal to see the various
functions and options. At the time of this writing, this link refers to the
latest API information:
[Link]
-v2-2-3-3
Navigating down to the Devices section would be a safe approach and
would result in practical information you might want to obtain for putting
into an asset management system. Specifically, you can use the
devices.get_device_list() method for this first approach. Now, create the
Python script in Example 16-37 as [Link] in your virtual
environment.