Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 903
Cortoloman Avatar asked May 18 '20 06:05

Cortoloman


People also ask

How do I know my SendGrid API key is working?

There is an endpoint to retrieve the scopes enabled for the given key at GET https://api.sendgrid.com/v3/scopes . Every valid key seems to work here, with 401 for invalid keys. Additionally it returns the scopes so you can check the api key is valid for the application, so for sending mail it should contain mail.

How do I set up a SendGrid API key?

Sign up for a SendGrid account. Enable Two-factor authentication. Create and store a SendGrid API Key with Mail Send > Full Access permissions. Complete Domain Authentication. Install Python. When you sign up for a free SendGrid account, you'll be able to send 100 emails per day forever. For more account options, see our pricing page.

How many code examples of sendgridapiclient() are there?

The following are 25 code examples of sendgrid.SendGridAPIClient () . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sendgrid , or try the search function .

How do I send plain text or HTML content in SendGrid?

You have two options for the content type: text/plain or text/html. The second parameter will take the plain text or HTML content you wish to send. To properly construct the message, pass each of the previous variables into the SendGrid library's Mail constructor. You can assign this to a variable named mail.

What version of Python does the SendGrid helper library support?

Though the SendGrid helper library supports Python back to version 2.7, we recommend using a 3.x version now that Python 2 has reached end-of-life status. It is possible to have multiple versions of Python on your computer. Some operating systems come with a version of Python already installed.


Video Answer


2 Answers

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: '[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)
  .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

like image 173
slideshowp2 Avatar answered Sep 19 '22 15:09

slideshowp2


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.

like image 26
Kazeem Erinfolami Avatar answered Sep 16 '22 15:09

Kazeem Erinfolami