10
def sendEmail(to,apNumber,paperType,zipedFile):

    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get("API-KEY"))

    to_email = mail.Email( "to@email.com")
    from_email = mail.Email( "from@email.com" )
    subject = 'This is a test email'
    content = mail.Content('text/plain', 'Example message.')
    message = mail.Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body = message.get())
    return response
mata
  • 67,110
  • 10
  • 163
  • 162
vinay
  • 123
  • 1
  • 1
  • 8
  • Are you sure the `API-KEY` environment variable is set? What OS are you using (`API-KEY` is a valid env variable name on Windows, but *nix doesn't allow `-` in the name). – mata Sep 27 '16 at 07:11
  • I am using ubuntu 16.0. may i know how to set environment variable for api – vinay Sep 27 '16 at 07:15
  • You'd usually export it in the shell before running your program, e.g. `export API_KEY="..."` - read more [here](https://help.ubuntu.com/community/EnvironmentVariables) – mata Sep 27 '16 at 07:31

6 Answers6

8

Setup Environment Variables.

To Set up environment Variable follow the below 3 steps

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env
Shridhar Patil
  • 362
  • 1
  • 17
3
    import os
    from sendgrid import SendGridAPIClient
    from sendgrid.helpers.mail import Mail
    message = Mail(
            from_email='...@gmail.com',
            to_emails='....@gmail.com',
            subject='Sending with Twilio SendGrid is Fun',    
            html_content='<strong>and easy to do anywhere, even with Python</strong>')
    try:
    **sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))**

        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(e)

I got the same error "UnauthorizedError: HTTP Error 401: Unauthorized"

I refactored the code from sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))

to

sg=SendGridAPIClient('......IeltIytmFYeQ0aSOt2UBYvv2E6Xh...')

Then it started working.

I change my API key settings to Full access in SendGrid portal.

Santhosh s
  • 184
  • 1
  • 5
3

I was getting "HTTP/1.1 401 Unauthorized" error in PHP. See the screenshot.

enter image description here

I solved it by removing getenv() from the API key.

Replace:

$apiKey = getenv("SendGrid_API_Key");
$sendgrid = new \SendGrid($apiKey);

with:

$apiKey = ("SendGrid_API_Key");
$sendgrid = new \SendGrid($apiKey);
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Mohit Tiwari
  • 165
  • 2
  • 6
0

I created a new key and that solved my issue. Exact issue for this is still unknown. As it was functional before.

0

The error for me was because I used the wrong key , actually, one needs to use the key that is created the first time when one create the Api key , which one cannot edit again. So one have to click "Create Api key" button and use this key

0

My problem was that though

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

worked fine when the script was called from the terminal, but it did not when it was called from my a javascript file in the browser. It's because the os.environ.get was actually different in that case. Directly having the API_KEY was the solution:

sg = SendGridAPIClient(api_key='SG.tbd')
erdomester
  • 11,789
  • 32
  • 132
  • 234