Search code examples
google-ads-api

How to handle Google Ads API rate limit when calling REST API?


I am using Google Ads REST API to pull Ads data. I am not using client library.

One question, how do you programatically check current API usage when calling requests, so you can stop and wait before continuing? Other APIs like Facebook Marketing API has a header in the result that tells you how much requests you have left, so I could stop and wait. Is there a similar info on Google Ads REST API?

Thank you for reading this.


Solution

  • Ultimately, I tried this method. So far, I haven't reached limit with it:

    The basic developer token for Google Ads API allow 15,000 requests per day as of this answer (Link: https://developers.google.com/google-ads/api/docs/access-levels). So that's 15,000 / 24 = 625 requests every hours. Further divisions show that I can have 625/60 = 10.4 requests every minutes. So 1 request every 6 seconds will ensure I won't reach rate limit.

    So my solution is:

    1. Measure the time it takes to complete a request call and subsequent processing
    2. If total time is over 6 seconds, perform the next request. Else, wait so the total time is 6 seconds, then perform the next request.

    The below code is what I used to perform this. Hope it helps you guys.

    import time
    from math import ceil
    waiting_seconds = 6
    
    
    start_time = time.time()
    
    ###############PERFORM API REQUEST HERE
    
    
    #Measure how long it takes, should be at least 6 secs to be under API limit
    end_time = time.time()
    elapsed = end_time - start_time
                
                
    if elapsed < waiting_seconds:
        remaining = ceil(waiting_seconds - elapsed)
        time.sleep(remaining)