Skip to content

Instantly share code, notes, and snippets.

@kpx-dev
Created November 21, 2014 04:46
Show Gist options
  • Save kpx-dev/9d0af4b48d167a9b675c to your computer and use it in GitHub Desktop.
Save kpx-dev/9d0af4b48d167a9b675c to your computer and use it in GitHub Desktop.
"""
This Python code snippet will show you how to get all SendGrid invalid
emails records.
install the amazing requests library
$ pip install requests==2.4.3
export your SendGrid credential from command line
$ export SG_USERNAME=your_sg_username
$ export SG_PASSWORD=your_sg_password
save this file as python example.py and run it
$ python example.py
gist link:
"""
import requests
import os
SG_URL = "https://api.sendgrid.com/api"
def build_url(endpoint, params=None):
url = "{}/{}.json?api_user={}&api_key={}".format(
SG_URL,
endpoint,
os.environ['SG_USERNAME'],
os.environ['SG_PASSWORD'])
if params:
for k, v in params.iteritems():
url += "&{}={}".format(k, v)
return url
if __name__ == '__main__':
print 'get all invalid emails...'
url = build_url("invalidemails.get")
res = requests.get(url)
print res.json()
print 'get all invalid emails count...'
url = build_url("invalidemails.count")
res = requests.get(url)
print res.json()
print 'delete an invalid email...'
url = build_url("invalidemails.delete")
data = {"email": "example@example.com"}
res = requests.post(url, data)
print res.json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment