Simple Sendgrid Example using Node

Published

Here is a very simple example of how to create a simple Sendgrid app using NodeJS.

Change directory into your node app.

cd /name/of/node/app

Install Sendgrid using Node Package Manager.

npm i sendgrid

Use your Sendgrid username and password to send an email.

var sendgrid  = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD);

exports.send = function(req, res, next){ 
    if(req.method == "POST"){
        var SENDGRID = {
            USER: "<<username>>", 
            KEY:  "<<key>>", 
            TO:   "<< Add Custom email>>", 
            FROM: "noreply@myapp.com" 
        };
        sendgrid.send({
            to      : SENDGRID.TO,
            from    : SENDGRID.FROM,
            subject : 'Feedback',
            text    : JSON.stringify(req.body)
        },
        function(err, json) {
            if (err) { return console.error(err); }
            console.log(json);
        });       
    }else{
        console.log("routes/index:", 'feedback');
    }
};

Comments