Awesome Open Source
Awesome Open Source

.. image:: :target: :width: 200px

Sign up_ for a SparkPost account and visit our Developer Hub_ for even more content.

.. _Sign up: .. _Developer Hub:

SparkPost Python API client

.. image:: :target: :alt: Build Status

.. image:: :target: :alt: Documentation Status

.. image:: :target: :alt: Coverage Status

The official Python package for using the SparkPost API.


  • Documentation for python-sparkpost_
  • SparkPost API Reference_

.. _python-sparkpost: .. _SparkPost API Reference:


Install from PyPI using pip_:

.. code-block:: bash

$ pip install sparkpost

.. _pip:

.. _pip:

Python 2.7 or later is required.

Get a key

Go to API & SMTP_ in the SparkPost app and create an API key. We recommend using the SPARKPOST_API_KEY environment variable:

.. code-block:: python

from sparkpost import SparkPost
sp = SparkPost() # uses environment variable

Alternatively, you can pass the API key to the SparkPost class:

.. code-block:: python

from sparkpost import SparkPost
sp = SparkPost('YOUR API KEY')

For SparkPost EU and Enterprise accounts, pass in a second parameter to set the API host.

.. code-block:: python

from sparkpost import SparkPost
sp = SparkPost('YOUR API KEY', '')

.. _API & SMTP:

Send a message

Here at SparkPost, our messages are known as transmissions. Let's use the underlying transmissions API_ to send a friendly test message:

.. code-block:: python

from sparkpost import SparkPost

sp = SparkPost()

response = sp.transmissions.send(
    recipients=['[email protected]'],
    html='<p>Hello world</p>',
    from_email='[email protected]',
    subject='Hello from python-sparkpost'

# outputs {u'total_accepted_recipients': 1, u'id': u'47960765679942446', u'total_rejected_recipients': 0}

.. _transmissions API:

Django Integration

The SparkPost python library comes with an email backend for Django. Put the following configuration in file.

.. code-block:: python

EMAIL_BACKEND = 'sparkpost.django.email_backend.SparkPostEmailBackend'

Replace API_KEY with an actual API key that you've generated in Get a Key_ section. Check out the full documentation_ on the Django email backend.

If you are using an EU account, set SPARKPOST_BASE_URI to The default value is

.. _full documentation:

Using with Google Cloud

There are a few simple modifications necessary to enable the use of the underlying requests library that python-sparkpost uses. First, add the requests and requests-toolbelt to your project's requirements.txt:

.. code-block::


Then create or update your file to include the following:

.. code-block:: python

import requests
import requests_toolbelt.adapters.appengine


Then deploy your app and you should be able to send using python-sparkpost on Google Cloud.


#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug. #. Fork the repository_ on GitHub and make your changes in a branch on your fork #. Write a test which shows that the bug was fixed or that the feature works as expected. #. Send a pull request. Make sure to add yourself to AUTHORS_.

.. _the repository: .. _AUTHORS:

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
python (50,867
django (973
email (295

Find Open Source By Browsing 7,000 Topics Across 59 Categories