DEV Community

Cover image for How to send a email using Sendgrid and Node.js ?

Posted on • Originally published at

How to send a email using Sendgrid and Node.js ?

In this blog, We will be seeing how to send a email with Nodejs and Sendgrid mail API.

Pre-requisites :

  1. Node and npm installed on your system

Generating API Key on Sendgrid:

We will first need to register for a free SendGrid account.


After adding your email-address and password , Click on Create Account. We need to further more details to make our way through the send-grid dashboard.


Enter the details and click on Get Started. You should land on the following screen.


Before you can send any email with sendgrid , you need to create sender identity.

In the sender creation form, fill up the details as follows (note that it’s better not to use a general email like Gmail):


Once you are finished with creating your sender identity , You need to verify the sender.


Head over to API-Keys in settings and click on Create API Key


Enter the name of key Sending Email and click on Restricted Access , under that click on mail send and enable that.


Once done , click on create & view. You should see your API key on the screen. Copy it and keep it safe, we will need that while writing code.


Let’s code.

Sending your first email :

Head over to your terminal and run the following

mkdir sending-email-sendgrid
cd sending-email-sendgrid
npm init --y
Enter fullscreen mode Exit fullscreen mode


Let’s install the following packages

yarn add dotenv @sendgrid/mail
Enter fullscreen mode Exit fullscreen mode

Open your code editor and create .env file with following content

Enter fullscreen mode Exit fullscreen mode

Create index.js file and paste the following

const mail = require('@sendgrid/mail');
const dotenv = require("dotenv")


const msg = {
  to: '',
  from: '', // Use the email address that you verified during creation of your sender identity
  subject: 'Sending my first email with Node.js',
  text: 'Email with Node js and Sendgrid',
  html: '<strong>hello world</strong>',

(async () => {
  try {
    await mail.send(msg);
        console.log('mail sent')
  } catch (error) {

    if (error.response) {
Enter fullscreen mode Exit fullscreen mode

What the above code does

  1. Importing the sendgrid/mail sdk which is helpful for sending the email and configuring the dotenv package to access the environment variables inside our node application.
  2. Configuring both sendgrid and dotenv package.

    Prepping the email to send. In here for the to section use the email which you verified during the sender creation

  3. Finally using send method to send the mail to the user.

Open your terminal and run the following

node index.js
Enter fullscreen mode Exit fullscreen mode

You should see mail sent on your console. Head over to the email to check for the same.

Note: Check the spam folder if the email is not in your inbox


🎉 🎉 🎉 Congratulations , You have successfully sent your email with Node.js and sendgrid.


That's pretty much it. Thank you for taking the time to read the blog post. I hope , everyone understood how to send your first email using sendgrid and node.js.

If you found the post useful , add ❤️ to it and let me know if I have missed something in the comments section. Feedback on the blog are most welcome.

Let's connect on twitter : (

Repo Link:

Top comments (2)

jamesvanderpump profile image
James Vanderpump

Nice intro Karthikeyan.
Next challenge is finding your optimal email template language so you can include a common header and footer. If you're using React you can simply use jsx/tsx files, reuse existing components and render the html output using ReactDomServer.renderToString() use (npm) juice to inline CSS from a .CSS file.

imkarthikeyan profile image

Thanks james. Yep. I am working on a use-case scenario with proper email template and automating it with github actions. Blog on that is coming up soon.