Open In App
Related Articles

Sending bulk emails in Node.js using SendGrid API

Like Article
Save Article
Report issue

What is SendGrid API? 
SendGrid is a platform for sending transactional and marketing emails to the customers. It provides scalability, reliability, and deliverability which is an important issue for an organization.
Benefits of using SendGrid API:

  • If you are using Nodemailer with Gmail then you can send only certain amount of emails per day.
  • Also, there is no need to set up your own SMTP server.
  • SMTP doesn’t provide the deliverability, i.e. email may or may not be sent.

Steps to send emails using SendGrid API:

  1. Setting up an API key: 
    • Go to sendgrid dashboard and click on create api key button.
    • Name the API-key as you want, for this tutorial we’ll name it node-mail
    • Copy the API-key as for security reasons you may not be able to see it again.
  2. Setting up a node.js app:
    • Create an empty NPM package using the command. (The –y flag which is passed is used to use the defaults in the generator instead of asking questions)
      npm init -y
    • Create a file named index.js and add a boiler plate code.


      // Importing http library
      const http = require("http");
      const PORT = 3000; // Defining PORT
      http.createServer((req, res) => {
          // Output Hello World on HTML page
          res.write("<h1>Hello World!</h1>");
      // Initializing server
      .listen(PORT,() => console.log(`Server running on PORT : ${PORT}`));
    • Now run the code by using node index command and go to link. You will see the output.

    • And in console 
  3. Installing SendGrid library: Install the SendGrid library by running the below command
    npm i @sendgrid/mail
  4. Sending emails by using library:


    const http = require("http");
    const PORT = 3000;
    http.createServer((req, res) => {
        // Initializing sendgrid object
        const mailer = require("@sendgrid/mail");
        // Insert your API key here
        // Setting configurations
        const msg = {
          to: ["", ""],
          from: "",
          subject: "Message sent for demo purpose",
            "<h1>New message from Geeksforgeeks</h1> 
    <p>Some demo text from geeksforgeeks.</p>
        // Sending mail
        mailer.send(msg, function(err, json) {
          if (err) {
            // Writing error message
            res.write("Can't send message sent");
          } else {
            // Writing success message
            res.write("Message sent");
    .listen(PORT, () => console.log(`Server running on PORT : ${PORT}`));
Now run the app again by using node index and go to in the browser and check both of your emails, you’ll see an output like below.

Last Updated : 13 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads