Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Event Webhook Node.js Code Example



Parse Webhook

parse-webhook page anchor

In this example, we want to parse all emails at address@email.sendgrid.biz and post the parsed email to http://sendgrid.biz/parse. We will be using Node and the Express framework.

Given this scenario, the following are the parameters you would set at the Parse API settings page(link takes you to an external page):


_10
Hostname: email.sendgrid.biz


_10
URL: http://sendgrid.biz/parse

To test this scenario, we sent an email to example@example.com and created the following code:


_24
_24
var express = require('express');
_24
var multer = require('multer');
_24
var app = express();
_24
_24
app.configure(function(){
_24
app.set('port', process.env.PORT || 3000);
_24
app.use(multer());
_24
});
_24
_24
app.post('/parse', function (req, res) {
_24
var from = req.body.from;
_24
var text = req.body.text;
_24
var subject = req.body.subject;
_24
var num_attachments = req.body.attachments;
_24
for (i = 1; i <= num_attachments; i++){
_24
var attachment = req.files['attachment' + i];
_24
// attachment will be a File object
_24
}
_24
});
_24
_24
var server = app.listen(app.get('port'), function() {
_24
console.log('Listening on port %d', server.address().port);
_24
});


To use the Event Webhook, you must first setup Event Notification.

In this scenario, we assume you've set the Event Notification URL to go the endpoint /event on your server. Given this scenario the following code will allow you to process events:


_20
_20
var express = require('express');
_20
var app = express();
_20
_20
app.configure(function(){
_20
app.set('port', process.env.PORT || 3000);
_20
app.use(express.bodyParser());
_20
});
_20
_20
app.post('/event', function (req, res) {
_20
var events = req.body;
_20
events.forEach(function (event) {
_20
// Here, you now have each event and can process them how you like
_20
processEvent(event);
_20
});
_20
});
_20
_20
var server = app.listen(app.get('port'), function() {
_20
console.log('Listening on port %d', server.address().port);
_20
});


Rate this page: