diff --git a/README.md b/README.md index 877308e..cf903a8 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,16 @@ [![Travis CI](https://travis-ci.org/SparkPost/node-sparkpost.svg?branch=master)](https://travis-ci.org/SparkPost/node-sparkpost) -# SparkPost Node.js SDK +![SparkPost Build by MessageSystems](/docs/sparkpost_logo.png) +# Node.js SDK -The official node.js binding for your favorite SparkPost APIs! +The official Node.js binding for your favorite [SparkPost APIs](https://www.sparkpost.com/api)! -Before using this library, you must have a valid API Key. +## Prerequisites -To get an API Key, please log in to your SparkPost account and generate one in the Settings page. +Before using this library, you must have: + +* A shiny new SparkPost Account, [sign up for a new account](https://app.sparkpost.com/#/sign-up) or [login to SparkPost](https://app.sparkpost.com/) +* A valid SparkPost API Key. Check out our [Support Center](https://support.sparkpost.com/) for information on how to [create API keys](https://support.sparkpost.com/customer/portal/articles/1933377-create-api-keys) ## Installation @@ -14,91 +18,96 @@ To get an API Key, please log in to your SparkPost account and generate one in t npm install sparkpost ``` -## Getting Started: Your First Mailing - -```javascript -var SparkPost = require('sparkpost') - , client = new SparkPost('YOUR API KEY'); +## 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.headers` + * Required: no + * Type: `Object` + * set headers that apply to all requests + +## Methods +* **request(options, callback)** + * `options` - [see request modules options](https://github.com/mikeal/request#requestoptions-callback) + * `options.uri` - can either be a full url or a path that is appended to `options.origin` used at initialization ([url.resolve](http://nodejs.org/api/url.html#url_url_resolve_from_to)) + * `callback` - executed after task is completed. **required** + * standard `callback(err, data)` + * `err` - any error that occurred + * `data.res` - full response from request client + * `data.body` - payload from response +* **get | post | put | delete(options, callback)** + * `options` - see request options + * `callback` - 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 +```js +var SparkPost = require('sparkpost'); +var client = new SparkPost('YOUR_API_KEY'); +``` -var trans = {}; +Using an API key stored in an environment variable +```js +process.env.SPARKPOST_API_KEY = 'YOUR_API_KEY'; +var SparkPost = require('sparkpost'); +var client = new SparkPost(); +``` -// Set some metadata for your email -trans.campaign = 'first-mailing'; -trans.from = 'you@your-company.com'; -trans.subject = 'First SDK Mailing'; +Specifying non-default options +```js +var SparkPost = require('sparkpost'); +var options = { + endpoint: 'https://dev.sparkpost.com:443' +}; +var client = new SparkPost('YOUR_API_KEY', options); +``` -// Add some content to your email -trans.html = '

Congratulations, {{name}}!

You just sent your very first mailing!

'; -trans.text = 'Congratulations, {{name}}!! You just sent your very first mailing!'; -trans.substitutionData = {name: 'YOUR FIRST NAME'}; +## Using the Node SDK Base Object +We may not wrap every resource available in the SparkPost SDK, for example the Node SDK does not wrap the Metrics resource, +but you can use the Node SDK 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. -// Pick someone to receive your email -trans.recipients = [{ address: { name: 'YOUR FULL NAME', email: 'YOUR EMAIL ADDRESS' } }]; +```js +// Get a list of domains that the Metrics API contains data on. +var options = { + uri: 'metrics/domains' +}; -// Send it off into the world! -client.transmission.send(trans, function(err, res) { - if (err) { - console.log('Whoops! Something went wrong'); +client.get(options, function(err, data) { + if(err) { console.log(err); - } else { - console.log('Woohoo! You just sent your first mailing!'); + return; } + + console.log(data.body); }); ``` -## Learn More -* For more detailed examples, check our examples: - * [Transmissions](https://github.com/MessageSystems/node-sdk/blob/master/examples/transmission/) -* Read our REST API documentation - - -## Field Descriptions -### Transmissions -| Field Name | Required? | Description | Data Type | -| ------------ | ----------- | ------------- | ----------- | -| description | no | Field for describing what this transmission is for the user | String | -| campaign | no | Field for assigning a given transmission to a specific campaign, which is a logical container for similar transmissions | String | -| metadata | no | Field for adding arbitrary key/value pairs which will be included in open/click tracking | Object (Simple) | -| substitutionData | no | Field for adding transmission level substitution data, which can be used in a variety of fields and in content | Object (Complex) | -| trackOpens | no | Field for enabling/disabling transmission level open tracking, if not set will use settings from Template | Boolean | -| trackClicks | no | Field for enabling/disabling transmission level click tracking, if not set will use settings from Template | Boolean | -| useSandbox | no | Field for enabling/disabling using sandbox domain to send transmission(You are limited to 50 messages ever with sandbox) | Boolean | -| useDraftTemplate | no | Field for allowing the sending of a transmission using a draft of a stored template (default: false) | Boolean | -| replyTo | no | Field for specifying the email address that should be used when a recipient hits the reply button | String | -| subject | yes | Field for setting the subject line of a given transmission | String | -| from | yes | Field for setting the from line of a given transmission | String or Object | -| html | yes** | Field for setting the HTML content of a given transmission | String | -| text | yes** | Field for setting the Plain Text content of a given transmission | String | -| rfc822 | no** | Field for setting the RFC-822 encoded content of a given transmission | String | -| template | no** | Field for specifying the Template ID of a stored template to be used when sending a given transmission | String | -| customHeaders | no | Field for specifying additional headers to be applied to a given transmission (other than Subject, From, To, and Reply-To) | Object (Simple) | -| recipients | yes** | Field for specifying who a given transmission should be sent to | Array of Objects | -| recipientList | no** | Field for specifying a stored recipient list ID to be used for a given transmission | String | - -** - If using inline content then html or text are required. If using RFC-822 Inline Content, then rfc822 is required. If using a stored recipient list, then recipientList is required. If using a stored template, then template is required. - -### Sending Domains -| Field Name | Required? | Description | Data Type | -| ------------ | ----------- | ------------- | ----------- | -| domainName | yes | Name of the sending domain | String | -| privateKey | yes** | Private key used to create the DKIM Signature. | String | -| publicKey | yes** | Public key to be retrieved from the DNS of the sending domain. | String | -| selector | yes** | DomainKey selector that indicates the DKIM public key location. | String | -| headers | no | Header fields to be included in the DKIM signature | String | - -** - If specifying a privateKey, publicKey, or selector, all three fields are required. - -## Tips and Tricks -### General -* You _must_ provide at least an API key when instantiating the SparkPost Library - `{ key: '184ac5480cfdd2bb2859e4476d2e5b1d2bad079bf' }` -* The SDK's features are namespaced under the various SparkPost API names. - -### Transmissions -* If you specify a stored recipient list and inline recipients in a Transmission, you will receive an error. -* If you specify HTML and/or Plain Text content and then provide RFC-822 encoded content, you will receive an error. - * RFC-822 content is not valid with any other content type. -* If you specify a stored template and also provide inline content, you will receive an error. -* By default, open and click tracking are enabled for a transmission. -* By default, a transmission will use the published version of a stored template. +## SparkPost API Resources Supported in Node SDK +Click on the desired API to see usage and more information + +* [Recipient Lists](/docs/resources/recipientLists.md) - `client.recipientLists` ([examples](/examples/recipientLists)) +* [Sending Domains](/docs/resources/sendingDomains.md) - `client.sendingDomains` ([examples](/examples/sendingDomains)) +* [Suppression List](/docs/resources/suppressionList.md) - `client.suppressionList` ([examples](/examples/suppressionList)) +* [Templates](/docs/resources/templates.md) - `client.templates` ([examples](/examples/templates)) +* [Transmissions](/docs/apis/transmission.md) - `client.transmission` ([examples](/examples/transmission)) +* [Webhooks](/docs/resources/webhooks.md) - `client.webhooks` ([examples](/examples/webhooks)) + ## Development diff --git a/docs/resources/recipientLists.md b/docs/resources/recipientLists.md new file mode 100644 index 0000000..6e80351 --- /dev/null +++ b/docs/resources/recipientLists.md @@ -0,0 +1,45 @@ +# Recipient Lists + +This library provides easy access to the [Recipient Lists](https://www.sparkpost.com/api#/reference/recipient-lists/) Resource. + +## Methods +* **all(callback)** + List a summary of all recipient lists. + * `callback` - executed after task is completed. **required** + * standard `callback(err, data)` + * `err` - any error that occurred + * `data.res` - full response from request client + * `data.body` - payload from response +* **find(options, callback)** + Retrieve details about a specified recipient list by its id + * `options.id` - the id of the recipient list you want to look up **required** + * `options.show_recipients` - specifies whether to retrieve the recipients Default: `false` + * `callback` - see all function +* **create(options, callback)** + Create a new recipient list + * `options.recipients` - an array of recipients to add to the list **required** + * `options.num_rcpt_errors` - limit the number of recipient errors returned + * `callback` - see all function +* **delete(id, callback)** + Delete an existing recipient list + * `id` - the id of the recipient list you want to delete **required** + * `callback` - see all function + +## Examples + +```js +var SparkPost = require('sparkpost'); +var client = new SparkPost('YOUR_API_KEY'); + +client.recipientLists.all(function(err, data) { + if(err) { + console.log(err); + return; + } + + console.log(data.body); +}); + +``` + +Check out all the examples provided [here](/examples/recipientLists). diff --git a/docs/resources/sendingDomains.md b/docs/resources/sendingDomains.md new file mode 100644 index 0000000..d0d2bf4 --- /dev/null +++ b/docs/resources/sendingDomains.md @@ -0,0 +1,60 @@ +# Sending Domains + +This library provides easy access to the [Sending Domains](https://www.sparkpost.com/api#/reference/sending-domains/) Resource. + +## Methods +* **all(callback)** + List an overview of all sending domains in the account. + * `callback` - executed after task is completed. **required** + * standard `callback(err, data)` + * `err` - any error that occurred + * `data.res` - full response from request client + * `data.body` - payload from response +* **find(domainName, callback)** + Retrieve a sending domain by its domain name + * `domainName` - the name of the domain you want to look up **required** + * `callback` - see all function +* **create(domainBody, callback)** + Create a new sending domain + * `domainBody` - see object description below **required** + * `callback` - see all function +* **update(domainBody, callback)** + Update an existing sending domain + * `domainBody` - see object description below **required** + * `callback` - see all function +* **verify(options, callback)** + Validate the specified verification field types for a sending domain + * `options.domainName` - the name of the domain you want to verify **required** + * `options.verifyDKIM` - initiates a check against the DKIM record default: `true` + * `options.verifySPF` - initiates a check against the SPF record default: `true` + + +### domainBody +| Field Name | Required? | Description | Data Type | +| ---------- | --------- | --------------------------------------------------------------- | --------- | +| domainName | yes | Name of the sending domain | String | +| privateKey | yes** | Private key used to create the DKIM Signature. | String | +| publicKey | yes** | Public key to be retrieved from the DNS of the sending domain. | String | +| selector | yes** | DomainKey selector that indicates the DKIM public key location. | String | +| headers | no | Header fields to be included in the DKIM signature | String | + +** - If specifying a privateKey, publicKey, or selector, all three fields are required. + +## Examples + +```js +var SparkPost = require('sparkpost'); +var client = new SparkPost('YOUR_API_KEY'); + +client.sendingDomains.all(function(err, data) { + if(err) { + console.log(err); + return; + } + + console.log(data.body); +}); + +``` + +Check out all the examples provided [here](/examples/sendingDomains). diff --git a/docs/resources/suppressionList.md b/docs/resources/suppressionList.md new file mode 100644 index 0000000..978a50e --- /dev/null +++ b/docs/resources/suppressionList.md @@ -0,0 +1,46 @@ +# Suppression List + +This library provides easy access to the [Suppression List](https://www.sparkpost.com/api#/reference/suppression-list/) Resource. + +## Methods +* **search(parameters, callback)** + Perform a filtered search for entries in your suppression list. + * `parameters` - Object of [search parameters](https://www.sparkpost.com/api#/reference/suppression-list/search/search-for-suppression-list-entries) + * `callback` - executed after task is completed. **required** + * standard `callback(err, data)` + * `err` - any error that occurred + * `data.res` - full response from request client + * `data.body` - payload from response +* **checkStatus(email, callback)** + * `email` - `String` email address to check **required** + * `callback` - see search function +* **removeStatus(email, callback)** + * `email` - `String` email address to remove **required** + * `callback` - see search function +* **upsert(recipient, callback)** + * `recipient` - [Recipient Object](https://www.sparkpost.com/api#/reference/recipient-lists) or `Array` of Recipient Objects + * `callback` - see search function + +## Examples + +```js +var SparkPost = require('sparkpost'); +var client = new SparkPost('YOUR_API_KEY'); +var parameters = { + from: '2015-05-07T00:00:00+0000' + , to: '2015-05-07T23:59:59+0000' + , limit: 5 +}; + +client.suppressionList.search(parameters, function(err, data) { + if(err) { + console.log(err); + return; + } + + console.log(data.body); +}); + +``` + +Check out all the examples provided [here](/examples/suppressionList). diff --git a/docs/resources/templates.md b/docs/resources/templates.md new file mode 100644 index 0000000..2248cbc --- /dev/null +++ b/docs/resources/templates.md @@ -0,0 +1,50 @@ +# Templates + +This library provides easy access to the [Templates](https://www.sparkpost.com/api#/reference/templates/) Resource. + +## Methods +* **all(callback)** + List a summary of all templates. + * `callback` - executed after task is completed. **required** + * standard `callback(err, data)` + * `err` - any error that occurred + * `data.res` - full response from request client + * `data.body` - payload from response +* **find(options, callback)** + Retrieve details about a specified template by its id + * `options.id` - the id of the template you want to look up **required** + * `options.draft` - specifies a draft or published template + * `callback` - see all function +* **create(options, callback)** + Create a new recipient list + * `options.template` - a template object **required** + * `callback` - see all function +* **delete(id, callback)** + Delete an existing template + * `id` - the id of the template you want to delete **required** + * `callback` - see all function +* **preview(options, callback)** + Preview the most recent version of an existing template by id + * `options.id` - the id of the template you want to look up **required** + * `options.data` - Object of substitution data + * `options.draft` - specifies a draft or published template + * `callback` - see all function + +## Examples + +```js +var SparkPost = require('sparkpost'); +var client = new SparkPost('YOUR_API_KEY'); + +client.templates.all(function(err, data) { + if(err) { + console.log(err); + return; + } + + console.log(data.body); +}); + +``` + +Check out all the examples provided [here](/examples/templates). diff --git a/docs/resources/transmission.md b/docs/resources/transmission.md new file mode 100644 index 0000000..46826db --- /dev/null +++ b/docs/resources/transmission.md @@ -0,0 +1,83 @@ +# Transmissions + +This library provides easy access to the [Transmissions](https://www.sparkpost.com/api#/reference/transmissions/) Resource. + +## Methods +* **all(callback)** + List an overview of all transmissions in the account. + * `callback` - executed after task is completed. **required** + * standard `callback(err, data)` + * `err` - any error that occurred + * `data.res` - full response from request client + * `data.body` - payload from response +* **find(transmissionID, callback)** + Retrieve the details about a transmission by its ID + * `transmissionID` - the id of the transmission you want to look up **required** + * `callback` - see all function +* **send(transmissionBody, callback)** + + +## Getting Started: Your First Mailing + +```javascript +var SparkPost = require('sparkpost') + , client = new SparkPost('YOUR API KEY'); + +var trans = {}; + +// Set some metadata for your email +trans.campaign = 'first-mailing'; +trans.from = 'you@your-company.com'; +trans.subject = 'First SDK Mailing'; + +// Add some content to your email +trans.html = '

Congratulations, {{name}}!

You just sent your very first mailing!

'; +trans.text = 'Congratulations, {{name}}!! You just sent your very first mailing!'; +trans.substitutionData = {name: 'YOUR FIRST NAME'}; + +// Pick someone to receive your email +trans.recipients = [{ address: { name: 'YOUR FULL NAME', email: 'YOUR EMAIL ADDRESS' } }]; + +// Send it off into the world! +client.transmission.send(trans, function(err, res) { + if (err) { + console.log('Whoops! Something went wrong'); + console.log(err); + } else { + console.log('Woohoo! You just sent your first mailing!'); + } +}); +``` + +## Field Descriptions +### Transmissions +| Field Name | Required? | Description | Data Type | +| ------------ | ----------- | ------------- | ----------- | +| description | no | Field for describing what this transmission is for the user | String | +| campaign | no | Field for assigning a given transmission to a specific campaign, which is a logical container for similar transmissions | String | +| metadata | no | Field for adding arbitrary key/value pairs which will be included in open/click tracking | Object (Simple) | +| substitutionData | no | Field for adding transmission level substitution data, which can be used in a variety of fields and in content | Object (Complex) | +| trackOpens | no | Field for enabling/disabling transmission level open tracking, if not set will use settings from Template | Boolean | +| trackClicks | no | Field for enabling/disabling transmission level click tracking, if not set will use settings from Template | Boolean | +| useSandbox | no | Field for enabling/disabling using sandbox domain to send transmission(You are limited to 50 messages ever with sandbox) | Boolean | +| useDraftTemplate | no | Field for allowing the sending of a transmission using a draft of a stored template (default: false) | Boolean | +| replyTo | no | Field for specifying the email address that should be used when a recipient hits the reply button | String | +| subject | yes | Field for setting the subject line of a given transmission | String | +| from | yes | Field for setting the from line of a given transmission | String or Object | +| html | yes** | Field for setting the HTML content of a given transmission | String | +| text | yes** | Field for setting the Plain Text content of a given transmission | String | +| rfc822 | no** | Field for setting the RFC-822 encoded content of a given transmission | String | +| template | no** | Field for specifying the Template ID of a stored template to be used when sending a given transmission | String | +| customHeaders | no | Field for specifying additional headers to be applied to a given transmission (other than Subject, From, To, and Reply-To) | Object (Simple) | +| recipients | yes** | Field for specifying who a given transmission should be sent to | Array of Objects | +| recipientList | no** | Field for specifying a stored recipient list ID to be used for a given transmission | String | + +** - If using inline content then html or text are required. If using RFC-822 Inline Content, then rfc822 is required. If using a stored recipient list, then recipientList is required. If using a stored template, then template is required. + +## Tips and Tricks +* If you specify a stored recipient list and inline recipients in a Transmission, you will receive an error. +* If you specify HTML and/or Plain Text content and then provide RFC-822 encoded content, you will receive an error. + * RFC-822 content is not valid with any other content type. +* If you specify a stored template and also provide inline content, you will receive an error. +* By default, open and click tracking are enabled for a transmission. +* By default, a transmission will use the published version of a stored template. diff --git a/docs/resources/webhooks.md b/docs/resources/webhooks.md new file mode 100644 index 0000000..8421644 --- /dev/null +++ b/docs/resources/webhooks.md @@ -0,0 +1,59 @@ +# Webhooks + +This library provides easy access to the [Webhooks](https://www.sparkpost.com/api#/reference/webhooks/) Resource. + +## Methods +* **all(options, callback)** + List currently extant webhooks. + * `options.timezone` - `String` Standard timezone identification Default: `UTC` + * `callback` - executed after task is completed. **required** + * standard `callback(err, data)` + * `err` - any error that occurred + * `data.res` - full response from request client + * `data.body` - payload from response +* **describe(options, callback)** + Retrieve details about a specified webhook by its id + * `options.id` - the id of the webhook you want to describe **required** + * `options.timezone` - `String` Standard timezone identification Default: `UTC` + * `callback` - see all function +* **create(webhook, callback)** + Create a new recipient list + * `webhook` - a webhook object **required** + * `callback` - see all function +* **update(webhook, callback)** + Update an existing webhook + * `webhook` - a webhook object **required** + * `callback` - see all function +* **delete(id, callback)** + Delete an existing webhook + * `id` - the id of the webhook you want to delete **required** + * `callback` - see all function +* **validate(options, callback)** + Sends an example message event batch from the Webhook API to the target URL + * `options.id` - the id of the webhook you want to validate **required** + * `options.message` - sample object to send to the target URL **required** + * `callback` - see all function +* **getBatchStatus(options, callback)** + Sends an example message event batch from the Webhook API to the target URL + * `options.id` - the id of the webhook you want to get status on **required** + * `options.limit` - `number` maximum number of results to return Default: `1000` + * `callback` - see all function + +## Examples + +```js +var SparkPost = require('sparkpost'); +var client = new SparkPost('YOUR_API_KEY'); + +client.webhooks.all(function(err, data) { + if(err) { + console.log(err); + return; + } + + console.log(data.body); +}); + +``` + +Check out all the examples provided [here](/examples/webhooks). diff --git a/docs/sparkpost_logo.png b/docs/sparkpost_logo.png new file mode 100644 index 0000000..44c0690 Binary files /dev/null and b/docs/sparkpost_logo.png differ diff --git a/examples/baseObject/getDomainsList.js b/examples/baseObject/getDomainsList.js new file mode 100644 index 0000000..f3c3ea8 --- /dev/null +++ b/examples/baseObject/getDomainsList.js @@ -0,0 +1,17 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + uri: 'metrics/domains' + }; + +client.get(options, function(err, data) { + if(err) { + console.log(err); + return; + } + + console.log(data.body); +}); diff --git a/examples/recipientLists/create_recipientList.js b/examples/recipientLists/create_recipientList.js new file mode 100644 index 0000000..8f268f0 --- /dev/null +++ b/examples/recipientLists/create_recipientList.js @@ -0,0 +1,35 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'UNIQUE_TEST_ID' + , name: 'Test Recipient List' + , recipients: [ + { + address: { + email: 'test1@test.com' + } + } + , { + address: { + email: 'test2@test.com' + } + } + , { + address: { + email: 'test3@test.com' + } + } + ] + }; + +client.recipientLists.create(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/recipientLists/delete_recipientList.js b/examples/recipientLists/delete_recipientList.js new file mode 100644 index 0000000..afabc46 --- /dev/null +++ b/examples/recipientLists/delete_recipientList.js @@ -0,0 +1,14 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +client.recipientLists['delete']('UNIQUE_TEST_ID', function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/recipientLists/get_all_recipientLists.js b/examples/recipientLists/get_all_recipientLists.js new file mode 100644 index 0000000..6f24c7d --- /dev/null +++ b/examples/recipientLists/get_all_recipientLists.js @@ -0,0 +1,14 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +client.recipientLists.all(function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/recipientLists/get_recipientList.js b/examples/recipientLists/get_recipientList.js new file mode 100644 index 0000000..a4b1e82 --- /dev/null +++ b/examples/recipientLists/get_recipientList.js @@ -0,0 +1,17 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'UNIQUE_TEST_ID' + }; + +client.recipientLists.find(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/recipientLists/get_recipientList_with_recipients.js b/examples/recipientLists/get_recipientList_with_recipients.js new file mode 100644 index 0000000..15d2415 --- /dev/null +++ b/examples/recipientLists/get_recipientList_with_recipients.js @@ -0,0 +1,18 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'UNIQUE_TEST_ID' + , show_recipients: true + }; + +client.recipientLists.find(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/suppressionList/checkStatus.js b/examples/suppressionList/checkStatus.js new file mode 100644 index 0000000..888dc01 --- /dev/null +++ b/examples/suppressionList/checkStatus.js @@ -0,0 +1,14 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +client.suppressionList.checkStatus('test@test.com', function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/suppressionList/removeStatus.js b/examples/suppressionList/removeStatus.js new file mode 100644 index 0000000..75881f8 --- /dev/null +++ b/examples/suppressionList/removeStatus.js @@ -0,0 +1,14 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +client.suppressionList.removeStatus('test@test.com', function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/suppressionList/search_suppressionList.js b/examples/suppressionList/search_suppressionList.js new file mode 100644 index 0000000..4836870 --- /dev/null +++ b/examples/suppressionList/search_suppressionList.js @@ -0,0 +1,19 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , parameters = { + from: '2015-05-07T00:00:00+0000' + , to: '2015-05-07T23:59:59+0000' + , limit: 5 + }; + +client.suppressionList.search(parameters, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/suppressionList/upsert.js b/examples/suppressionList/upsert.js new file mode 100644 index 0000000..bb7ab8b --- /dev/null +++ b/examples/suppressionList/upsert.js @@ -0,0 +1,20 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , recipient = { + email: 'test@test.com' + , transactional: false + , non_transactional: true + , description: 'Test description' + }; + +client.suppressionList.upsert(recipient, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/suppressionList/upsert_bulk.js b/examples/suppressionList/upsert_bulk.js new file mode 100644 index 0000000..98171a1 --- /dev/null +++ b/examples/suppressionList/upsert_bulk.js @@ -0,0 +1,34 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , recipients = [ + { + email: 'test1@test.com' + , transactional: false + , non_transactional: true + , description: 'Test description 1' + }, + { + email: 'test2@test.com' + , transactional: true + , non_transactional: true + , description: 'Test description 2' + }, + { + email: 'test3@test.com' + , transactional: true + , non_transactional: false + , description: 'Test description 3' + } + ]; + +client.suppressionList.upsert(recipients, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/templates/create_template.js b/examples/templates/create_template.js new file mode 100644 index 0000000..a6f6ca4 --- /dev/null +++ b/examples/templates/create_template.js @@ -0,0 +1,25 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + template: { + id: 'TEST_ID' + , name: 'Test Template' + , content: { + from: 'test@test.com' + , subject: 'Test email template!' + , html: 'This is a test email template!' + } + } + }; + +client.templates.create(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/templates/delete_template.js b/examples/templates/delete_template.js new file mode 100644 index 0000000..a780bba --- /dev/null +++ b/examples/templates/delete_template.js @@ -0,0 +1,14 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +client.templates['delete']('TEST_ID', function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/templates/get_all_templates.js b/examples/templates/get_all_templates.js new file mode 100644 index 0000000..8f3f309 --- /dev/null +++ b/examples/templates/get_all_templates.js @@ -0,0 +1,14 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +client.templates.all(function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/templates/get_draft_template.js b/examples/templates/get_draft_template.js new file mode 100644 index 0000000..87a1c34 --- /dev/null +++ b/examples/templates/get_draft_template.js @@ -0,0 +1,18 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'TEST_ID' + , draft: true + }; + +client.templates.find(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/templates/get_template.js b/examples/templates/get_template.js new file mode 100644 index 0000000..e4a41d4 --- /dev/null +++ b/examples/templates/get_template.js @@ -0,0 +1,17 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'TEST_ID' + }; + +client.templates.find(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/templates/preview_template.js b/examples/templates/preview_template.js new file mode 100644 index 0000000..261c314 --- /dev/null +++ b/examples/templates/preview_template.js @@ -0,0 +1,18 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'TEST_ID' + , data: {} + }; + +client.templates.preview(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/templates/update_published_template.js b/examples/templates/update_published_template.js new file mode 100644 index 0000000..ca6a411 --- /dev/null +++ b/examples/templates/update_published_template.js @@ -0,0 +1,25 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + template: { + id: 'TEST_ID' + , content: { + from: 'test@test.com' + , subject: 'Updated Published Test email template!' + , html: 'This is a published test email template! Updated!' + } + } + , update_published: true + }; + +client.templates.update(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/templates/update_template.js b/examples/templates/update_template.js new file mode 100644 index 0000000..ebf570c --- /dev/null +++ b/examples/templates/update_template.js @@ -0,0 +1,24 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + template: { + id: 'TEST_ID' + , content: { + from: 'test@test.com' + , subject: 'Updated Test email template!' + , html: 'This is a test email template! Updated!' + } + } + }; + +client.templates.update(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/webhooks/create_webhook.js b/examples/webhooks/create_webhook.js new file mode 100644 index 0000000..1d1b19d --- /dev/null +++ b/examples/webhooks/create_webhook.js @@ -0,0 +1,25 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , webhook = { + name: 'Test webhook' + , target: 'http://client.test.com/test-webhook' + , auth_token: 'AUTH_TOKEN' + , events: [ + 'delivery', + 'injection', + 'open', + 'click' + ] + }; + +client.webhooks.create(webhook, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/webhooks/delete_webhook.js b/examples/webhooks/delete_webhook.js new file mode 100644 index 0000000..95f75cf --- /dev/null +++ b/examples/webhooks/delete_webhook.js @@ -0,0 +1,14 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +client.webhooks['delete']('TEST_WEBHOOK_UUID', function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/webhooks/describe_webhook.js b/examples/webhooks/describe_webhook.js new file mode 100644 index 0000000..99e9020 --- /dev/null +++ b/examples/webhooks/describe_webhook.js @@ -0,0 +1,18 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'TEST_WEBHOOK_UUID' + , timezone: 'America/New_York' + }; + +client.webhooks.describe(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/webhooks/getBatchStatus.js b/examples/webhooks/getBatchStatus.js new file mode 100644 index 0000000..d527cc3 --- /dev/null +++ b/examples/webhooks/getBatchStatus.js @@ -0,0 +1,18 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'TEST_WEBHOOK_UUID' + , limit: 1000 + }; + +client.webhooks.getBatchStatus(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/webhooks/get_all_webhooks.js b/examples/webhooks/get_all_webhooks.js new file mode 100644 index 0000000..3f36053 --- /dev/null +++ b/examples/webhooks/get_all_webhooks.js @@ -0,0 +1,14 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key); + +client.webhooks.all(function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/webhooks/update_webhook.js b/examples/webhooks/update_webhook.js new file mode 100644 index 0000000..05284c5 --- /dev/null +++ b/examples/webhooks/update_webhook.js @@ -0,0 +1,22 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , webhook = { + id: 'TEST_WEBHOOK_UUID' + , name: 'Renamed Test webhook' + , events: [ + 'policy_rejection', + 'delay' + ] + }; + +client.webhooks.update(webhook, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/examples/webhooks/validate_webhook.js b/examples/webhooks/validate_webhook.js new file mode 100644 index 0000000..5b885ba --- /dev/null +++ b/examples/webhooks/validate_webhook.js @@ -0,0 +1,20 @@ +'use strict'; + +var key = 'YOURAPIKEY' + , SparkPost = require('sparkpost') + , client = new SparkPost(key) + , options = { + id: 'TEST_WEBHOOK_UUID' + , message: { + msys: {} + } + }; + +client.webhooks.validate(options, function(err, res) { + if (err) { + console.log(err); + } else { + console.log(res.body); + console.log('Congrats you can use our SDK!'); + } +}); diff --git a/lib/recipientLists.js b/lib/recipientLists.js index 81ecfba..b34fc40 100644 --- a/lib/recipientLists.js +++ b/lib/recipientLists.js @@ -10,6 +10,12 @@ var toApiFormat = function(input) { module.exports = function(client) { var recipientLists = { + all: function(callback) { + var reqOpts = { + uri: api + }; + client.get(reqOpts, callback); + }, find: function(options, callback) { options = options || {}; @@ -29,29 +35,25 @@ module.exports = function(client) { client.get(reqOpts, callback); }, - all: function(callback) { - var reqOpts = { - uri: api - }; - client.get(reqOpts, callback); - }, create: function(options, callback) { options = options || {}; - if(!options.list) { - callback(new Error('list is required')); + if(!options.recipients) { + callback(new Error('recipients list is required')); return; } var reqOpts = { uri: api - , json: toApiFormat(options.list) }; if (options.num_rcpt_errors) { reqOpts.qs = reqOpts.qs || {}; reqOpts.qs.num_rcpt_errors = options.num_rcpt_errors; + delete options.num_rcpt_errors; } + reqOpts.json = toApiFormat(options); + client.post(reqOpts, callback); } }; diff --git a/lib/templates.js b/lib/templates.js index 5a4f305..01f9593 100644 --- a/lib/templates.js +++ b/lib/templates.js @@ -10,6 +10,12 @@ var toApiFormat = function(input) { module.exports = function(client) { var templates = { + all: function(callback) { + var options = { + uri: api + }; + client.get(options, callback); + }, find: function(options, callback) { options = options || {}; @@ -29,12 +35,6 @@ module.exports = function(client) { client.get(reqOpts, callback); }, - all: function(callback) { - var options = { - uri: api - }; - client.get(options, callback); - }, create: function(options, callback) { options = options || {}; @@ -61,7 +61,7 @@ module.exports = function(client) { var object = toApiFormat(options.template) , reqOpts = { uri: api + '/' + object.id - , json: toApiFormat(options.template) + , json: object }; if(options.update_published) { diff --git a/lib/webhooks.js b/lib/webhooks.js index 7b0706f..f6f594d 100644 --- a/lib/webhooks.js +++ b/lib/webhooks.js @@ -1,9 +1,12 @@ 'use strict'; -var api = 'webhooks'; +var _ = require('lodash') + , api = 'webhooks'; var toApiFormat = function(input) { - var model = input; + var model = _.clone(input); + + delete model.id; return model; }; @@ -75,7 +78,7 @@ module.exports = function(client) { var object = toApiFormat(webhook) , options = { - uri: api + '/' + object.id + uri: api + '/' + webhook.id , json: object }; @@ -115,9 +118,9 @@ module.exports = function(client) { uri: api + '/' + options.id + '/batch-status' }; - if (options.timezone) { + if (options.limit) { reqOpts.qs = reqOpts.qs || {}; - reqOpts.qs.timezone = options.timezone; + reqOpts.qs.limit = options.limit; } client.get(reqOpts, callback); diff --git a/test/spec/recipientLists.spec.js b/test/spec/recipientLists.spec.js index 7f5d5eb..11fe5a7 100644 --- a/test/spec/recipientLists.spec.js +++ b/test/spec/recipientLists.spec.js @@ -72,7 +72,7 @@ describe('Recipient Lists Library', function() { it('should call client post method with the appropriate uri', function(done) { var options = { - list: test_list + recipients: test_list }; recipientLists.create(options, function(err, data) { @@ -83,7 +83,7 @@ describe('Recipient Lists Library', function() { it('should throw an error if id is missing', function(done) { recipientLists.create(null, function(err) { - expect(err.message).to.equal('list is required'); + expect(err.message).to.equal('recipients list is required'); expect(client.post).not.to.have.been.called; done(); }); @@ -91,7 +91,7 @@ describe('Recipient Lists Library', function() { it('should allow num_rcpt_errors to be set in options', function(done) { var options = { - list: test_list, + recipients: test_list, num_rcpt_errors: 3 }; diff --git a/test/spec/webhooks.spec.js b/test/spec/webhooks.spec.js index 63086b1..a913cd6 100644 --- a/test/spec/webhooks.spec.js +++ b/test/spec/webhooks.spec.js @@ -207,14 +207,14 @@ describe('Webhooks Library', function() { }); }); - it('should allow timezone to be set in options', function(done) { + it('should allow limit to be set in options', function(done) { var options = { id: 'test', - timezone: 'America/New_York' + limit: 1000 }; webhooks.getBatchStatus(options, function(err, data) { - expect(client.get.firstCall.args[0].qs).to.deep.equal({timezone: 'America/New_York'}); + expect(client.get.firstCall.args[0].qs).to.deep.equal({limit: 1000}); done(); }); });