• Stars
    star
    180
  • Rank 213,097 (Top 5 %)
  • Language
    JavaScript
  • License
    Other
  • Created about 10 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SparkPost client library for Node.js

Sign up for a SparkPost account and visit our Developer Hub for even more content.

Node.js Client Library

Travis CI Coverage Status NPM version

The official Node.js binding for your favorite SparkPost APIs!

Prerequisites

Before using this library, you must have:

Installation

npm install sparkpost

Note: Node.js versions 0.10 and 0.12 are no longer supported.

Initialization

new SparkPost(apiKey[, options]) - Initialization

  • apiKey
    • Required: yes (unless key is stored in SPARKPOST_API_KEY environment variable)
    • Type: String
    • a passed in apiKey will take precedence over an environment variable
  • options.origin or options.endpoint
    • Required: no
    • Type: String
    • Default: https://api.sparkpost.com:443
      Note: To use the SparkPost EU API you will need to set this to https://api.eu.sparkpost.com:443.
  • options.apiVersion
    • Required: no
    • Type: String
    • Default: v1
  • options.stackIdentity
    • Required: no
    • Type: String
    • An optional identifier to include in the User-Agent header. e.g. product/1.0.0
  • options.headers
    • Required: no
    • Type: Object
    • set headers that apply to all requests
  • options.debug
    • Required: no
    • Type: Boolean
    • Default: false
    • appends full response from request client as debug when true for debugging purposes
      Note: This will expose your api key to the client-side. Do not use in production.

Methods

Note: All methods return promises and accept an optional last argument callback. Read about how we handle callbacks and promises.

  • request(options[, callback])
    • options - see request modules options
    • options.uri - can either be a full url or a path that is appended to options.origin used at initialization (url.resolve)
    • options.debug - setting to true includes full response from request client for debugging purposes
  • get | post | put | delete(options[, callback])
    • options - see request options
    • Request method will be overwritten and set to the same value as the name of these methods.

Creating a SparkPost Client

Passing in an API key

const SparkPost = require('sparkpost');
const client = new SparkPost('YOUR_API_KEY');

Using an API key stored in an environment variable

//Create an env var as SPARKPOST_API_KEY
const SparkPost = require('sparkpost');
const client = new SparkPost();

Specifying non-default options

const SparkPost = require('sparkpost');
const options = {
  endpoint: 'https://dev.sparkpost.com:443'
};
const client = new SparkPost('YOUR_API_KEY', options);

Using the Node Client Library Base Object

We may not wrap every resource available in the SparkPost Client Library, for example the Node Client Library does not wrap the Metrics resource, but you can use the Node Client Library Base Object to form requests to these unwrapped resources. Here is an example request using the base object to make requests to the Metrics resource. Here is an example request using the base object to make requests to the Metrics resource.

// Get a list of domains that the Metrics API contains data on.
const options = {
  uri: 'metrics/domains'
};

client.get(options)
  .then(data => {
    console.log(data);
  })
  .catch(err => {
    console.log(err);
  });

Send An Email "Hello World" Example

Below is an example of how to send a simple email. Sending an email is known as a transmission. By using the send method on the transmissions service that's available from the SparkPost object you instantiate, you can pass in an object with all the transmission attributes relevant to the email being sent. The send method will return a promise that will let you know if the email was sent successful and if not information about the error that occurred. If a callback is passed, it will be executed.

const SparkPost = require('sparkpost');
const client = new SparkPost('<YOUR API KEY>');

// If you have a SparkPost EU account you will need to pass a different `origin` via the options parameter:
// const euClient = new SparkPost('<YOUR API KEY>', { origin: 'https://api.eu.sparkpost.com:443' });

client.transmissions.send({
    options: {
      sandbox: true
    },
    content: {
      from: '[email protected]',
      subject: 'Hello, World!',
      html:'<html><body><p>Testing SparkPost - the world\'s most awesomest email service!</p></body></html>'
    },
    recipients: [
      {address: '<YOUR EMAIL ADDRESS>'}
    ]
  })
  .then(data => {
    console.log('Woohoo! You just sent your first mailing!');
    console.log(data);
  })
  .catch(err => {
    console.log('Whoops! Something went wrong');
    console.log(err);
  });

SparkPost API Resources Supported in Node Client Library

Click on the desired API to see usage and more information

Development

Setup

Run npm install inside the repository to install all the dev dependencies.

Testing

Once all the dependencies are installed, you can execute the unit tests using npm test

Contributing

Guidelines for adding issues

Our coding standards

Submitting pull requests

ChangeLog

See ChangeLog here

More Repositories

1

heml

HEML is an open source markup language for building responsive email.
JavaScript
4,329
star
2

php-sparkpost

SparkPost client library for PHP
PHP
195
star
3

matchbox

πŸ”₯ A react UI component library
TypeScript
109
star
4

python-sparkpost

SparkPost client library for Python
Python
92
star
5

gosparkpost

