0

How to check account API limit when using sendgrid python API?

I need to know the limits and according will tweak my program to avoid hitting the API limits. Probably the limit is 600 calls per minute.

But I want to know the python api endpoint which will retrieve that.

The only documentation I found is: https://sendgrid.com/docs/API_Reference/Web_API_v3/How_To_Use_The_Web_API_v3/rate_limits.html

How do I implement this in python API client?

  • Did you try anything so far? If so, share your code. [This SO question](https://stackoverflow.com/questions/17301938/making-a-request-to-a-restful-api-using-python) has an example of making an API request through python and there are many more examples available if you use Google. – Casper Jul 09 '18 at 08:51
  • 1
    The documentation is very clear about rate limiting. You will find rate limiting quote and information in ```resource``` endpoint response header. ```X-RateLimit-Limit``` means **Number of api calls you can**, ```X-RateLimit-Remaining``` means **remaining api call limit within specific time frame**, ```X-RateLimit-Reset``` means **when rate limiting will be reset to normal**. So, whenever you want to know about rate limit you will have to call ```https://api.sendgrid.com/v3/resource``` endpoint to know that. – Tamim Jul 09 '18 at 09:03

1 Answers1

0
Here is what I use to get usage rates

LOGIN = # paste your login 
PASSWORD = # paste your password g
sg = SendGridAPIClient(PASSWORD, impersonate_subuser = "")
response = sg.client.user.credits.get() today = date.today()
ddate = today.strftime("%Y-%m-%d") 
d = datetime.today() - timedelta(days=n)
ddate2 = d.strftime("%Y-%m-%d") 
tdate=d.strftime("%m-%d-%Y")
params = {'aggregated_by': 'day', 'limit': 1,
          'start_date': ddate2, 'end_date': ddate2, 'offset': 1}
response = sg.client.stats.get(query_params=params)
datas=json.loads(response.body)
tdate = today.strftime("%m-%d-%y") 
print(tdate)
sent=datas[0]['stats'][0]['metrics']['requests']
open=datas[0]['stats'][0]['metrics']['unique_opens']
remain=100-int(sent)
print(f"Emails sent today = {sent}, with {remain} remaining in daily limit")
if sent==0:
    percentage="{:.0%}".format(0)
else:
    percentage = "{:.0%}".format(open/sent)
print(f"Open rate on this date is {percentage} ")
Allen
  • 722
  • 4
  • 13
  • 31