Code Monkey home page Code Monkey logo

node-sparkpost's Introduction

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 Slack Status

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. For versions < 4, please continue using sparkpost v1.3.8

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
  • 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>');

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

node-sparkpost's People

Contributors

aaron-em avatar aimeeknight avatar artlogic avatar avigoldman avatar aydrian avatar bdeanindy avatar beardyman avatar bizob2828 avatar coldacid avatar colestrode avatar creatovisguru avatar danieljuhl avatar ewandennis avatar gpittarelli avatar jasonrhodes avatar jimlynchcodes avatar lfreneda avatar mary-grace avatar mstdokumaci avatar orval avatar richleland avatar rnzo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.