SparkPost client library for the Go Programming Language
Go
62
star
6

elixir-sparkpost

SparkPost client library for Elixir https://developers.sparkpost.com
Elixir
44
star
7

java-sparkpost

SparkPost client library for Java
Java
39
star
8

nodemailer-sparkpost-transport

Sparkpost transport for Nodemailer
JavaScript
26
star
9

wordpress-sparkpost

WordPress plugin to use SparkPost email
PHP
20
star
10

pizza-bot

Learn how to order πŸ• from Slack
JavaScript
20
star
11

developers.sparkpost.com

SparkPost Developer Site and Documentation
API Blueprint
18
star
12

heml.io

HEML website
JavaScript
17
star
13

code-snippets

A repository with helpful programming resources for SparkPost developers.
JavaScript
17
star
14

bouncy-sink

A PMTA-based sink application that does opens, clicks, bounces, OOBs and FBLs
Python
16
star
15

github-alerts-to-slack

Sends a report of Github security alerts to Slack
JavaScript
11
star
16

gitplus

Generic tools and instructions for making it easier to work with git as a team.
Shell
11
star
17

support-docs

SparkPost support articles
TypeScript
11
star
18

auditmated

Automated npm auditing
Shell
10
star
19

gimli

Gimli is a crash tracing/analysis framework. It is available under a 3-clause BSD style license.
C
9
star
20

sp-forwarding-service

A small Heroku service that will consume inbound message webhook POSTs and forward them through SparkPost.
JavaScript
8
star
21

raffles

Email-driven raffle service
JavaScript
8
star
22

slack-etiquette

Kind of like Emily Post, but for Slack
7
star
23

umem

Portable umem Slab allocator
C
7
star
24

sparkpost-cli

A CLI to perform various tasks using the SparkPost API
Go
6
star
25

mandrill-sparkpost-templates

Translate Mandrill Handlebars templates into SparkPost templates
JavaScript
6
star
26

eriksen

A model marshaling library for dual-write/single-read data migration
JavaScript
6
star
27

template-library

A collection of example email templates for use with SparkPost
HTML
5
star
28

node-sparkpost-cli

A command-line interface to SparkPost.
JavaScript
5
star
29

elixir-webhook-sample

A report of recipient OS usage from SparkPost click events and the User-Agent header, with Elixir and Phoenix
Elixir
4
star
30

sparkpost-csv2tx

BASH script to import a CSV and convert it to a Recipient List and delivery a Transmission with SparkPost.
Shell
3
star
31

community-bot

A bot for the SparkPost community Slack team
JavaScript
3
star
32

event-data

self-hosted message events
JavaScript
3
star
33

azure-csharp-webhook-sample

A .NET Core sample that receives and processes SparkPost webhook events
C#
3
star
34

httpdump

Store HTTP request data, and provide a simple framework for post-processing in parallel.
Go
3
star
35

momentum-documentation

Repository for Momentum Documentation
JavaScript
3
star
36

csharp

Community-maintained C# SparkPost client library
3
star
37

eslint-config-sparkpost

ESLint configuration for Javascript based SparkPost projects
JavaScript
3
star
38

mbot

our hubot
CoffeeScript
2
star
39

sparkyEvents-Gmail-bounces-Dec2020

Python
2
star
40

postman-collection

A Postman collection for SparkPost
2
star
41

campaign-sparkpost

✨ SparkPost email provider for Campaign
JavaScript
2
star
42

momentum-scriptlets

Lua
2
star
43

msys-jlog-tools

Miscellaneous tools for JLog (journalled log) files
Perl
2
star
44

momentum_sample_policy

Example policy for Momentum
Perl
2
star
45

sparkybluemix-poc

An example of using Sparkpost.com for sending email via IBM Bluemix PaaS
JavaScript
2
star
46

AzureFunctionsWebhooksDemo

Consume webhooks with Azure Functions
C#
1
star
47

design.sparkpost.com

SparkPost Design Documentation Site
HTML
1
star
48

bower-sinon

Bower repo for Sinon.js
JavaScript
1
star
49

styles

CSS
1
star
50

lua-policies

Example business policies, implemented in the Momentum Business Policy Framework
Lua
1
star
51

relaymsgdb

Persist, parse, and present subject lines, organized by sender localpart
Go
1
star
52

node-red-contrib-sparkpost

Letting SparkPost go with the flow in node-red
JavaScript
1
star
53

AnalyticsTakeHomeTest

A place to showcase your abilities
1
star
54

codeland

1
star
55

webhook-report-sample

Build a report of recipient OS usage from SparkPost click events and the User-Agent header
PHP
1
star
56

events-search-example

Example code for integrating with SparkPost Events Search API
JavaScript
1
star
57

ux-github-slack-alerts

JavaScript
1
star
58

puppet-openresty

Ruby
1
star
59

appengine-flexible-python-sample

Sample app using Python, Flask and SparkPost in the Google App Engine flexible environment
Python
1
star