'API Key does not start with "SG." SendGrid

I am trying to set up SendGrid add-on in my Heroku NodeJS app. I created the API Key and set it as an environment variable.

The whole API key looks something like: SG.actualValue.bbb_cccccc

The first setup I did I set the whole key as as my SENDGRID_API_KEY and I got this error:

API key does not start with SG.

So, I realized the mistake and unset the environment variable and set it again only to the actualValue part of the whole key.

However, I still get the same error. I tried doing the same thing again or restarting the terminal(actually, whole laptop).

This is the test code I am trying to run from the SendGrid setup page:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const msg = {
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);

I tried creating a new key and setting it, but I get the same error. I tried setting it to the whole key, but without ".SG" or just the bbb_ccccc part. Thank you in advance.



Solution 1:[1]

hello there If you are using node js, make sure you have the require('dotenv').config() inside the file that needs the sendgrid/nodemailer module. Without it, the sendgrid transporter will have an undefined value instead of the api_key. i also encountered the same issue and its solved.

Solution 2:[2]

I'm using SendGrids v3 and dotenv v8.2, on Node.js SendGrid set up an env file SendGrid.env, inside it has export SENDGRID_API_KEY, I renamed the file to .env, removed export and now it works.

At top of my sendEmail file looks like this:

require('dotenv').config();
const sgMail = require('@sendgrid/mail');
const apiKey = `${process.env.SENDGRID_API_KEY}`;
console.log("SendGrid key ", apiKey);

my .env file looks like this:

SENDGRID_API_KEY='SG.{left blank}................0EmA'
ANOTHER_API_KEY='ANOTEHERKEY'

Solution 3:[3]

I changed the single quote to double quote in .env. That fixed my bug. Here is how my sendgrid.env file looks like:

SENDGRID_API_KEY = "SG.oDXKOMdlT3-nCEQt........"

Solution 4:[4]

You need to require('dotenv').config(); at the beginning of your test file, otherwise it won't be able to find your key.

Solution 5:[5]

I had a same problem I removed the export keyword from the .env file and it worked for me

Solution 6:[6]

I had this problem while running my code in the dev environment. I am also using the env-cmd package. I fixed the problem by changing my package.json file from this:

"dev": "env-cmd -f ./config/dev.env nodemon src/index.js"

to this:

"dev": "env-cmd -f ./config/dev.env nodemon src/emails/account.js"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kazeem Erinfolami
Solution 2 Sven Eberth
Solution 3 Kelvin Schoofs
Solution 4 luca
Solution 5 Cedric Zoppolo
Solution 6 Green