Send emails with Node.js using SendGrid

Hashan Bhanuka
3 min readFeb 26, 2022

What is SendGrid?

SendGrid is a cloud-based SMTP provider that allows you to send email without having to maintain email servers. SendGrid manages all of the technical details, from scaling the infrastructure to ISP outreach and reputation monitoring to whitelist services and real-time analytics.

Step 1 - Create a SendGrid Account

To send emails with SendGrid from Node.js, we first need to sign up for a SendGrid account.

Here you can create a SendGrid account by providing a valid email address and password.

After successfully creating your account, you will see something like this.

Step 2 - Create a Single Sender

Navigate to the following link and create a sender. https://app.sendgrid.com/settings/sender_auth/senders

After filling all the fields, click on Create button. Then you will receive an activation email.

Step 3 - Create Express Server

First, create a new folder. And then, open the folder in VS Code and create a package.json file with the npm init -y command.

Then you need to install the following dependencies. To do this, you need to run the following commands.

npm install express dotenv cors @sendgrid/mail --save
npm install nodemon --save-dev

After installing the dependencies, open the package.json file and change the script in the package.json as below.

Next, create files and folders in your project directory according to the following structure.

Then, Add the following code to the relevant files

  • server.js
  • sendEmail.js

Step 4 - Get the API Key

First, you need to navigate to the SendGrid Dashboard and click on Email API, and then the integration guide.

After that, you need to choose the WebAPI option

Now select the Node js option, as below:

Now you need to generate an API key by giving a name to API Key, as below :

Then, copy the API Key and paste it into the .env file as follows,

SENDGRID_API_KEY= Paste your API KEY

Now you can run your express server by typing npm startin the terminal. Then open the browser and navigate to http://localhost:5000.

Then you will see a page as below,

Now, you can send an email by clicking send email link.

After a few seconds, You will receive an email as follows,

--

--