45

SendGrid seems to be preventing my node js server from sending emails.

I get this error message in the response on sending off an email:

The provided authorization grant is invalid, expired or revoked

I have an API key setup as well and have followed the documentation.

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
AngularM
  • 15,982
  • 28
  • 94
  • 169

11 Answers11

80

You need to use the API KEY GENERATED

enter image description here

DO NOT USE the API KEY ID

enter image description here

Sendgrid only displays the generated key once when you create it. enter image description here

If you didn't record it somewhere when you created the key you'll need to create a new key and then you'll probably want to delete the old key since it would be useless if you don't know what it is.

FYI: The API key in the screenshot above is already deleted. I deleted it right away so please don't worry about me leaking that key.

Justin Steele
  • 2,230
  • 13
  • 16
5

This is a late answer and JAVA oriented.. But I simply copied the docs and didn't notice..

SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));

I just put my key in there and didn't see the getEnv. Silly of course.. but when trying to get things to work quickly...

James
  • 1,263
  • 2
  • 12
  • 12
  • 1
    ```new SendGrid("SENDGRID_API_KEY");``` That worked for me. System.getenv didnt work. – Yuvaraj Chitela Jan 10 '20 at 15:57
  • Yes, your code just needed to be like : SendGrid sg = new SendGrid("YOUR_SENDGRID_API_KEY"); without the System.getenv() method! wonder why they have it in the docs anyway – Akash Verma Oct 09 '21 at 21:14
4

For me I just had to generate a new API key. For some weird reason the former API key has become invalid, so I also added an alert for this case

Asaf David
  • 3,167
  • 2
  • 22
  • 38
  • This was the case for me as well. Do you have any idea why the former API key became invalid? Also, what kind of alert have you set up? – Massimo Bortone Sep 27 '20 at 12:32
  • Have no idea why this was the case and it's more than half a year ago, so sorry can't provide any more useful info – Asaf David Sep 29 '20 at 10:43
2

In Sendgrid v3, I had the similar issue when using env variable in Node JS. If I use env variable, I get the above issue. But if I drop the string into the require process, it works.

Doesn't Work:

SENDGRID_API_KEY=SG.XXXXXXXXXXXXXXXXXXX
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);

Works

var sg = require('sendgrid')('SG.XXXXXXXXXXXXXXXXXXX');

Replace SG.XXXXXXXXXXXXXXXXXXX with API Key Generated (which you can only see once during key generation).

EDIT

Note : Make sure you don't save this to public repository. If you do anyone can use your API Key and also your account will be suspended by Sendgrid team temporarily until you remove it from repository.

  • This solution works for me, but do you have any idea of how to fix this. This is not a good solution for developer who uses the .env file. – Isak La Fleur Aug 17 '17 at 18:43
  • I got the same error. So if anyone will get into same issue - double check your's .env file. The key should not be in quotes "" or any other separators. Should look something like: SENDGRID_API_KEY = your_key_here – Dmytro Zharkov Sep 22 '17 at 10:53
2
  • If you are using node js,
  • make sure you have the require('dotenv').config()line inside the file that needs the sendgrid/nodemailer module.
  • Without it, the sendgrid transporter will have an undefined value instead of the api_key.
Rohan Devaki
  • 2,931
  • 1
  • 14
  • 22
2

In my case I was trying to debug the connection by using telnet and kept getting rejected.

Turns out that these two are not equivalent, echo will include \n in the encoded string.

echo 'apikey' | base64
printf 'apikey' | base64

So yeah, make sure you don't include the newline in the API key.

Martin Melka
  • 7,177
  • 16
  • 79
  • 138
  • thank you for this suggestion. I'm using SendGrid in kubernetes and needed to store the apikey value encrypted using base64. I've been going crazy about this for days, and this is exactly what it was happening to me. – matus Oct 25 '21 at 10:53
0

Here's my solution:

  1. Install the dotenv package: npm i dotenv

  2. Go to the earliest entry point of your application(i.e index.js) and put this at the top:

    const dotenv = require('dotenv').config();

  3. Create a .env file and add SENDGRID_API_KEY=<YOUR_API_KEY>. NO quotes '' or "".

  4. In your file which you use sendgrid, add this to the top:

    const sgMail = require('@sendgrid/mail');

    sgMail.setApiKey(process.env.SENDGRID_API_KEY);

Done.

cguy
  • 325
  • 5
  • 15
0

It might be late for an answer but for people who are getting the same problem in spring boot

it might be caused at initialization when you are using env or property value

i was initializing the variable in the constructor before the value was loaded by spring and it was giving the same output. so either initialize it in the method you are calling the SendGrid function or do it after values are loaded

0

Instead of using, api_key: ${process.env.SENDGRIDAPIKEY}

try, api_key: "" + ${process.env.SENDGRIDAPIKEY} + ""

worked for me,

0

For me; I've just created a new API key and then it works again. I'm %100 sure it was working with no any code change.

Maybe it is an issue with SendGrid or some sort of security action from SendGrid.

freewill
  • 1,111
  • 1
  • 10
  • 23
-2

I had the same issue, it disappeared after I verified my email address and enabled 2FA.