Skip to content

Instantly share code, notes, and snippets.

@emiel
Last active April 11, 2018 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emiel/99e5c103dfffaf05629ca305ff546c18 to your computer and use it in GitHub Desktop.
Save emiel/99e5c103dfffaf05629ca305ff546c18 to your computer and use it in GitHub Desktop.
Decode SendGrid Event ID in Python
def decode_eid(eid):
"""
Decodes a SendGrid event id (sg_event_id) and returns as uuid
"""
eid_len = len(eid)
if eid_len == 22:
res = uuid.UUID(bytes=base64.urlsafe_b64decode(eid + "=="))
elif eid_len == 48:
res = uuid.UUID(base64.urlsafe_b64decode(eid).decode("ascii"))
else:
raise Exception("Unsupported sendgrid event id: {}".format(eid))
assert res.variant == uuid.RFC_4122 and res.version == 4
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment