Forums

Sendgrid API not working

Hi,

I have a developer account and am trying to use the Sendgrid API to send transaction emails from my web app. I am getting the following error:

python_http_client.exceptions.UnauthorizedError: HTTP Error 401: Unauthorized
2018-12-12 15:44:42,242: Exception on /forgotpassword [POST]
Traceback (most recent call last):
  File "/home/sidearmjohnny/.virtualenvs/flaskenv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sidearmjohnny/.virtualenvs/flaskenv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/sidearmjohnny/.virtualenvs/flaskenv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/sidearmjohnny/.virtualenvs/flaskenv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/sidearmjohnny/.virtualenvs/flaskenv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/sidearmjohnny/.virtualenvs/flaskenv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/sidearmjohnny/mysite/flask_app.py", line 144, in forgot_password
    response = sg.client.mail.send.post(request_body=mail.get())
  File "/home/sidearmjohnny/.virtualenvs/flaskenv/lib/python3.6/site-packages/python_http_client/client.py", line 252, in http_request
    return Response(self._make_request(opener, request, timeout=timeout))
  File "/home/sidearmjohnny/.virtualenvs/flaskenv/lib/python3.6/site-packages/python_http_client/client.py", line 176, in _make_request
    raise exc
python_http_client.exceptions.UnauthorizedError: HTTP Error 401: Unauthorized

It's working correctly on my local, and I am able to send emails from pythonanywhere when I run the code separately in my virtual environment called flaskenv (where I've installed sendgrid and set up run "source ./sendgrid.env" which includes my private key) via the command line. Here's the code that works when run in a separate file from the command line:

# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import sendgrid
import os
from sendgrid.helpers.mail import *



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

Any thoughts on why I may be getting this error?

Best,

Solved my problem by updating the code in my application as follows

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))

to

sg = sendgrid.SendGridAPIClient(apikey='<my sendgrid key here>')

I'm not happy with this solution, and would rather figure out why I couldn't get the variable from the environment variable SENDGRID_API_KEY.

Anyone know how to set environment variables?

We have a help page for that: http://help.pythonanywhere.com/pages/environment-variables-for-web-apps/