sendgrid-python
sendgrid-python copied to clipboard
SendGrid Email Activity API query
I am trying to use the SendGrid email activity API to retrieve a list activities from a celery task in python. I'm using the sendgrid library version 6.11.0 (https://pypi.org/project/sendgrid/). I would like the task to run nightly and retrieve activities between the last time the task ran and the current time. It appears that you should be able to use a query parameter that specifies the two dates (https://www.twilio.com/docs/sendgrid/for-developers/sending-email/getting-started-email-activity-api). So I tried the following:
from sendgrid import SendGridAPIClient
sg_client = SendGridAPIClient(self._api_key)
start_time = '2024-06-19T00:00:00Z'
end_time = '2024-06-19T23:59:59Z'
response = sg_client.client.messages.get(query_params={
'limit': 100000,
'last_event_time': 'BETWEEN TIMESTAMP ' + start_time + ' AND TIMESTAMP ' + end_time,
})
This does return messages, but includes messages outside the dates specified. I also tried url encoding the last_event_time value with and without quotes around the start and end times (and many other variations) with no success. On a positive note, I ran the command using Curl and it worked.
Additionally, notice the large limit parameter. It seems that limit is required but there is no offset parameter. I have no idea how many messages exist between the two dates. How can I know if I've retrieved them all? If I ask for 100 and less than 100 are returned I could assume I'm done. If I ask for 100 and get 100 then there are probably more. But without an offset parameter how can I retrieve them? I also did not see the maximum limit documented anywhere.