19

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: 'test@example.com',
  from: 'test@example.com',
  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.

Cortoloman
  • 695
  • 1
  • 7
  • 14
  • I am getting this same error with NextJS and vercel on production but in the local dev environment, it works fine. Does anyone know what can be the issue? – Shadab Jan 01 '22 at 12:01

9 Answers9

17

API key does not start with SG.

means the API key of SendGrid SHOULD start with SG. So you didn't set the environment variables correctly. You need to check it. Just use console.log print the environment variables. Or, use

$ heroku run bash -a mighty-river-12802

to start a console for your app, and use printenv to print the environment variables.

Running bash on ⬢ mighty-river-12802... up, run.1571 (Free)
~ $ printenv
TERM=xterm-256color
WEB_MEMORY=512
MEMORY_AVAILABLE=512
COLUMNS=367
DYNO=run.1571
PATH=/app/.heroku/node/bin:/app/.heroku/yarn/bin:/usr/local/bin:/usr/bin:/bin:/app/bin:/app/node_modules/.bin
WEB_CONCURRENCY=1
_=/usr/bin/printenv
PWD=/app
PS1=\[\033[01;34m\]\w\[\033[00m\] \[\033[01;32m\]$ \[\033[00m\]
NODE_ENV=production
LINES=49
TIMES=5
HOME=/app
SHLVL=2
PORT=6791
NODE_HOME=/app/.heroku/node

TIMES: 5 environment variable is set via heroku config vars:

enter image description here

E.g.

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

sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'novaline.dulin@gmail.com',
  from: 'test@example.com',
  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)
  .then(() => console.log('send mail success'))
  .catch(console.log);
$ export SENDGRID_API_KEY=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
$ node index.js
send mail success

Received the email as expected:

enter image description here

Lin Du
  • 88,126
  • 95
  • 281
  • 483
10

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.

5

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'
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
2

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........"
Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31
Jean Chen
  • 29
  • 1
1

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

luca
  • 126
  • 6
  • environment variables for production should be set on the server so do not need the dotenv. dotenv is for development not production – Ufenei augustine Feb 01 '22 at 05:37
1

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

Cedric Zoppolo
  • 4,271
  • 6
  • 29
  • 59
0

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"
Green
  • 507
  • 10
  • 20
0

i had the same problem in NestJS and the problem was that I am very new in NestJS lol.

SendGrid.setApiKey(this.configService.get<string>('SENDGRID_API_KEY'));

the configureService use the name of the variable to get the value and I thought that I should pass the variable and not the name

rudighert
  • 315
  • 1
  • 4
  • 11
-1

This problem might occur if you did not restart the server every time you set something on environment variable (dev.env).
For example if you put your key from sendgrid or any value in environmental variable (dev.env) you should restart the server ,In my case I also faced the same issue, then I put process.env.SENDGRID_API_KEY in console.log and found undefined in the terminal so what in my case did next was typed ^c in the terminal and hit enter then:

npm run dev

Now see in the terminal you can see the key showing in the terminal, Hope bit solves your problem

Tyler2P
  • 2,324
  • 26
  • 22
  • 31