API Issue

  • 27 October 2020
  • 12 replies
  • 185 views

Userlevel 3

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.


12 replies

Userlevel 4
Badge

Hey sir,

 

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

 


In the coming weeks, we will be releasing updates to the Automox API and introducing API limits to ensure optimum platform performance for our customers. We will begin to roll out changes to our endpoints to begin to support and eventually enforce limits on the number of results returned per request. These endpoints will have a limit parameter available, and this parameter will have both a default value and a maximum limit. The limit argument will allow values between 1-500, with a default of 50…

 

Userlevel 3

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.

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

Userlevel 3

Okay thanks.


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

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
Userlevel 3

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

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

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

Userlevel 3

Thanks guys, seems to pull data.

Will play around with it to tailor to my needs.


Appreciate the assistance.

Userlevel 4
Badge

 


In the coming weeks, we will be releasing updates to the Automox API and introducing API limits to ensure optimum platform performance for our customers. We will begin to roll out changes to our endpoints to begin to support and eventually enforce limits on the number of results returned per request. These endpoints will have a limit parameter available, and this parameter will have both a default value and a maximum limit. The limit argument will allow values between 1-500, with a default of 50…

 

Userlevel 7

I made a clean version of @jarod.smilkstein’s code above and posted it here for anyone that needs to update their scripts with pagination code:
 

 


I’ve taken @jarod.smilkstein’s code from here.
There’s 3 things to change in this code to make it work:

put in your API key
put in your orgID
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 outpu…

 

 

 

 

Reply