SendGrid Python Library http://sendgrid.com
  • Python 99.6%
  • Makefile 0.2%
Find a file
Thomas Wodarek ef7267afa4
Merge pull request #1 from twodarek/dependabot/pip/flask-3.1.3
chore(deps): bump flask from 1.1.4 to 3.1.3
2026-05-05 23:53:14 -04:00
.github/workflows chore: use make-test instead of make test-docker (#1117) 2025-09-11 13:20:25 +05:30
examples feat: geolocation setter in sendgrid-python for GDPR compliance (#1073) 2023-11-24 00:29:36 +05:30
proposals chore: update GitHub branch references to use HEAD (#929) 2020-08-11 10:29:40 -07:00
sendgrid Release 6.12.5 2025-09-19 11:48:40 +05:30
static/img docs: clean up and reconcile first timers documentation 2020-08-18 10:11:13 -05:00
test chore(deps): bump flask from 1.1.4 to 3.1.3 2026-05-06 03:06:26 +00:00
use_cases feat: Add reply_to_list functionality (#1062) 2023-03-21 13:28:49 -05:00
.coveragerc coverage set up for tox 2017-10-01 14:41:57 +02:00
.env_sample Add a .env_sample file and Update README 2017-10-27 09:10:40 +07:00
.gitignore feat: allow interactive docker testing with 'command' env var 2020-05-29 09:27:39 -05:00
app.json chore: update GitHub branch references to use HEAD (#929) 2020-08-11 10:29:40 -07:00
CHANGELOG.md [Librarian] Version Bump 2025-09-19 11:47:28 +05:30
cleanup.sh Version Bump v3.0.0: full v3 Web API support 2016-06-13 14:20:30 -07:00
CODE_OF_CONDUCT.md docs: baseline all the templated markdown docs (#865) 2020-01-17 16:10:52 -06:00
CONTRIBUTING.md fix: #1108 - Replace ecdsa with cryptography (#1114) 2025-09-11 13:27:07 +05:30
Dockerfile fix: migrate to common prism setup (#888) 2020-05-05 11:37:16 -05:00
FIRST_TIMERS.md chore: drop the issue links from FIRST_TIMERS doc 2022-05-12 10:28:26 -05:00
LICENSE chore: install docker-compose (#1099) 2025-04-11 15:00:51 +05:30
live_test.py docs: cleanup support/help/contact information 2020-04-08 16:19:24 -05:00
Makefile feat: add support for python3.12 and 3.13 (#1087) 2025-04-15 20:35:45 +05:30
MANIFEST.in chore: update license references 2020-10-27 16:42:33 -05:00
Procfile Heroku adjustments 2016-08-16 15:10:58 -07:00
PULL_REQUEST_TEMPLATE.md Adding misc as PR type (#1058) 2022-07-06 16:14:12 -07:00
README.md fix: #1108 - Replace ecdsa with cryptography (#1114) 2025-09-11 13:27:07 +05:30
README.rst fix: #1108 - Replace ecdsa with cryptography (#1114) 2025-09-11 13:27:07 +05:30
requirements.txt chore(deps): bump flask from 1.1.4 to 3.1.3 2026-05-06 03:06:26 +00:00
setup.py fix: #1108 - Replace ecdsa with cryptography (#1114) 2025-09-11 13:27:07 +05:30
test.csv Add small logo 2019-04-18 09:08:17 -07:00
tox.ini feat: add support for python3.12 and 3.13 (#1087) 2025-04-15 20:35:45 +05:30
TROUBLESHOOTING.md docs: improve signed event webhook validation docs (#1013) 2021-10-08 06:51:53 -07:00
twilio_sendgrid_logo.png Add logo file 2019-04-05 20:35:47 -07:00
twilio_sendgrid_logo_small.png Add small logo 2019-04-18 09:08:17 -07:00
USAGE.md chore: update GitHub branch references to use HEAD (#929) 2020-08-11 10:29:40 -07:00

SendGrid Logo

BuildStatus Docker Badge MIT licensed Twitter Follow GitHub contributors Open Source Helpers

This library allows you to quickly and easily use the SendGrid Web API v3 via Python.

Version 3.X.X+ of this library provides full support for all SendGrid Web API v3 endpoints, including the new v3 /mail/send.

This library represents the beginning of a new path for SendGrid. We want this library to be community driven and SendGrid led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create issues and pull requests or simply upvote or comment on existing issues or pull requests.

If you need help using SendGrid, please check the Twilio SendGrid Support Help Center.

Please browse the rest of this README for further detail.

Table of Contents

Installation

Prerequisites

  • Python version 2.7+
  • The SendGrid service, starting at the free level

Setup Environment Variables

Mac

Update the development environment with your SENDGRID_API_KEY (more info here), for example:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

SendGrid also supports local environment file .env. Copy or rename .env_sample into .env and update SENDGRID_API_KEY with your key.

Windows

Temporarily set the environment variable(accessible only during the current cli session):

set SENDGRID_API_KEY=YOUR_API_KEY

Permanently set the environment variable(accessible in all subsequent cli sessions):

setx SENDGRID_API_KEY "YOUR_API_KEY"

Install Package

pip install sendgrid

Dependencies

Quick Start

Hello Email

The following is the minimum needed code to send an email with the /mail/send Helper (here is a full example):

With Mail Helper Class

import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = To("test@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)

The Mail constructor creates a personalization object for you. Here is an example of how to add it.

Without Mail Helper Class

The following is the minimum needed code to send an email without the /mail/send Helper (here is a full example):

import sendgrid
import os

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
data = {
  "personalizations": [
    {
      "to": [
        {
          "email": "test@example.com"
        }
      ],
      "subject": "Sending with SendGrid is Fun"
    }
  ],
  "from": {
    "email": "test@example.com"
  },
  "content": [
    {
      "type": "text/plain",
      "value": "and easy to do anywhere, even with Python"
    }
  ]
}
response = sg.client.mail.send.post(request_body=data)
print(response.status_code)
print(response.body)
print(response.headers)

General v3 Web API Usage (With Fluent Interface)

import sendgrid
import os

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
response = sg.client.suppression.bounces.get()
print(response.status_code)
print(response.body)
print(response.headers)

General v3 Web API Usage (Without Fluent Interface)

import sendgrid
import os

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
response = sg.client._("suppression/bounces").get()
print(response.status_code)
print(response.body)
print(response.headers)

Processing Inbound Email

Please see our helper for utilizing our Inbound Parse webhook.

Usage

Use Cases

Examples of common API use cases, such as how to send an email with a transactional template.

Announcements

All updates to this library are documented in our CHANGELOG and releases.

How to Contribute

We encourage contribution to our libraries (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

Quick links:

Troubleshooting

Please see our troubleshooting guide for common library issues.

About

sendgrid-python is maintained and funded by Twilio SendGrid, Inc. The names and logos for sendgrid-python are trademarks of Twilio SendGrid, Inc.

Support

If you need support, please check the Twilio SendGrid Support Help Center.

License

The MIT License (MIT)