sendgrid-python: UnauthorizedError: HTTP Error 401: Unauthorized error appears when sending e-mail

Issue Summary

I can send e-mails with my api key by using curl with following examples: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/curl_examples.html

But when I try to send an e-mail with “Hello Mail/With Mail Helper Class” code, I get following error:

Traceback (most recent call last):
  File "gridsend.py", line 11, in <module>
    response = sg.client.mail.send.post(request_body=mail.get())
  File "/usr/local/lib/python3.6/site-packages/python_http_client/client.py", line 227, in http_request
    return Response(self._make_request(opener, request))
  File "/usr/local/lib/python3.6/site-packages/python_http_client/client.py", line 161, in _make_request
    raise exc
python_http_client.exceptions.UnauthorizedError: HTTP Error 401: Unauthorized

Technical details:

  • sendgrid-python Version: 5.3.0
  • Python Version: 3.6

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Now I understand the issue. Example code contains this code apikey=os.environ.get('SENDGRID_API_KEY') what I understand from here, I just need to type my API key to here. However, it’s the name of the environment variable. Documentation is not clear on this and it’s very confusing. I suggest you to make this point clear for everyone. Cheers

I have reopened this issue so that we can update the README to make this issue clearer.

Thanks @utkusen!

In addition to making that line clear in the python code, I think the README should also be updated with how to properly set the environment variable for the api key on windows. I found(after multiple attempts) that typing set SENDGRID_API_KEY=<insert_your_api_key> into the cli works. @thinkingserious

Here is an even easier method to test your API Key.