Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

sendgrid

sendgrid230.6kMITdeprecated5.2.3TypeScript support: included

Please see v6.X+ at https://www.npmjs.com/org/sendgrid

Official SendGrid NodeJS library.

readme

THIS PACKAGE HAS MOVED

Please see the latest official package here, thank you!

BuildStatus npm version Email Notifications Badge

NEW: Subscribe to email notifications for releases and breaking changes.

This library allows you to quickly and easily use the SendGrid Web API v3 via Node.js.

Version 3.X.X+ of this library provides full support for all SendGrid Web API v3 endpoints, including the new v3 /mail/send.

This library represents the beginning of a new path for SendGrid. We want this library to be community driven and SendGrid led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create issues and pull requests or simply upvote or comment on existing issues or pull requests.

Please browse the rest of this README for further detail.

We appreciate your continued support, thank you!

Table of Contents

Installation

Prerequisites

  • Node.js version 4, 6 or 7
  • The SendGrid service, starting at the free level

Setup Environment Variables

Update the development environment with your SENDGRID_API_KEY, for example:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

Install Package

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.

npm install --save sendgrid

Dependencies

Quick Start

Hello Email

The following is the minimum needed code to send an email with the /mail/send Helper (here is a full example):

With Mail Helper Class

var helper = require('sendgrid').mail;
var fromEmail = new helper.Email('test@example.com');
var toEmail = new helper.Email('test@example.com');
var subject = 'Sending with SendGrid is Fun';
var content = new helper.Content('text/plain', 'and easy to do anywhere, even with Node.js');
var mail = new helper.Mail(fromEmail, subject, toEmail, content);

var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var request = sg.emptyRequest({
  method: 'POST',
  path: '/v3/mail/send',
  body: mail.toJSON()
});

sg.API(request, function (error, response) {
  if (error) {
    console.log('Error response received');
  }
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
});

The Mail constructor creates a personalization object for you. Here is an example of how to add to it.

Without Mail Helper Class

The following is the minimum needed code to send an email without the /mail/send Helper (here is a full example):

var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var request = sg.emptyRequest({
  method: 'POST',
  path: '/v3/mail/send',
  body: {
    personalizations: [
      {
        to: [
          {
            email: 'test@example.com'
          }
        ],
        subject: 'Sending with SendGrid is Fun'
      }
    ],
    from: {
      email: 'test@example.com'
    },
    content: [
      {
        type: 'text/plain',
        value: 'and easy to do anywhere, even with Node.js'
      }
    ]
  }
});

// With promise
sg.API(request)
  .then(function (response) {
    console.log(response.statusCode);
    console.log(response.body);
    console.log(response.headers);
  })
  .catch(function (error) {
    // error is an instance of SendGridError
    // The full response is attached to error.response
    console.log(error.response.statusCode);
  });

// With callback
sg.API(request, function (error, response) {
  if (error) {
    console.log('Error response received');
  }
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
});

General v3 Web API Usage

var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);

// GET Collection
var request = sg.emptyRequest({
  method: 'GET',
  path: '/v3/api_keys'
});

// With promise
sg.API(request)
  .then(function (response) {
    console.log(response.statusCode);
    console.log(response.body);
    console.log(response.headers);
  })
  .catch(function (error) {
    // error is an instance of SendGridError
    // The full response is attached to error.response
    console.log(error.response.statusCode);
  });

// With callback
sg.API(request, function (error, response) {
  if (error) {
    console.log('Error response received');
  }
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
});

Usage

Use Cases

Examples of common API use cases, such as how to send an email with a transactional template.

Announcements

Please see our announcement regarding breaking changes. Your support is appreciated!

All updates to this library are documented in our CHANGELOG and releases. You may also subscribe to email release notifications for releases and breaking changes.

Roadmap

If you are interested in the future direction of this project, please take a look at our open issues and pull requests. We would love to hear your feedback.

How to Contribute

