python-3.xrestvirtual-machinenetbox

API call to netbox using python requests doesn't doesn't get all the VM


I need to get all the VM name that I have in the virtualization part in Netbox.

I want to use an API call using requests module in python. I can't use pynetbox module as it doesn't fit my needs at the moment.

I have approximately 5500 VM registered in Netbox. I know it's a lot, and it'll go bigger and bigger in the time.

My issue is that the API call return only 1000 items, not all the item that are registered in Netbox.

Is this a limitation made by netbox's api ? Do you know how I can workaround that limitation ?

Here is the code I have tested so far. Pretty basic:

r = requests.get("https://netbox/api/virtualization/virtual-machines/?limit=10000", headers={"Authorization":"Token "+ token}, verify=False).json()

for vm in r['results']:
    list_vm.append(vm['name'])

print(list_vm)
print(len(list_vm))

Basically, the print(len(list_vm)) return "1000" and not more.

Thank you for your help :)


Solution

  • You didn't mentioned your version of NetBox, but the current documentation for the API states there is a default max limit of 1000, regardless of whether you pass a larger value for the limit parameter.

    The maximum number of objects that can be returned is limited by the MAX_PAGE_SIZE configuration parameter, which is 1000 by default. Setting this to 0 or None will remove the maximum limit. An API consumer can then pass ?limit=0 to retrieve all matching objects with a single request.

    So you have two options:

    1. Use the pagination feature of the API and make multiple requests until there is no more data (recommended).
    2. Change the value of MAX_PAGE_SIZE in your configuration to zero or something higher than 5500.

    See docs: https://docs.netbox.dev/en/stable/integrations/rest-api/#pagination