Skip to content
SparkPost client library for Python
Python Makefile
Branch: master
Clone or download
duanehutchins and jgzamora Transmission class updated to use the keys defined by the SparkPost A…
…PI (#193)

Old keys still function by being remapped to match the API
Latest commit c78de15 Jun 5, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs Allowing US or EU API to be used in the Django backend. (#188) May 29, 2019
examples I noticed the documentation said a recipients array will replace the … ( May 29, 2019
sparkpost Transmission class updated to use the keys defined by the SparkPost A… Jun 5, 2019
test Transmission class updated to use the keys defined by the SparkPost A… Jun 5, 2019
.editorconfig Add editorconfig file Oct 4, 2015
.gitignore Refactor as per code review Nov 2, 2015
.travis.yml Add Python 3.5 to CI runs May 6, 2016
AUTHORS.rst Allowing US or EU API to be used in the Django backend. (#188) May 29, 2019
CHANGELOG.md Bump versions and update changelog Mar 22, 2018
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md May 15, 2018
CONTRIBUTING.md Added tox support. refs #67 Feb 26, 2016
LICENSE boilerplate Jan 30, 2015
MANIFEST.in added wheel support, prep for PyPI Feb 8, 2015
Makefile add links to deprecation message, modify lib order Mar 7, 2017
README.rst Allowing US or EU API to be used in the Django backend. (#188) May 29, 2019
dev-requirements.txt
setup.cfg added wheel support, prep for PyPI Feb 8, 2015
setup.py Bump version Jan 4, 2019
test-requirements.txt
tox.ini Added tox support. refs #67 Feb 26, 2016

README.rst

Sign up for a SparkPost account and visit our Developer Hub for even more content.

SparkPost Python API client

Build Status Documentation Status Coverage Status

The official Python package for using the SparkPost API.

Documentation

Installation

Install from PyPI using pip:

$ pip install sparkpost

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:

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

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

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.

from sparkpost import SparkPost
sp = SparkPost('YOUR API KEY', 'https://api.eu.sparkpost.com')

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:

from sparkpost import SparkPost

sp = SparkPost()

response = sp.transmissions.send(
    use_sandbox=True,
    recipients=['someone@somedomain.com'],
    html='<p>Hello world</p>',
    from_email='test@sparkpostbox.com',
    subject='Hello from python-sparkpost'
)

print(response)
# outputs {u'total_accepted_recipients': 1, u'id': u'47960765679942446', u'total_rejected_recipients': 0}

Django Integration

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

SPARKPOST_API_KEY = 'API_KEY'
SPARKPOST_BASE_URI = 'api.sparkpost.com'
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 api.eu.sparkpost.com. The default value is api.sparkpost.com.

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:

requests
requests-toolbelt

Then create or update your appengine_config.py file to include the following:

import requests
import requests_toolbelt.adapters.appengine

requests_toolbelt.adapters.appengine.monkeypatch()

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

Contribute

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
  2. Fork the repository on GitHub and make your changes in a branch on your fork
  3. Write a test which shows that the bug was fixed or that the feature works as expected.
  4. Send a pull request. Make sure to add yourself to AUTHORS.
You can’t perform that action at this time.