Node.js server using Hapi framework to get callbacks from SendGrid.
SendGrid sends 4 attributes in POST which we are saving to MySQL database:
- smtp-id
- category
- timestamp
- event
For details about SendGrid's webhook documentation visit http://sendgrid.com/docs/API_Reference/Webhooks/event.html.
Install hapi
and the other dependencies (as listed in package.json
) from npm
with command
npm install
The callbacks will be available with POST request on URL /callback
Launch the application (node .
) and run cURL with POST data:
curl -X POST http://localhost:8000/callback -d "email=test@example.com×tamp=1390078739&event=open&smtp-id=32131231&category=all"
Since we are running this server on Amazon EC2 under a load balancer, it requires a healthy check to be set up. Our healthy check will connect to database and select last inserted row to see if the database connection can be established.
For this healthy check to work, you need to:
- check table name in
config.js
- it should match the table name you are using to save the callbacks - set up healthy check in Amazon AWS load balancer. It is be available with GET request on URL
/healthy-check
Don't forget to create config.js
file before you launch the application. You have two options:
- copy
config.template.js
toconfig.js
and update parameters - create a symlink to another directory with other config files, i.e.
ln -s /srv/SecretConfig/production/config/config.sendgrid.js ./config.js
We recommend running it on a server with forever
which is a great tool making sure it will run continuously, literaly, forever :-) See more info about forever in the documentation.
This is what we use to run it on our server:
forever start --minUptime 1000 --spinSleepTime 5000 /srv/nodejs/SendGrid/index.js
Copyright (c) 2014 Hotel Quickly Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.