Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

v3 API Python Code Example


(information)

Info

We recommend using SendGrid Python, our client library, available on GitHub(link takes you to an external page), with full documentation.

(information)

Info

Do you have an API Key(link takes you to an external page) yet? If not, go get one. You're going to need it to integrate!


Using SendGrid's Python Library

using-sendgrids-python-library page anchor

The following example sends a single email with the Twilio SendGrid Python library. Set your API key in the SENDGRID_API_KEY environment variable, then build and send the message.

Send an email with the Twilio SendGrid Python library

send-an-email-with-the-twilio-sendgrid-python-library page anchor
1
# using SendGrid's Python Library
2
# https://github.com/sendgrid/sendgrid-python
3
import os
4
from sendgrid import SendGridAPIClient
5
from sendgrid.helpers.mail import Mail
6
7
message = Mail(
8
from_email='from_email@example.com',
9
to_emails='to@example.com',
10
subject='Sending with Twilio SendGrid is Fun',
11
html_content='<strong>and easy to do anywhere, even with Python</strong>')
12
try:
13
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
14
response = sg.send(message)
15
print(response.status_code)
16
print(response.body)
17
print(response.headers)
18
except Exception as e:
19
print(e.message)