We encourage contribution to our libraries (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

Troubleshooting

Please see our troubleshooting guide for common library issues.

About

sendgrid-nodejs is guided and supported by the SendGrid Developer Experience Team.

sendgrid-nodejs is maintained and funded by SendGrid, Inc. The names and logos for sendgrid-nodejs are trademarks of SendGrid, Inc.

SendGrid Logo

changelog

Change Log

All notable changes to this project will be documented in this file.

[5.2.2] - 2017-08-14

Fix

  • Pull #418: Fixed #417: fix: Update TypeScript definition to correct callback arguments for API
  • Thanks to Benjamin Pannell for the PR!

[5.2.1] - 2017-08-09

Fixed

  • Pull #413: Handle null and undefined values in substitutions
  • Thanks to Matteo Ferrando for the PR!

[5.2.0] - 2017-07-20

Fixed

  • Pull #410: Cast substitution values to strings
  • Thanks to Ryan James for the PR!

[5.1.2] - 2017-06-30

Fixed

[5.1.1] - 2017-06-1

Added

  • Pull #391
  • Return personalizations as an array of request compatible JSON.
  • Thanks to Paul Hrimiuc for the PR!

[5.1.0] - 2017-05-3

Added

  • Pull #325
  • Run prism for tests
  • Now the mock SendGrid server, powered by Prism is automated locally and on Travis
  • Thanks to Mike Ralphson for the PR!

[5.0.1] - 2017-04-24

Fixed

  • Pull #385
  • Remove JSON.parse() around response bodies in contact-importer
  • Thanks to Eemeli Aro for the PR!

[5.0] - 2017-03-31

BREAKING CHANGE

  • Pull #328
  • Drop 0.10, 0.12 from supported Node.js versions, add 6
  • Thanks to Mike Ralphson for the PR!

[4.10] - 2017-03-30

Added

  • Pull #303
  • Add Inbound Parse data parser
  • Thanks to Jamie for the PR!

[4.9] - 2017-03-28

Added

  • Pull #371
  • Catch error in sendgrid, partially solves #370
  • Thanks to gf for the PR!

[4.8.4] - 2017-03-14

Fixed

  • Pull #348
  • Fix typescript definition for the ClickTracking
  • Thanks to dhenriques for the PR!

[4.8.3] - 2017-03-14

Fixed

  • Pull #368, Fixes #367
  • Personalization.getSubstitutions() return type incorrect in index.d.ts TypeScript definition file
  • Thanks to Brian Love for the PR!

[4.8.2] - 2017-03-14

Fixed

  • Pull #338
  • Fix function name in mail helper and fix the corresponding test
  • Thanks to Seoker Wang for the PR!

[4.8.1] - 2017-03-13

Fixed

  • Pull #354
  • fix: Make various TypeScript request components optional
  • Thanks to Benjamin Pannell for the PR!

[4.8.0] - 2017-03-10

Added

  • Pull #333
  • [TypeDefinition] SendGrid.API should return Promise<T> instead of PromiseLike<T>
  • Thanks to Ayman Nedjmeddine for the PR!

[4.7.1] - 2016-10-25

Added

[4.7.0] - 2016-10-14

Added

  • Pull #323, Fixed issue #317
  • Updates nodejs-http-client dependency to v2.3.0
  • Invoke the API callback with a mocked response upon Error
  • Thanks to Huli for the PR!

[4.6.0] - 2016-10-13

Added

  • Pull #319, Fixed issue #266
  • Converts response.body to have valid json objects
  • Thanks to Hugo Durães for the pull request!

[4.5.0] - 2016-10-05

Added

[4.4.1] - 2016-09-27

Fixed

[4.4.0] - 2016-09-27

Added

[4.3.1] - 2016-09-27

Fixed

[4.3.0] - 2016-09-15

Added

[4.2.1] - 2016-09-15

Fixed

[4.2.0] - 2016-09-14

Added

[4.1.0] - 2016-09-09

Added

[4.0.2] - 2016-08-24

Added

  • Table of Contents in the README
  • Added a USE_CASES.md section, with the first use case example for transactional templates

[4.0.1] - 2016-08-02

Fixed

  • Pull request #264: Fixed backwards compatability with Node.js versions 0.10 and 0.12
  • Use var instead of let
  • Check if Promise is defined

[4.0.0] - 2016-08-02

Breaking Change

Fixed

Added

  • Extracted some logic into helpers
  • Using a getEmptyRequest helper to avoid code duplication
  • emtpyRequest now accepts an object with data to extend the empty request with, this will allow simpler syntax for initializing requests.
  • Callback function now receives two parameters as per Node conventions (error, response)
  • If no callback provided, the method will return a promise instead.
  • Implemented promise API when not passing a callback function
  • Using native Promise by default if present, but allow users to override this with any other implementation by setting Sendgrid.Promise to any value, e.g. Sendgrid.Promise = require('bluebird')

[3.0.11] - 2016-07-26

Added

[3.0.10] - 2016-07-23

Fixed

[3.0.9] - 2016-07-20

Added

  • README updates
  • Update introduction blurb to include information regarding our forward path
  • Update the v3 /mail/send example to include non-helper usage
  • Update the generic v3 example to include non-fluent interface usage

[3.0.8] - 2016-07-12

Added

  • Update docs, unit tests and examples to include Sender ID

[3.0.7] - 2016-07-05

Updated

[3.0.6] - 2016-07-01

Fixed

  • GET suppression/bounces needs header to be Accept: application/json

[3.0.5] - 2016-06-14

Fixed

[3.0.4] - 2016-06-14

Fixed

  • Fixed exports and README example

[3.0.3] - 2016-06-14

Added

  • Moving mail helper export

[3.0.2] - 2016-06-14

Added

  • Added mail helper

[3.0.1] - 2016-06-14

Fixed

  • Missing index.js

[3.0.0] - 2016-06-13

Added

  • Breaking change to support the v3 Web API
  • New HTTP client
  • v3 Mail Send helper

[2.0.0] - 2015-10-13

Fixed

  • Add cc now uses the WebAPI instead of the SMTPApi. Read disclaimer for details

[1.9.1] - 2015-7-20

Changed

  • Pinned request version to be less than 2.59.0 because it broke something

[1.9.0] - 2015-7-07

Added

  • setFromName function #175

[1.8.0] - 2015-5-06

Added

  • addBcc and setBccs functions

[1.7.0] - 2015-4-27

Added

  • Support for API keys

[1.6.1] - 2015-4-5

Added

  • Updated version of lodash for optimizations #158
  • Temporarily make travis use npm 2.7.6 3e16a2

[1.6.0] - 2015-2-3

Added

  • ASM Group ID support
  • CHANGELOG.md