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:
- 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.
- Go to sendgrid dashboard and click on create api key button.
- Setting up a node.js app:
- Start a node.js app using the command
npm init -y
- Create a file named
index.jsand add a boiler plate code.// Importing http libraryconst http = require("http");const PORT = 3000;// Defining PORThttp.createServer((req, res) => {// Output Hello World on HTML pageres.write("<h1>Hello World!</h1>");res.end();})// Initializing server.listen(PORT, () => console.log(`Server running on PORT : ${PORT}`));chevron_rightfilter_none - Now run the code by using
node indexcommand and go to127.0.0.1:3000link. You will see the output.
And in console
- Start a node.js app using the command
- Installing SendGrid library: Install the SendGrid library by running the below command
npm i @sendgrid/mail
- Sending emails by using library:
const http = require("http");const PORT = 3000;http.createServer((req, res) => {// Initializing sendgrid objectconst mailer = require("@sendgrid/mail");// Insert your API key heremailer.setApiKey("<your-api-key>");// Setting configurationsconst msg = {to: ["youremail@gmail.com","your.second.email@gmail.com"],from:"noreply@example.com",subject:"Message sent for demo purpose",html:"<h1>New message from Geeksforgeeks</h1><p>Some demo text from geeksforgeeks.</p>"};// Sending mailmailer.send(msg,function(err, json) {if(err) {console.log(err);// Writing error messageres.write("Can't send message sent");}else{// Writing success messageres.write("Message sent");}});res.end();}).listen(PORT, () => console.log(`Server running on PORT : ${PORT}`));chevron_rightfilter_noneNow run the app again by using
node indexand go to127.0.0.1:3000in the browser and check both of your emails, you’ll see an output like below.
Recommended Posts:
- Sending bulk SMS in Node.js using Twilio
- Sending Emails Using API in Flask-Mail
- Nodejs | Automatic restart NodeJs server with nodemon
- Sending SMS using NEXMO API in Node.js
- PHP | Sending mails using mail() function
- Sending Push Notifications : Progressive Web Apps
- Nodejs | DNS
- Web-Socket in NodeJS
- Nodejs | Jimp
- Nodejs | DNS | setServers()
- Nodejs | GM implode() Function
- Nodejs | GM solarize() Function
- Nodejs | GM edge() Function
- Nodejs | GM spread() Function
- Nodejs | GM operator() Function
- Nodejs | GM randomThreshold() Function
- Dockerizing a simple Nodejs app
- Nodejs | GM recolor() Function
- Nodejs | GM chop() Function
- Nodejs | GM enhance() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

