Skip to contentSkip to navigationSkip to topbar
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):

Hostname: email.sendgrid.biz
URL: http://sendgrid.biz/parse

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

1
2
var express = require('express');
3
var multer = require('multer');
4
var app = express();
5
6
app.configure(function(){
7
app.set('port', process.env.PORT || 3000);
8
app.use(multer());
9
});
10
11
app.post('/parse', function (req, res) {
12
var from = req.body.from;
13
var text = req.body.text;
14
var subject = req.body.subject;
15
var num_attachments = req.body.attachments;
16
for (i = 1; i <= num_attachments; i++){
17
var attachment = req.files['attachment' + i];
18
// attachment will be a File object
19
}
20
});
21
22
var server = app.listen(app.get('port'), function() {
23
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:

1
2
var express = require('express');
3
var app = express();
4
5
app.configure(function(){
6
app.set('port', process.env.PORT || 3000);
7
app.use(express.bodyParser());
8
});
9
10
app.post('/event', function (req, res) {
11
var events = req.body;
12
events.forEach(function (event) {
13
// Here, you now have each event and can process them how you like
14
processEvent(event);
15
});
16
});
17
18
var server = app.listen(app.get('port'), function() {
19
console.log('Listening on port %d', server.address().port);
20
});