API Key does not start with "SG." SendGrid
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)~ $ printenvTERM=xterm-256colorWEB_MEMORY=512MEMORY_AVAILABLE=512COLUMNS=367DYNO=run.1571PATH=/app/.heroku/node/bin:/app/.heroku/yarn/bin:/usr/local/bin:/usr/bin:/bin:/app/bin:/app/node_modules/.binWEB_CONCURRENCY=1_=/usr/bin/printenvPWD=/appPS1=\[\033[01;34m\]\w\[\033[00m\] \[\033[01;32m\]$ \[\033[00m\]NODE_ENV=productionLINES=49TIMES=5HOME=/appSHLVL=2PORT=6791NODE_HOME=/app/.heroku/node
TIMES: 5
environment variable is set via heroku config vars:
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.jssend mail success
Received the email as expected:
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.
I'm using SendGrids v3 and dotenv v8.2, on Node.jsSendGrid 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'