cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
3733
Views
5
Helpful
4
Replies

DNA Center API 500 record limit

bunjiega
Level 1
Level 1

I am fairly new at API's (coming from route/switch background), but I have DNA Center API calls working, but I only get back a max of 500 records, which is expected from what I see in the documentation. How would we get more records than 500 since I have far more than 500 in my DNAC inventory.

 

I tried this --> https://community.cisco.com/t5/automation-and-analytics/method-to-return-more-than-500-devices-in-api-get-quot-network/td-p/3437814  - which does work, but only when NOT using a filter at the end of the URL.

 

An example is trying to get all of the access-points from DNAC with this URL: 

https://dnac.xxx.org/api/v1/network-device?series=.*Unified.*

This works for only the first 500 - how would I get the next 500?

 

Thank you!!!

1 Accepted Solution

Accepted Solutions

bunjiega
Level 1
Level 1

Did finally find using the filter 'offset=500' gets the next batch!

View solution in original post

4 Replies 4

bunjiega
Level 1
Level 1

Did finally find using the filter 'offset=500' gets the next batch!

Hello!

 

i am having a issue here too.  if i place offset=500 as a filter at the end of my query and i actually get less than 500.  i have over 700 devices....how would i modify the rest api call to get all my inventory items?  

 

Thanks in Advance,

 

def network_device_list(dnac
        token = dnac_login(host, username, password)
        url = f"https://{host}/api/v1/network-device?offset=500"
        headers = {'x-auth-token': token}
        response = requests.get(url, headers=headers, verify=False)
        data = response.json()
       
        return data

 

 

 

Mike

My understanding is that you would need to make 2 calls. 1 without the offset filter and the next one with it. The first call would get the first 500 devices, the second call would start at device 501 (since you are using the offset) and it would get the rest.

 

Here is what I made and it works for me...

import requests 
from requests.auth import HTTPBasicAuth 
from requests.packages.urllib3.exceptions import InsecureRequestWarning #suppresses insecure warning message 
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)   #suppresses insecure warning message 
url = "https://XXXXXXXXXXXXXXXXX/api/system/v1/auth/token" 
headers = {"content-type": "application/json"}
response = requests.post(url, auth=HTTPBasicAuth(username="XXXXXXXXXXXXX", password="XXXXXXXXXXXXX"), headers=headers, verify=False) 
token = response.json()["Token"] 
#print (token) 
headers['x-auth-token'] = token 


def call_with_pagination(): 
    url = "https://XXXXXXXXXXXXXXXX/api/v1/network-device?" 
    my_json_list = [] # this is used to store all the different calls into one list (this is a list of dictionaries) 
    paginate_number = 0 
    network_devices = requests.get(url, headers=headers, verify=False) # make 1st call
    print (f"""Call 1 has { len(network_devices.json()['response']) } records""")
    for ech in network_devices.json()['response']:  # Add received json to my_json_list 
        my_json_list.append(ech)
    #    
    while len(network_devices.json()['response']) == 500:  
        url = "https://XXXXXXXXXXXXXXXXX/api/v1/network-device"   # need to reset the url each time   
        paginate_number += len(network_devices.json()['response']) # Adds to the pagination number so we can get the rest of the devices
        url = url + f"/{paginate_number}/500"  # Adding the pagination filter to the url
        #     
        network_devices = requests.get(url, headers=headers, verify=False) # make another call 
        print (f"""This Call has { len(network_devices.json()['response']) } records""") 
        for ech in network_devices.json()['response']:  # Add received json to my_json_list 
            my_json_list.append(ech) 
    return my_json_list        
   
my_list = call_with_pagination() 
print ("Total number of recieved records: " + str(len(my_list)))
#print(my_list) # uncomment this line to actually print ALL the devices

thanks for the reply Jeremy.  so i also had a heck of a time formatting the output the way i wanted it because i realized the output was a listed of dictionaries.  does your logic create more of this since you are storing the results into another list?

 

-Mike