Search code examples
node.jssendgridsendgrid-api-v3

SendGrid Email delivery status API


I am current using the SendGrid email API with Node.js to send an email using their sample code on GitHub.

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'test@example.com',
  from: 'test@example.com',
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);

I would like to be able to show the delivery status of this email in my app. Is this possible using the SendGrid API and if so, how should I do this?

Thank you


Solution

  • I am not sure if you are looking for this, But they have email activity API which you have to subscribe separately. email activity

    var http = require("https");
    
    var options = {
      "method": "GET",
      "hostname": "api.sendgrid.com",
      "port": null,
      "path": "/v3/messages?query=status="processed" AND to_email="<<email>>"",
      "headers": {
        "authorization": "Bearer <<YOUR_API_KEY_HERE>>"
      }
    };
    
        var req = http.request(options, function (res) {
          var chunks = [];
    
          res.on("data", function (chunk) {
            chunks.push(chunk);
          });
    
          res.on("end", function () {
            var body = Buffer.concat(chunks);
            console.log(body.toString());
          });
        });
    
        req.write("{}");
        req.end();
    

    Also, check this for compund query: https://sendgrid.com/docs/for-developers/sending-email/getting-started-email-activity-api/

    Some of email activity : enter image description here