API Issue

Hi, has anyone had any issues with the API from today?

I had a script that pulled down all machines and was working fine for the last few weeks however stopped this morning with an ‘Invoke-WebRequest : The remote server returned an error: (500) Internal Server Error’.

Script is (XXXX added for privacy):

$apiKey = "XXXXX"
$orgID = "XXXXX"
$expDir = 'XXXXX'
#############################################################################################################

#Pull server detail by group from API and export as csv to defined directory
$uri = "https://console.automox.com/api/servers?o=$orgID&api_key=$apiKey&l=1100"
(Invoke-WebRequest -Method -Uri $uri).Content | ConvertFrom-Json | Select-Object results -ExpandProperty results| Select-Object name, serial_number | Export-Csv -Path $expDir\AutomoxAPI_Export.csv -NoTypeInformation -Force

If I remove the ‘&l=1100’ from the uri variable it works without issue, however only pulls 500 devices.
This only started happening from today.

1 Like

Hey sir,

I believe this was posted recently about the new API limits. You can find the post here.

Thank you that explain it.
So in short to be able to pull 1500 devices you’d have to use a uri of something like this? “https://console.automox.com/api/servers?o=$orgID&api_key=$apiKey&l=500&p=3

&l=500 for the 500 limit
&p=3 for 3 pages
in in effect pulling 1500?

Correct me if I am wrong?

Hey Rob,
You’ll actually have to pull 3 times, with p=0, 1 and 2. You can check the length of the results array in the response and loop over pages until < 500 are returned to know you’ve reached the last page.

1 Like

rmullen,

I tracked down your 500 error in our system and you’ve run into the exact reason we will be enforcing limits in our API starting in December. Because of the complexity of information returned by certain API endpoints and how it is retrieved, large requests have a tendency to cause timeouts or a strain on our infrastructure. The servers endpoint is one of our most problematic ones because it looks at the policies, policy results, events, and recent commands for each device.

Switching to using the limit parameter with a max of 500 should definitely help and is the best path forward.

PB

1 Like

Okay thanks.

Can you please let me know the API script I would use then? I’m a tad confused on that part…

1 Like

I’m a little bit of a powershell hack, but this should work:

$data = @()
while($true) {
    $uri = "https://console.automox.com/api/servers?o=$orgID&api_key=$apiKey&l=500&p=$page"
    $resp = (Invoke-WebRequest -Method GET -Uri $uri).Content | ConvertFrom-Json | Select-Object results
    $data += $resp.results

    if($resp.results.count -lt 500) {
        break
    }
    $page += 1
}

$data | Select-Object name, serial_number | Export-Csv -Path $expDir\AutomoxAPI_Export.csv -NoTypeInformation -Force
1 Like

Still get the same internal server error 500 adding that in.

$apiKey = 'XXXX'
$orgID = 'XXXX'
$expDir = 'c:\temp'
#############################################################################################################

$data = @()
while($true) {
    $uri = "https://console.automox.com/api/servers?o=$orgID&api_key=$apiKey&l=500&p=$page"
    $resp = (Invoke-WebRequest -Method GET -Uri $uri).Content | ConvertFrom-Json | Select-Object results
    $data += $resp.results

    if($resp.results.count -lt 500) {
        break
    }
    $page += 1
}

$data | Select-Object name, serial_number | Export-Csv -Path $expDir\AutomoxAPI_Export.csv -NoTypeInformation -Force
1 Like

Long time listener, first time programmer. Where is the $page variable originally set?

1 Like

Oops, looks like I missed that in the paste. Please put in $page = 0 before the loop

1 Like

Thanks guys, seems to pull data.
Will play around with it to tailor to my needs.

Appreciate the assistance.

1 Like