|
1 | 1 | [](https://travis-ci.org/SparkPost/node-sparkpost)
|
2 | 2 |
|
3 |
| -# SparkPost Node.js SDK |
| 3 | + |
| 4 | +# Node.js SDK |
4 | 5 |
|
5 |
| -The official node.js binding for your favorite SparkPost APIs! |
| 6 | +The official Node.js binding for your favorite [SparkPost APIs](https://www.sparkpost.com/api)! |
6 | 7 |
|
7 |
| -Before using this library, you must have a valid API Key. |
| 8 | +## Prerequisites |
8 | 9 |
|
9 |
| -To get an API Key, please log in to your SparkPost account and generate one in the Settings page. |
| 10 | +Before using this library, you must have: |
| 11 | + |
| 12 | +* 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/) |
| 13 | +* 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) |
10 | 14 |
|
11 | 15 | ## Installation
|
12 | 16 |
|
13 | 17 | ```
|
14 | 18 | npm install sparkpost
|
15 | 19 | ```
|
16 | 20 |
|
17 |
| -## Getting Started: Your First Mailing |
18 |
| - |
19 |
| -```javascript |
20 |
| -var SparkPost = require('sparkpost') |
21 |
| - , client = new SparkPost('YOUR API KEY'); |
| 21 | +## Initialization |
| 22 | +**new SparkPost(apiKey, options)** - Initialization |
| 23 | + |
| 24 | +* `apiKey` |
| 25 | + * Required: yes (unless key is stored in `SPARKPOST_API_KEY` environment variable) |
| 26 | + * Type: `String` |
| 27 | + * a passed in apiKey will take precedence over an environment variable |
| 28 | +* `options.origin` or `options.endpoint` |
| 29 | + * Required: no |
| 30 | + * Type: `String` |
| 31 | + * Default: `https://api.sparkpost.com:443` |
| 32 | +* `options.apiVersion` |
| 33 | + * Required: no |
| 34 | + * Type: `String` |
| 35 | + * Default: `v1` |
| 36 | +* `options.headers` |
| 37 | + * Required: no |
| 38 | + * Type: `Object` |
| 39 | + * set headers that apply to all requests |
| 40 | + |
| 41 | +## Methods |
| 42 | +* **request(options, callback)** |
| 43 | + * `options` - [see request modules options](https://github.com/mikeal/request#requestoptions-callback) |
| 44 | + * `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)) |
| 45 | + * `callback` - executed after task is completed. **required** |
| 46 | + * standard `callback(err, data)` |
| 47 | + * `err` - any error that occurred |
| 48 | + * `data.res` - full response from request client |
| 49 | + * `data.body` - payload from response |
| 50 | +* **get | post | put | delete(options, callback)** |
| 51 | + * `options` - see request options |
| 52 | + * `callback` - see request options |
| 53 | + * Request method will be overwritten and set to the same value as the name of these methods. |
| 54 | + |
| 55 | +## Creating a SparkPost Client |
| 56 | + |
| 57 | +Passing in an API key |
| 58 | +```js |
| 59 | +var SparkPost = require('sparkpost'); |
| 60 | +var client = new SparkPost('YOUR_API_KEY'); |
| 61 | +``` |
22 | 62 |
|
23 |
| -var trans = {}; |
| 63 | +Using an API key stored in an environment variable |
| 64 | +```js |
| 65 | +process.env.SPARKPOST_API_KEY = 'YOUR_API_KEY'; |
| 66 | +var SparkPost = require('sparkpost'); |
| 67 | +var client = new SparkPost(); |
| 68 | +``` |
24 | 69 |
|
25 |
| -// Set some metadata for your email |
26 |
| -trans.campaign = 'first-mailing'; |
27 |
| - |
28 |
| -trans.subject = 'First SDK Mailing'; |
| 70 | +Specifying non-default options |
| 71 | +```js |
| 72 | +var SparkPost = require('sparkpost'); |
| 73 | +var options = { |
| 74 | + endpoint: 'https://dev.sparkpost.com:443' |
| 75 | +}; |
| 76 | +var client = new SparkPost('YOUR_API_KEY', options); |
| 77 | +``` |
29 | 78 |
|
30 |
| -// Add some content to your email |
31 |
| -trans.html = '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>'; |
32 |
| -trans.text = 'Congratulations, {{name}}!! You just sent your very first mailing!'; |
33 |
| -trans.substitutionData = {name: 'YOUR FIRST NAME'}; |
| 79 | +## Using the Node SDK Base Object |
| 80 | +We may not wrap every resource available in the SparkPost SDK, for example the Node SDK does not wrap the Metrics resource, |
| 81 | +but you can use the Node SDK Base Object to form requests to these unwrapped resources. Here is an example request using the |
| 82 | +base object to make requests to the Metrics resource. Here is an example request using the base object to make requests to |
| 83 | +the Metrics resource. |
34 | 84 |
|
35 |
| -// Pick someone to receive your email |
36 |
| -trans.recipients = [{ address: { name: 'YOUR FULL NAME', email: 'YOUR EMAIL ADDRESS' } }]; |
| 85 | +```js |
| 86 | +// Get a list of domains that the Metrics API contains data on. |
| 87 | +var options = { |
| 88 | + uri: 'metrics/domains' |
| 89 | +}; |
37 | 90 |
|
38 |
| -// Send it off into the world! |
39 |
| -client.transmission.send(trans, function(err, res) { |
40 |
| - if (err) { |
41 |
| - console.log('Whoops! Something went wrong'); |
| 91 | +client.get(options, function(err, data) { |
| 92 | + if(err) { |
42 | 93 | console.log(err);
|
43 |
| - } else { |
44 |
| - console.log('Woohoo! You just sent your first mailing!'); |
| 94 | + return; |
45 | 95 | }
|
| 96 | + |
| 97 | + console.log(data.body); |
46 | 98 | });
|
47 | 99 | ```
|
48 | 100 |
|
49 |
| -## Learn More |
50 |
| -* For more detailed examples, check our examples: |
51 |
| - * [Transmissions](https://github.com/MessageSystems/node-sdk/blob/master/examples/transmission/) |
52 |
| -* Read our REST API documentation - <http://www.sparkpost.com/docs> |
53 |
| - |
54 |
| -## Field Descriptions |
55 |
| -### Transmissions |
56 |
| -| Field Name | Required? | Description | Data Type | |
57 |
| -| ------------ | ----------- | ------------- | ----------- | |
58 |
| -| description | no | Field for describing what this transmission is for the user | String | |
59 |
| -| campaign | no | Field for assigning a given transmission to a specific campaign, which is a logical container for similar transmissions | String | |
60 |
| -| metadata | no | Field for adding arbitrary key/value pairs which will be included in open/click tracking | Object (Simple) | |
61 |
| -| substitutionData | no | Field for adding transmission level substitution data, which can be used in a variety of fields and in content | Object (Complex) | |
62 |
| -| trackOpens | no | Field for enabling/disabling transmission level open tracking, if not set will use settings from Template | Boolean | |
63 |
| -| trackClicks | no | Field for enabling/disabling transmission level click tracking, if not set will use settings from Template | Boolean | |
64 |
| -| useSandbox | no | Field for enabling/disabling using sandbox domain to send transmission(You are limited to 50 messages ever with sandbox) | Boolean | |
65 |
| -| useDraftTemplate | no | Field for allowing the sending of a transmission using a draft of a stored template (default: false) | Boolean | |
66 |
| -| replyTo | no | Field for specifying the email address that should be used when a recipient hits the reply button | String | |
67 |
| -| subject | yes | Field for setting the subject line of a given transmission | String | |
68 |
| -| from | yes | Field for setting the from line of a given transmission | String or Object | |
69 |
| -| html | yes** | Field for setting the HTML content of a given transmission | String | |
70 |
| -| text | yes** | Field for setting the Plain Text content of a given transmission | String | |
71 |
| -| rfc822 | no** | Field for setting the RFC-822 encoded content of a given transmission | String | |
72 |
| -| template | no** | Field for specifying the Template ID of a stored template to be used when sending a given transmission | String | |
73 |
| -| customHeaders | no | Field for specifying additional headers to be applied to a given transmission (other than Subject, From, To, and Reply-To) | Object (Simple) | |
74 |
| -| recipients | yes** | Field for specifying who a given transmission should be sent to | Array of Objects | |
75 |
| -| recipientList | no** | Field for specifying a stored recipient list ID to be used for a given transmission | String | |
76 |
| - |
77 |
| -** - 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. |
78 |
| - |
79 |
| -### Sending Domains |
80 |
| -| Field Name | Required? | Description | Data Type | |
81 |
| -| ------------ | ----------- | ------------- | ----------- | |
82 |
| -| domainName | yes | Name of the sending domain | String | |
83 |
| -| privateKey | yes** | Private key used to create the DKIM Signature. | String | |
84 |
| -| publicKey | yes** | Public key to be retrieved from the DNS of the sending domain. | String | |
85 |
| -| selector | yes** | DomainKey selector that indicates the DKIM public key location. | String | |
86 |
| -| headers | no | Header fields to be included in the DKIM signature | String | |
87 |
| - |
88 |
| -** - If specifying a privateKey, publicKey, or selector, all three fields are required. |
89 |
| - |
90 |
| -## Tips and Tricks |
91 |
| -### General |
92 |
| -* You _must_ provide at least an API key when instantiating the SparkPost Library - `{ key: '184ac5480cfdd2bb2859e4476d2e5b1d2bad079bf' }` |
93 |
| -* The SDK's features are namespaced under the various SparkPost API names. |
94 |
| - |
95 |
| -### Transmissions |
96 |
| -* If you specify a stored recipient list and inline recipients in a Transmission, you will receive an error. |
97 |
| -* If you specify HTML and/or Plain Text content and then provide RFC-822 encoded content, you will receive an error. |
98 |
| - * RFC-822 content is not valid with any other content type. |
99 |
| -* If you specify a stored template and also provide inline content, you will receive an error. |
100 |
| -* By default, open and click tracking are enabled for a transmission. |
101 |
| -* By default, a transmission will use the published version of a stored template. |
| 101 | +## SparkPost API Resources Supported in Node SDK |
| 102 | +Click on the desired API to see usage and more information |
| 103 | + |
| 104 | +* [Recipient Lists](/docs/resources/recipientLists.md) - `client.recipientLists` ([examples](/examples/recipientLists)) |
| 105 | +* [Sending Domains](/docs/resources/sendingDomains.md) - `client.sendingDomains` ([examples](/examples/sendingDomains)) |
| 106 | +* [Suppression List](/docs/resources/suppressionList.md) - `client.suppressionList` ([examples](/examples/suppressionList)) |
| 107 | +* [Templates](/docs/resources/templates.md) - `client.templates` ([examples](/examples/templates)) |
| 108 | +* [Transmissions](/docs/apis/transmission.md) - `client.transmission` ([examples](/examples/transmission)) |
| 109 | +* [Webhooks](/docs/resources/webhooks.md) - `client.webhooks` ([examples](/examples/webhooks)) |
| 110 | + |
102 | 111 |
|
103 | 112 | ## Development
|
104 | 113 |
|
|
0 commit comments