AutomoxAutomox

Example of API call with pagination limits

Nic-Automox
December 16, 2020 at 7:59 pm UTC

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.

 

 

powershell37 lines
$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

This discussion is archived and closed to replies. Continue it in the Automox Community Slack.

Discuss on Slack

Replies (1)

Dale_CourtneyMarch 23, 2022
#1

Here’s how I did it in Python: 

 

9 lines
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