我正在尝试在我的Heroku NodeJS应用程序中设置NodeJS插件。我创建了API键并将其设置为环境变量。
整个API键看起来类似于: SG.actualValue.bbb_cccccc
在我做的第一个设置中,我将整个键设置为我的SENDGRID_API_KEY,并得到了以下错误:
API密钥不以SG开头。
因此,我意识到了错误,取消了环境变量,并再次将其设置为整个键的actualValue部分。
然而,我仍然会遇到同样的错误。我试着再次做同样的事情或者重新启动终端(实际上,整个笔记本电脑)。
这是我试图从SendGrid安装页面运行的测试代码:
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);我尝试创建一个新键并设置它,但是我得到了相同的错误。我尝试将其设置为整个键,但没有".SG“或bbb_ccccc部分。提前谢谢你。
发布于 2020-05-19 10:14:31
API密钥不以SG开头。
这意味着SendGrid 的API键应该从SG.开始,因此您没有正确设置环境变量。你得检查一下。只需使用console.log打印环境变量。或者,使用
$ heroku run bash -a mighty-river-12802若要为应用程序启动控制台,请使用printenv打印环境变量。
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/nodeTIMES: 5环境变量通过heroku vars设置:

例如。
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收到的电子邮件如预期:

发布于 2020-06-16 14:11:54
你好,如果您使用节点js,请确保在需要sendgrid/nodemailer模块的文件中包含了require('dotenv').config()。没有它,sendgrid传输程序将有一个未定义的值,而不是api_key。我也遇到了同样的问题并解决了这个问题。
发布于 2020-07-07 19:47:09
我使用的是SendGrids v3和dotenv8.2,在Node.js SendGrid上设置了env文件SendGrid.env,其中有导出SENDGRID_API_KEY,我将文件重命名为.env,删除导出,现在它开始工作了。
在我的sendEmail文件顶部,如下所示:
require('dotenv').config();
const sgMail = require('@sendgrid/mail');
const apiKey = `${process.env.SENDGRID_API_KEY}`;
console.log("SendGrid key ", apiKey);我的.env文件如下所示:
SENDGRID_API_KEY='SG.{left blank}................0EmA'
ANOTHER_API_KEY='ANOTEHERKEY'https://stackoverflow.com/questions/61863508
复制相似问题