Skip to main content

Example of API call with pagination limits

  • December 16, 2020
  • 1 reply
  • 728 views

Nic-Automox

I’ve taken @jarod.smilkstein’s code from here.

 

There’s 3 things to change in this code to make it work:

 

 

  1.  
  2. put in your API key
  3. put in your orgID
  4. set the $limit variable to the size you want, with a maximum of 500

     

 

 

This code will pull down all your device info via the https://console.automox.com/api/servers

API call, but you can sub that out for whatever info you’re wanting to pull. The data is saved to a csv, but you can also do manipulation to the $data object in the script first before outputting any data.

 

 

You can use this example code to update any of your existing scripts that were created prior to the API limits.

 

 

$apiKey = 'YOUR_API_KEY'

$orgID = 'YOUR_ORG_ID'

$page = 0

$limit = 5



$data = @()

while($true) {

    $uri = "https://console.automox.com/api/servers?o=$orgID&api_key=$apiKey&l=$limit&p=$page"

    $resp = (Invoke-WebRequest -Method GET -Uri $uri -UseBasicParsing).Content | ConvertFrom-Json | Select-Object results

    $data += $resp.results



    if($resp.results.count -lt $limit) {

        break

    }

    $page += 1

}





$data | Export-Csv -Path .\AutomoxAPI_Export.csv -NoTypeInformation -Force

1 reply

Here’s how I did it in Python: 

 

page = 0
data = []
while page >= 0:
    url = 'https://console.automox.com/api/servers?o=<<o_id>>&limit=500&page=' + str(page)
    r = requests.get(url, headers=automox_headers)
    if r.json() == []:
        break
    data.extend(r.json())
    page = page + 1

 

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings