Skip to main content

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.

Westyy
Forum|alt.badge.img
  • Pro
  • October 27, 2020

Hey sir,

 

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

 

https://community.automox.com/product-updates-4/coming-soon-api-limits-1495


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…

 


  • Novice
  • October 27, 2020

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?


Jarod-Automox

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.


PaulB
  • Former Automox Employee
  • October 27, 2020

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


  • Novice
  • October 27, 2020

Okay thanks.


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


Jarod-Automox

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


  • Novice
  • October 27, 2020

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

PaulB
  • Former Automox Employee
  • October 27, 2020

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


Jarod-Automox

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


  • Novice
  • October 27, 2020

Thanks guys, seems to pull data.

Will play around with it to tailor to my needs.


Appreciate the assistance.


Forum|alt.badge.img
  • Power User
  • October 29, 2020
https://community.automox.com/product-updates-4/coming-soon-api-limits-1495

 


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…

 


Nic-Automox
  • Former Automox Employee
  • December 16, 2020

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:
 

https://community.automox.com/automox-labs-8/example-of-api-call-with-pagination-limits-829

 


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


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