Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

SendGrid Node.js helper library


This nodejs module allows you to quickly and easily send emails through SendGrid using nodejs.

WARNING: This module was recently upgraded from 0.4.x to 1.0.1. There were API breaking changes for various method names. See usage for up to date method names.

BuildStatus NPM version

var sendgrid  = require('sendgrid')(api_user, api_key);
  to:       '',
  from:     '',
  subject:  'Hello World',
  text:     'My first email through SendGrid.'
}, function(err, json) {
  if (err) { return console.error(err); }


The following recommended installation requires npm. If you are unfamiliar with npm, see the npm docs. Npm comes installed with Node.js since node version 0.8.x therefore you likely already have it.

Add the following to your package.json file:

  "dependencies": {
    "sendgrid": "1.0.3"

Install sendgrid-nodejs and its dependencies:

npm install

Alternative Installation

You can also install sendgrid locally with the following command:

npm install sendgrid

Example App

There is a sendgrid-nodejs-example app to help jumpstart your development.


To begin using this library, initialize the SendGrid object with your SendGrid credentials.

var sendgrid  = require('sendgrid')(api_user, api_key);

Create a new JavaScript object with your message details.

var payload   = {
  to      : '',
  from    : '',
  subject : 'Saying Hi',
  text    : 'This is my first email through SendGrid'

Send it.

sendgrid.send(payload, function(err, json) {
  if (err) { console.error(err); }


Email helps you more powerfully prepare your message to be sent.

To get started create an Email object where params is a javascript object. You can pass in as much or as little to params as you want.

var sendgrid  = require('sendgrid')(api_user, api_key);
var email     = new sendgrid.Email(params);


Here is a sample for using it.

var sendgrid  = require('sendgrid')(api_user, api_key);
var email     = new sendgrid.Email({
  to:       '',
  from:     '',
  subject:  'Subject goes here',
  text:     'Hello world'
sendgrid.send(email, function(err, json) {
  if (err) { return console.error(err); }

Available params

var params = {
  smtpapi:  new sengrid.SmtpapiHeaders(),
  to:       [],
  toname:   [],
  from:     '',
  fromname: '',
  subject:  '',
  text:     '',
  html:     '',
  bcc:      [],
  replyto:  '',
  date:     new Date(),
  files: [
      filename:     '',           // required only if file.content is used.
      contentType:  '',           // optional
      cid:          '',           // optional, used to specify cid for inline content
      path:         '',           //
      url:          '',           // == One of these three options is required
      content:      ('' | Buffer) //
  file_data:  {},
  headers:    {}

NOTE: anything that is available in the Email constructor is available for use in the sendgrid.send function.

Setting params

You can set params like you would for any standard JavaScript object.

var sendgrid  = require('sendgrid')(api_user, api_key);
var email     = new sendgrid.Email({to: ''});      = "";
email.replyto = "";
email.subject = "This is a subject";


You can add one or multiple TO addresses using addTo.

var email     = new sendgrid.Email(); 
sendgrid.send(email, function(err, json) { });

NOTE: This is different than setting an array on to. The array on to will show everyone the to addresses it was sent to. Using addTo will not. Usually, you'll want to use addTo.


var email     = new sendgrid.Email(); 
sendgrid.send(email, function(err, json) { });


var email     = new sendgrid.Email(); 
email.setSubject('Some subject');
sendgrid.send(email, function(err, json) { });


var email     = new sendgrid.Email(); 
email.setText('Some text');
sendgrid.send(email, function(err, json) { });


var email     = new sendgrid.Email(); 
email.setHtml('<h1>Some html</h1>');
sendgrid.send(email, function(err, json) { });


You can add custom headers. This will ADD rather than SET headers.

var email     = new sendgrid.Email(); 
email.setHeaders({full: 'hearts'});   // headers = {full: 'hearts'}
email.addHeader('spin', 'attack');   // headers = {full: 'hearts', spin: 'attack'}
email.addHeader('mask', 'salesman'); // headers = {full: 'hearts', spin: 'attack', mask: 'salesman'}
sendgrid.send(email, function(err, json) { });


You can set custom headers.

var email     = new sendgrid.Email(); 
email.setHeaders({full: 'hearts'});   // headers = {full: 'hearts'}
email.setHeaders({mask: 'salesman'}); // headers = {mask: 'salesman'}
sendgrid.send(email, function(err, json) { });


var email     = new sendgrid.Email();
email.addSubstitution('keep', 'secret'); // sub = {keep: ['secret']}
email.addSubstitution('other', ['one', 'two']);   // sub = {keep: ['secret'], other: ['one', 'two']}


var email     = new sendgrid.Email();
email.setSubstitutions({keep: ['secret'], other: ['one', 'two']}); // sub = {keep: ['secret'], other: ['one', 'two']});


var email     = new sendgrid.Email();
email.addSection({'-charge-': 'This ship is useless.'}); // section = {'-charge-': 'This ship is useless.'}


var email     = new sendgrid.Email();
email.setSections({'-charge-': 'This ship is useless.'}); // section = {'-charge-': 'This ship is useless.'}


var email     = new sendgrid.Email();
email.setUniqueArg({cow: 'chicken'}); // unique_args = {cow: 'chicken'}
email.addUniqueArg({cat: 'dog'});     // unique_args = {cow: 'chicken', cat: 'dog'}


var email     = new sendgrid.Email();
email.setUniqueArgs({cow: 'chicken'}); // unique_args = {cow: 'chicken'}
email.setUniqueArgs({dad: 'proud'});   // unique_args = {dad: 'proud'}


var email     = new sendgrid.Email();
email.addCategory('tactics');        // category = ['tactics']
email.addCategory('advanced');       // category = ['tactics', 'advanced']


var email     = new sendgrid.Email();
email.setCategories(['tactics']);        // category = ['tactics']
email.setCategories(['snowball-fight']); // category = ['snowball-fight']


Alternatively, you can add filter settings one at a time.

var email     = new sendgrid.Email();
email.addFilter('footer', 'enable', 1);
email.addFilter('footer', 'text/html', '<strong>boo</strong>');


You can set a filter using an object literal.

var email     = new sendgrid.Email();
  'footer': {
    'setting': {
      'enable': 1,
      'text/plain': 'You can haz footers!'


You can add files directly from content in memory. It will try to guess the contentType based on the filename.

  filename: 'secret.txt',
  content:  new Buffer('You will never know....')

You can add files directly from a url. It will try to guess the contentType based on the filename.

  filename: 'icon.jpg',
  url: ''

You can add files from a path on the filesystem. It will try to grap the filename and contentType from the path.

  path: '../files/resume.txt'

You can tag files for use as inline HTML content. It will mark the file for inline disposition using the specified "cid".

  cid: 'the_logo',           // should match cid value in html
  path: '../files/logo.png'
email.addHtml('<div>Our logo:<img src="cid:the_logo"></div>');


sendgrid-nodejs uses the node request module. You can pass in options to be merged. This enables you to use your own https.Agent, node-tunnel or the request proxy url. Please note that sendgrid requires https.

var sendgrid = require('sendgrid')('username', 'password', {
proxy: "http://localproxy:3128" });


var https = require('https');
var agent = new https.Agent();
// Set Max Sockets to 500
agent.maxSockets = 500;

var sendgrid = require('sendgrid')('username', 'password', { web: {
pool: agent } });


When filing an issue please include your package.json and the output of npm ls sendgrid to help us isolate and repro the issue


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Running Tests

The existing tests can be run using Mocha with the following command:

npm test

You can run individual tests with the following command:

./node_modules/.bin/mocha [path to test].js

Integration Tests

In order to run the integration tests, you'll need to update the environment file with your valid SendGrid credentials. Start by making a live copy of the example:

cp .env.example .env.test

Next, open up .env.test and fill it in. After you have updated the environment file with your credentials, you can run the suite using the following command:

npm test


Licensed under the MIT License.

Something went wrong with that request. Please try again.