Sendgrid-nodejs: Setup issue

Created on 10 Feb 2017  Â·  14Comments  Â·  Source: sendgrid/sendgrid-nodejs

Hi,
I'm trying to setup SendGrid, and I'm just following the instructions in the tutorial for "With Mail Helper Class". After I run the following code:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

I get an error "export: Command not found."

When I try to send my first send grid e-mail I get an error 401. 'The provided authorization grant is invalid, expired, or revoked.'

I've checked and echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env did create a sendgrid.env in my projects root folder.

When I checked the:
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
console.log(sg);

The console returns Authorization: 'Bearer undefined',.

Any help would be appreciated. Thank you.

All the best,
Don

  • sendgrid-nodejs Version: master (latest commit: [commit number])
  • Node v 6.3.1
question

Most helpful comment

This is an old issue but I was facing a similar issue and the problem was that I had 2 terminal windows open. One for installing packages and the other for running the server. The source ./sendgrid.env needs to be done in the terminal window you're running the server in. So stopping the server, running the command and restarting the server fixed the issue for me.

All 14 comments

Hello @DonWono,

Are you developing on Windows, Mac or Linux?

Hi @thinkingserious

Thanks for your quick reply. I'm developing on a Mac. When everything works I will be deploying to an Ubuntu server.

What happens if you do the following from your command line:

echo $SENDGRID_API_KEY

If you don't see your API Key, try:

export SENDGRID_API_KEY='YOUR_API_KEY'
echo $SENDGRID_API_KEY

Hi @thinkingserious,
Thank you for your help! When I checked the echo $SENDGRID_API_KEY, it did print out my api key, but I still couldn't load it into my node application using:

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

I did a work around, and put the API_KEY into a JSON file and added the JSON file to gitignore.
Everything seems to be working now.

Thank you for your help.

I'm having a similar issue when using a env variable. If I use the env variable I get the issue above. If I just drop the string into the require process it works. I can not figure out why.

Works...
var sg = require('sendgrid')('SG.iIpwjF6ASgmRuYmwl_KHoQ.EoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxY3NQ9vwd0');

Doesn't Work...
SENDGRID_API_KEY=SG.iIpwjF6ASgmRuYmwl_KHoQ.EoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxY3NQ9vwd0
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);

Even if I log out the env and compare it to the string it's an exact match.

Hello @thegregthomp,

Your "Doesn't Work" case is failing because process.env.SENDGRID_API_KEY is looking for the environment variable to be set.

On a Unix-like system, this is usually done like this on the command line:

export SENDGRID_API_KEY='YOUR_API_KEY'

Then you can verify it works by executing the following on the command line:

echo $SENDGRID_API_KEY

To get your "Doesn't Work" case to work:

SENDGRID_API_KEY=SG.iIpwjF6ASgmRuYmwl_KHoQ.EoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxY3NQ9vwd0
var sg = require('sendgrid')('SENDGRID_API_KEY');

But that is not recommended. We recommend you store your API Key in an environment variable for security reasons.

I hope that helps. Thanks!

This is an old issue but I was facing a similar issue and the problem was that I had 2 terminal windows open. One for installing packages and the other for running the server. The source ./sendgrid.env needs to be done in the terminal window you're running the server in. So stopping the server, running the command and restarting the server fixed the issue for me.

What's the solution to this if you're on windows? My issue may be different, as even with the key directly in place it still doesn't seem to work.

I do know it's not accessing my api key properly when I use .env but I also get 'unresolved promise...' as well

Are you setting it like this?

C:> setx SENDGRID_API_KEY "YOUR_API_KEY"

Another way to provide environment variables to Node is to prepend the
environment variable on the command line:

SENDGRID_API_KEY=SG.xxx.yyyy node app.js

On Thu, Nov 29, 2018 at 9:52 PM Drew Clements notifications@github.com
wrote:

What's the solution to this if you're on windows? My issue may be
different, as even with the key directly in place it still doesn't seem to
work.

I do know it's not accessing my api key properly when I use .env but I
also get 'unresolved promise...' as well

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/sendgrid/sendgrid-nodejs/issues/356#issuecomment-443090894,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACL-svP5ufBQ__slqnDwyJmC_2rh7Cguks5u0LmdgaJpZM4L9ILb
.

I ended up getting this resolved. My .env file was one level up in the
directory to where I was using sendgrid.

So I fought for 3 days because I was missing a '.'
I was using source ./sendgrid.env and needed source ../sendgrid.env

On Thu, Dec 6, 2018 at 5:17 PM Ashley Roach notifications@github.com
wrote:

Are you setting it like this?

C:> setx SENDGRID_API_KEY "YOUR_API_KEY"

Another way to provide environment variables to Node is to prepend the
environment variable on the command line:

SENDGRID_API_KEY=SG.xxx.yyyy node app.js

On Thu, Nov 29, 2018 at 9:52 PM Drew Clements notifications@github.com
wrote:

What's the solution to this if you're on windows? My issue may be
different, as even with the key directly in place it still doesn't seem
to
work.

I do know it's not accessing my api key properly when I use .env but I
also get 'unresolved promise...' as well

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<
https://github.com/sendgrid/sendgrid-nodejs/issues/356#issuecomment-443090894
,
or mute the thread
<
https://github.com/notifications/unsubscribe-auth/ACL-svP5ufBQ__slqnDwyJmC_2rh7Cguks5u0LmdgaJpZM4L9ILb

.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/sendgrid/sendgrid-nodejs/issues/356#issuecomment-445050097,
or mute the thread
https://github.com/notifications/unsubscribe-auth/Aih_7oFC879FKsT4v6dWY4fXVVfBORN7ks5u2ZdzgaJpZM4L9ILb
.

Oh bummer @drewclem but glad you figured it out!

If you're a window user and getting source not recognized.
Try this command
set ./sendgrid.env
It works for me.

Hi, I am still facing a 401 error from my python code. I have checked my environment variable is set properly as well.
What should i do ?? :(

@MuhammadSufyanMalik Tracking here: https://github.com/sendgrid/sendgrid-python/issues/561

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zvone187 picture zvone187  Â·  4Comments

Chrischuck picture Chrischuck  Â·  3Comments

murphman300 picture murphman300  Â·  4Comments

agostonbonomi picture agostonbonomi  Â·  3Comments

thidasapankaja picture thidasapankaja  Â·  4Comments