title: 403 Forbidden in Node.js while sending email using sendgrid
body: <p>I am designing a contact page in which UI is rendered using React. I have a form which is supposed to send email on submit. Here is the UI code for handling submit:</p>
  
  <pre><code>    handleSubmit = (event) =&gt; {
      event.preventDefault();
  
      this.setState({
          disabled: true
      });
  
      Axios.post('http://localhost:3040/api/email', this.state)
          .then( res =&gt; {
              if(res.data.success){
  
                this.setState({
                  disabled: false,
                  emailSent: true
                });
              } else{
                  this.setState({
                      disabled: false,
                      emailSent: false
                  });
              }
          })
          .catch(err =&gt; {
              this.setState({
                  disabled: false,
                  emailSent: false
              });
          });
  }
  </code></pre>
  
  <p>The api to send email is written in Node.js. Used @sendgrid//mail to trigger send. On debugging I can see that the form values are reaching the api but on send it throws 403 Forbidden error. Here is the api code:</p>
  
  <pre><code>app.post('/api/email', (req, res, next) =&gt; {
  sendGrid.setApiKey('&lt;Generated key in sendgrid&gt;');
  const msg = {
      to: 'some@email.com',
      from: req.body.email,
      subject: 'Website Contact Page',
      text: req.body.message
  }
  
  sendGrid.send(msg).then(result =&gt; {
      res.status(200).json({
          success: true
      });
  })
  .catch(err =&gt; {
      console.log('error: ', err);
      res.status(401).json({
          success: false
      });
  });
  });
  </code></pre>
  
  <p>The following is the error trace I am getting in the VSCode console while debugging:</p>
  
  <pre><code>stack:"Error: Forbidden
  at axios.then.catch.error (c:\react\portfolio-api\node_modules\@sendgrid\client\src\classes\client.js:105:29)
  at process._tickCallback (internal/process/next_tick.js:68:7)"
  </code></pre>
  
  <p><strong>proto</strong>:Error {constructor: , toString: , toJSON: }</p>
  
  <p>Not sure why its giving me Forbidden error. Please let me know if I need to add more info here. Thanks in advance :)</p>
  
  <p>EDIT:- 
  Followed the doc here at sendgrid to create an API key and used the same in sendGrid.setApiKey().</p>
  
  <p><a href="https://i.stack.imgur.com/xikRB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xikRB.png" alt="enter image description here"></a></p>