Work around with API throttling in Powershell


I’m querying a hubspot API with invoke-restmethod and it works fine. However since the API has a call limit, some of my scripts are failing. How can I work around this limit?
Initial thought is to add a sleep timer whenever it detects the limit is breaching.

Thanks in advance!

Hey @mhaisekar.sameer15 Welcome to forums :slight_smile: Happy to see you here.

Yes, putting up some milliseconds delay between calls or a delay once detecting the error response of breaching the limit will be a good idea.
are you hitting the API in a loop ?

Hey! :slight_smile:

Yes, hitting in a loop and has multiple loops :wink:


Then a delay on failure would be good enough.

What is the limit, is it 100 calls in 100 secs you can catch the exception in catch block and parse the error, if its based on max hits then wait for some time in and resume.

function CheckAndWait {
 Param($ErrorMessage )
 if($Message -like "*message for max hits*")
 { Start-Sleep -MilliSeconds 500 }
 else {$False}

foreach($Whatever in $WhateverList){
     Invoke-RestMethod .... -ErrorAction Stop
    if( -not (CheckAndWait -ErrorMessage $_.Exception.Message))
       throw $_

PS: Purely untested code :wink: