Search code examples
pythonherokusparkpost

Getting API Key from SparkPost / Heroku in python


I have installed SparkPost on heroku. The issue is that I cannot find any of the settings for SparkPost either to conect with domain or use the API. The easier route seems to install the sparkpost package with pip, but using the documentation, I cannot pull in the API either with:

 sp = SparkPost()

or

sp = SparkPost(os.environ['API_KEY_SPARKPOST'])

The former gives the following error:

SparkPostException: No API key. Improve message.

and the latter, cannot find any environment variable. I had a search, I don't see any environment variables that are related to SparkPost. Not do I find any API option in the control panel with which to find one.


Solution

  • for the Heroku add-on the environment variable is called SPARKPOST_API_KEY and that is what gets used by default. Assuming you're running this locally, the problem in the first example is that you probably don't have SPARKPOST_API_KEY in your environment. When you run heroku local it will load up variables in a file called .env in your project's root. You'll want to add that by doing something like this:

    heroku config:get SPARKPOST_API_KEY -s >> .env

    Your second example is exactly what the python-sparkpost library does under the hood, but the name of the var is SPARKPOST_API_KEY.

    You can see a list of all the env vars available for the SparkPost add-on here: https://devcenter.heroku.com/articles/sparkpost#provisioning-the-add-on. You can also run heroku config to see all of your env vars from the command-line.