Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js @sendgrid/mail Error: Unauthorized

I want to send email from my application (Angular). I tried to use @sendgrid/mail in my back with node.js, but it's not working.

The error is :

(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
    at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
    at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
    at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
    at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

Maybe some body can help me ?

Please find bellow my app.js

var sendgrid = require('@sendgrid/mail');
app.post('/sendemail', (request, response) => {
    const body = request.body;
    const email = body.email;
    console.log('sendMail');


    SENDGRID_APY_KEY = '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';
    sendgrid.setApiKey(process.env.SENDGRID_APY_KEY);
    const msg = {
      to: '[email protected]',
      from: '[email protected]',
      subject: 'Votre mot de passe provisoire',
      text: 'Bonjour !',
      html: '<p>Bonjour!</p><p>Voici votre mot de passe provisoire:</p> <p>Cordialement,</p> <p>Infogène Lyon</p>',
    };

    sendgrid.send(msg);
    console.log(msg);

  });
like image 734
Ambre Naude Avatar asked Dec 19 '18 14:12

Ambre Naude


1 Answers

The issue lies in the below piece

SENDGRID_APY_KEY = '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';
    sendgrid.setApiKey(process.env.SENDGRID_APY_KEY);

When you are starting up the server, you need to pass in the sendgrid api key as an env variable.The above bit of code looks for SENDGRID_APY_KEY as an env variable.

To get this to work without passing in env varaiables:

Remove the process.env and declare SENDGRID_APY_KEY as a variable.

var SENDGRID_APY_KEY = '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';
    sendgrid.setApiKey(SENDGRID_APY_KEY);

To pass in the SENDGRID_APY_KEY as an env variable:

SENDGRID_APY_KEY='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' node app.js // or however you are starting up the server

If you do pass in the key as an env variable, you need to set the sendgrid api from that env variable, like this: sendgrid.setApiKey(process.env.SENDGRID_APY_KEY)

Also, you should reset/renew your API key (assuming that the one you posted is your current one)

like image 170
Rastalamm Avatar answered Nov 15 '22 04:11

Rastalamm