├── .editorconfig ├── .eslintrc ├── .gitignore ├── .jshintrc ├── .travis.yml ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.txt ├── README.md ├── docs ├── ADDING_ISSUES.md ├── CODE_STYLE_GUIDE.md ├── async.md ├── resources │ ├── events.md │ ├── inboundDomains.md │ ├── messageEvents.md │ ├── recipientLists.md │ ├── relayWebhooks.md │ ├── sendingDomains.md │ ├── subaccounts.md │ ├── suppressionList.md │ ├── templates.md │ ├── transmissions.md │ └── webhooks.md └── sparkpost_logo.png ├── examples ├── baseObject │ └── getDomainsList.js ├── events │ └── search_message.js ├── inboundDomains │ ├── create.js │ ├── delete.js │ ├── get.js │ └── list.js ├── messageEvents │ ├── search_campaignClicks.js │ ├── search_default.js │ └── search_messageEvents.js ├── recipientLists │ ├── create.js │ ├── delete.js │ ├── get.js │ ├── list.js │ └── update.js ├── relayWebhooks │ ├── create.js │ ├── delete.js │ ├── get.js │ ├── list.js │ └── update.js ├── sendingDomains │ ├── create.js │ ├── delete.js │ ├── get.js │ ├── list.js │ ├── update.js │ └── verify.js ├── subaccounts │ ├── create.js │ ├── get.js │ ├── list.js │ └── update.js ├── suppressionList │ ├── delete.js │ ├── get.js │ ├── list.js │ ├── upsert.js │ └── upsert_bulk.js ├── templates │ ├── create.js │ ├── delete.js │ ├── get.js │ ├── get_draft.js │ ├── list.js │ ├── preview.js │ ├── update.js │ └── update_published.js ├── transmissions │ ├── get.js │ ├── list.js │ ├── list_by_campaign.js │ ├── list_by_template.js │ ├── send_all_fields.js │ ├── send_inline_image.js │ ├── send_mime_parts.js │ ├── send_rfc822.js │ ├── send_stored_recipients_inline_content.js │ ├── send_stored_recipients_stored_content.js │ ├── send_stored_template.js │ ├── send_with_bcc.js │ ├── send_with_bcc_sugar.js │ ├── send_with_cc.js │ └── send_with_cc_sugar.js └── webhooks │ ├── create.js │ ├── delete.js │ ├── get.js │ ├── getBatchStatus.js │ ├── getDocumentation.js │ ├── getSamples.js │ ├── list.js │ ├── update.js │ └── validate.js ├── lib ├── events.js ├── inboundDomains.js ├── messageEvents.js ├── recipientLists.js ├── relayWebhooks.js ├── sendingDomains.js ├── sparkpost.js ├── subaccounts.js ├── suppressionList.js ├── templates.js ├── transmissions.js ├── webhooks.js └── withCallback.js ├── package-lock.json ├── package.json └── test ├── mocha.opts └── spec ├── events.spec.js ├── inboundDomains.spec.js ├── messageEvents.spec.js ├── recipientLists.spec.js ├── relayWebhooks.spec.js ├── sendingDomains.spec.js ├── sparkpost.spec.js ├── subaccounts.spec.js ├── suppressionList.spec.js ├── templates.spec.js ├── transmissions.spec.js └── webhooks.spec.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = "utf-8" 8 | 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "sparkpost/api", 3 | "globals": { 4 | "Promise": true 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | xunit.xml 3 | test/reports/* 4 | 5 | # vscode 6 | .vscode -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node" : true, 3 | "es3" : false, 4 | "esnext" : true, 5 | "strict" : true, 6 | "curly" : true, 7 | "eqeqeq" : true, 8 | "immed" : true, 9 | "indent" : 2, 10 | "newcap" : true, 11 | "noarg" : true, 12 | "quotmark" : true, 13 | "undef" : true, 14 | "unused" : true, 15 | "asi" : false, 16 | "boss" : false, 17 | "debug" : false, 18 | "laxcomma" : true, 19 | "maxcomplexity" : 5 20 | } 21 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '4' 4 | - '5' 5 | - '6' 6 | after_success: 7 | - npm run coveralls 8 | notifications: 9 | slack: 10 | secure: dsz+D/TuylEi+6zqdB5dVqyMlpbpafaBBcAwYIijTK6LuG8KdIdGNSFVX1ro6o3bJFwMvtfxNeK1eFrMy8l6VHZQL0dkXWRmCl/pxLhEntUiYTDwDOtiqy1QLZtv5AqtsdSr1qLiOJtgF6gXk66xipnV2UzjLVVoxzSrdOSnX4U= 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | This project adheres to [Semantic Versioning](http://semver.org/). 4 | 5 | ## [Unreleased][unreleased] 6 | - Security patches to dev-dependencies [#237](https://github.com/SparkPost/node-sparkpost/pull/237) by @jgzamora 7 | 8 | ## [2.1.4] - 2019-10-01 9 | ### Added 10 | - Events API functionality by @sstaley-sparkpost 11 | - Example for [searching the events/message API](/examples/events/search_message.js) by @sstaley-sparkpost. 12 | 13 | ## [2.1.3] - 2018-10-24 14 | ### Fixed 15 | - Template preview draft option [bug](https://github.com/SparkPost/node-sparkpost/issues/233) by @jgzamora 16 | - Updated example for [retrieving a recipient list](/examples/recipientLists/get.js) by @WanderingBrooks. 17 | 18 | ### Added 19 | - Example for [sending a transmission with an inline image](/examples/transmissions/send_inline_image.js) by @aydrian. 20 | 21 | ## [2.1.2] - 2017-01-20 22 | ### Fixed 23 | - Callbacks are no longer being called twice in some methods by @avrahamgoldman. See Issue [203](https://github.com/SparkPost/node-sparkpost/issues/203). 24 | 25 | ## [2.1.1] - 2017-01-11 26 | ### Changed 27 | - Removed our addition to the native Promise prototype in favor of a bluebird-inspired callback-wrapping function. See Issue [#199](https://github.com/SparkPost/node-sparkpost/issues/199). Thanks @danieljuhl. 28 | 29 | ### Fixed 30 | - Empty CC sugar method no longer triggers an API error by @avrahamgoldman. 31 | 32 | ## [2.1.0] - 2016-12-20 33 | ### Added 34 | - You can now add recipients to CC and BCC using sugar methods by @avrahamgoldman. See the updated [transmissions documentation](/docs/resources/transmissions.md) and [CC](/examples/transmissions/send_with_cc_sugar.js)/[BCC](/examples/transmissions/send_with_bcc_sugar.js) examples. 35 | 36 | ### Changed 37 | - Updated the following npm packages: coveralls, eslint, eslint-config-sparkpost, mocha, lodash, and request by @aydrian. 38 | - Resolved new linting issues from eslint-config-sparkpost update by @avrahamgoldman. 39 | 40 | ### Fixed 41 | - The options parameter on the `transmissions.send()` method is now optional if you're using a callback function by @avrahamgoldman. 42 | - The options parameter on the `templates.get()` method is now optional if you're using a callback function by @avrahamgoldman. 43 | 44 | ## [2.0.1] - 2016-11-10 45 | ### Added 46 | - Node.js version is now tracked using the User-Agent header by @ewandennis. 47 | - An optional "stack identifier" that can be set during [initialization](README.md#initialization) so we can track libraries that use node-sparkpost via the User-Agent header by @ewandennis. 48 | 49 | ## [2.0.0] - 2016-11-04 - *breaking* 50 | With this major release, we streamlined and simplified the library making it more of a thin wrapper, adding sugar methods when needed. Parameters are no longer abstracted and are passed directly to the API as laid out in the [official documentation](https://developers.sparkpost.com/api/). Please see the updated [resource docs](/docs/resources) and [examples](/examples). The high level changes have been listed below. 51 | 52 | ### Added 53 | - Support for Promises and Callbacks. See [Async Handling](/docs/async.md). 54 | - Debug option on initialization attaches debug information on response 55 | 56 | ### Changed 57 | - Methods return the response body instead of the full response. 58 | - Standardized methods on all API wrappers. See Issue [#175](https://github.com/SparkPost/node-sparkpost/issues/175). 59 | - Transmissions `send` method now takes an object of [transmission attributes](https://developers.sparkpost.com/api/transmissions.html#header-transmission-attributes) as the first parameter. Any other options, such as `num_rcpt_errors` has been moved to a second optional `options` parameter. 60 | - Removed the `toApiFormat` method, parameters are passed directly to the API as snake_case. 61 | - Now using ESLint with SparkPost config instead of JSLint 62 | - Now using NPM scripts instead of grunt 63 | 64 | ### Fixed 65 | - Responses for `GET` requests are now properly parsed as JSON by @aydrian. Closes [#111](https://github.com/SparkPost/node-sparkpost/issues/111) 66 | 67 | ### Removed 68 | - No longer supporting Node.js versions 0.10 & 0.12. We will be following the [LTS Schedule](https://github.com/nodejs/LTS) going forward. 69 | - Removed SendGrid Compatibility layer. 70 | 71 | ## [1.3.8] - 2016-08-26 72 | - [#165](https://github.com/SparkPost/node-sparkpost/pull/165) Updated webhook update method to not send id in request (@aydrian) 73 | 74 | ## [1.3.7] - 2016-07-28 75 | - [#150](https://github.com/SparkPost/node-sparkpost/pull/150) Upgrade lodash version to 4 (@rnzo) 76 | 77 | ## [1.3.6] - 2016-07-14 78 | - [#148](https://github.com/SparkPost/node-sparkpost/pull/148) Preserve array/object structure of API data (@gpittarelli) 79 | 80 | ## [1.3.5] - 2016-05-13 81 | - [#146](https://github.com/SparkPost/node-sparkpost/pull/146) Single recipient suppression list upserts now use the bulk update endpoint (@jgzamora) 82 | 83 | ## [1.3.4] - 2016-05-10 84 | - [#144](https://github.com/SparkPost/node-sparkpost/pull/144) Updated bulk suppression list upsert payload (@aydrian) 85 | 86 | ## [1.3.3] - 2016-04-25 87 | - [#142](https://github.com/SparkPost/node-sparkpost/pull/142) Upgrade request to 2.72.0 and fix affected test (@artlogic) 88 | 89 | ## [1.3.2] - 2016-04-13 90 | - [#139](https://github.com/SparkPost/node-sparkpost/pull/139) Make Gruntfile.js cross-platform friendly (@coldacid) 91 | - [#137](https://github.com/SparkPost/node-sparkpost/pull/137) Fix missing `subaccounts` property in SparkPost class (@coldacid) 92 | - [#134](https://github.com/SparkPost/node-sparkpost/pull/134) Let inboundDomains.create() use a domain name as per the docs (@orval) 93 | - [#132](https://github.com/SparkPost/node-sparkpost/pull/132) Some docs erroneously refer to recipient lists (@orval) 94 | 95 | ## [1.3.1] - 2016-04-01 96 | - [#130](https://github.com/SparkPost/node-sparkpost/pull/130) Refactored toApiFormat.js to use json-pointer (@orval) 97 | 98 | ## [1.3.0] - 2016-04-01 99 | - [#129](https://github.com/SparkPost/node-sparkpost/pull/129) Added support for subaccounts (@coldacid) 100 | - [#126](https://github.com/SparkPost/node-sparkpost/pull/126) body might be undefined (@mstdokumaci) 101 | - [#121](https://github.com/SparkPost/node-sparkpost/pull/121) Added support for Relay Webhooks (@aydrian) 102 | - [#119](https://github.com/SparkPost/node-sparkpost/pull/119) Added support for inbound domains (@aydrian) 103 | - [#118](https://github.com/SparkPost/node-sparkpost/pull/118) Added support for deleting a sending domain (@aydrian) 104 | - [#115](https://github.com/SparkPost/node-sparkpost/pull/115) ReadMe "Hello World" Example (@JimTheMan) 105 | 106 | ## [1.2.0] - 2016-03-14 107 | - [#109](https://github.com/SparkPost/node-sparkpost/pull/109) README updates (@aydrian) 108 | - [#108](https://github.com/SparkPost/node-sparkpost/pull/108) removes from and subject from transmission stored template send example (@colestrode) 109 | - [#106](https://github.com/SparkPost/node-sparkpost/pull/106) correcting send_transmission_all_fields.js example (@lfreneda) 110 | - [#104](https://github.com/SparkPost/node-sparkpost/pull/104) Added a mocha test task to the default grunt task (@ewandennis) 111 | - [#101](https://github.com/SparkPost/node-sparkpost/pull/101) Added gzipped response support (@ewandennis) 112 | - [#100](https://github.com/SparkPost/node-sparkpost/pull/100) Added message events wrapper (@ewandennis) 113 | - [#97](https://github.com/SparkPost/node-sparkpost/pull/97) Add nodejs versions to TravisCI (@richleland) 114 | 115 | ## [1.1.0] - 2015-08-13 116 | - [#92](https://github.com/SparkPost/node-sparkpost/pull/92) Added Coveralls.io (@aydrian) 117 | - [#91](https://github.com/SparkPost/node-sparkpost/pull/91) Added Recipient List Update method, Docs, and Examples (@aydrian) 118 | - [#85](https://github.com/SparkPost/node-sparkpost/pull/85) Added getDocumentation and getSamples functions to Webhooks resource (@aydrian) 119 | 120 | ## [1.0.1] - 2015-08-06 121 | - [#88](https://github.com/SparkPost/node-sparkpost/pull/88) Modified toApiFormat spec test file to fit standard naming convention. Added tests for code coverage. (@aydrian) 122 | - [#87](https://github.com/SparkPost/node-sparkpost/pull/87) Removed dependency for snake-case (@aydrian) 123 | - [#83](https://github.com/SparkPost/node-sparkpost/pull/83) Make sure to support sending with a stored recipient list or template, while also leaving headers and substitution data alone. (@bdeanindy) 124 | - [#80](https://github.com/SparkPost/node-sparkpost/pull/80) Updated transmission docs and examples. (@aydrian) 125 | - [#78](https://github.com/SparkPost/node-sparkpost/pull/78) Update Sending Domains docs and examples. (@aydrian) 126 | - [#70](https://github.com/SparkPost/node-sparkpost/pull/70) Update examples for newly added toApiFormat (@bdeanindy) 127 | 128 | ## [1.0.0] - 2015-06-06 - *breaking* 129 | - [#68](https://github.com/SparkPost/node-sparkpost/pull/68) Added keywords for searching in npm (@nornholdj) 130 | - [#67](https://github.com/SparkPost/node-sparkpost/pull/67) Created a change log as CHANGELOG.md (@aydrian) 131 | - [#66](https://github.com/SparkPost/node-sparkpost/pull/66) Issue [#51](https://github.com/SparkPost/node-sparkpost/issues/51) Modify the base object to handle non 2XX status and simplify second callback param (@aydrian) 132 | - [#56](https://github.com/SparkPost/node-sparkpost/pull/56) Issue [#46](https://github.com/SparkPost/node-sparkpost/issues/46) Updates to Transmissions library (@aydrian) 133 | - [#55](https://github.com/SparkPost/node-sparkpost/pull/55) Fix doc about using process.env.SPARKPOST_API_KEY (@bizob2828) 134 | - [#54](https://github.com/SparkPost/node-sparkpost/pull/54) fixed link to transmissions in readme (@bizob2828) 135 | - [#45](https://github.com/SparkPost/node-sparkpost/pull/45) Issue [#45](https://github.com/SparkPost/node-sparkpost/issues/44) Accept camelCase or native API format seamlessly (@bdeanindy) 136 | - Issue [#64](https://github.com/SparkPost/node-sparkpost/issues/64) Update webhooks to use toApiFormat 137 | - Issue [#63](https://github.com/SparkPost/node-sparkpost/issues/63) Updated suppressionlists to use toApiFormat 138 | - Issue [#61](https://github.com/SparkPost/node-sparkpost/issues/61) Update sending domains to use toApiFormat 139 | - Issue [#60](https://github.com/SparkPost/node-sparkpost/issues/60) Update templates to use toApiFormat 140 | - Issue [#58](https://github.com/SparkPost/node-sparkpost/issues/58) Update recipientLists to use toApiFormat 141 | - Issue [#57](https://github.com/SparkPost/node-sparkpost/issues/58) Update transmissions to use toApiFormat 142 | 143 | ## [0.9.0] - 2015-05-12 - *breaking* 144 | - [#49](https://github.com/SparkPost/node-sparkpost/pull/49) Issue [#48](https://github.com/SparkPost/node-sparkpost/issues/48) Add Grunt Bump (@aydrian) 145 | - [#47](https://github.com/SparkPost/node-sparkpost/pull/47) Issue [#44](https://github.com/SparkPost/node-sparkpost/issues/44) Create docs and examples for new APIs (@aydrian) 146 | - [#43](https://github.com/SparkPost/node-sparkpost/pull/43) Issue [#42](https://github.com/SparkPost/node-sparkpost/issues/42) Modified sendingDomains.verify and updated the unit tests and examples.(@aydrian) 147 | - [#41](https://github.com/SparkPost/node-sparkpost/pull/41) Issue [#40](https://github.com/SparkPost/node-sparkpost/issues/40) sparkpost.js test cleanup (@aydrian) 148 | - [#39](https://github.com/SparkPost/node-sparkpost/pull/39) Issue [#38](https://github.com/SparkPost/node-sparkpost/issues/38) Updated Sending Domains unit tests. Added error checking. (@aydrian) 149 | - [#34](https://github.com/SparkPost/node-sparkpost/pull/34) Issue [#33](https://github.com/SparkPost/node-sparkpost/issues/33) made transmission unit tests more unity (@colestrode) 150 | - [#32](https://github.com/SparkPost/node-sparkpost/pull/32) Issue [#10](https://github.com/SparkPost/node-sparkpost/issues/10) Webhooks API (@aydrian) 151 | - [#31](https://github.com/SparkPost/node-sparkpost/pull/31) Issue [#6](https://github.com/SparkPost/node-sparkpost/issues/6) Templates SDK (@aydrian) 152 | - [#30](https://github.com/SparkPost/node-sparkpost/pull/30) Issue [#20](https://github.com/SparkPost/node-sparkpost/issues/20) Suppression List SDK (@aydrian) 153 | - [#29](https://github.com/SparkPost/node-sparkpost/pull/29) Issue [#7](https://github.com/SparkPost/node-sparkpost/issues/7) Recipient Lists SDK (@aydrian) 154 | - [#27](https://github.com/SparkPost/node-sparkpost/pull/27) Issue [#16](https://github.com/SparkPost/node-sparkpost/issues/16) Adding a base object (@aydrian) 155 | 156 | ## [0.1.6] - 2015-04-16 157 | - [#26](https://github.com/SparkPost/node-sparkpost/pull/26) removed defaulting open/click tracking to true (@jmartin4563) 158 | 159 | ## [0.1.5] - 2015-04-02 160 | - [#22](https://github.com/SparkPost/node-sparkpost/pull/22) Apply fix for [#21](https://github.com/SparkPost/node-sparkpost/issues/21) and add appropriate unit test / update mock (@jmartin4563) 161 | 162 | ## [0.1.4] - 2015-02-24 163 | - Sending Domains Functionality and Transmissions Sandbox Option 164 | 165 | ## [0.1.3] - 2015-01-08 166 | - Open & Click Tracking Disabling Bug Fix 167 | 168 | ## [0.1.2] - 2014-12-10 169 | - Added SendGrid compatibility layer 170 | 171 | ## [0.1.1] - 2014-12-10 172 | - Added SendGrid compatibility layer 173 | 174 | ## 0.1.0 - 2014-11-10 175 | - First Release! 176 | 177 | [unreleased]: https://github.com/sparkpost/node-sparkpost/compare/v2.1.2...HEAD 178 | [2.1.2]: https://github.com/sparkpost/node-sparkpost/compare/v2.1.1...v2.1.2 179 | [2.1.1]: https://github.com/sparkpost/node-sparkpost/compare/v2.1.0...v2.1.1 180 | [2.1.0]: https://github.com/sparkpost/node-sparkpost/compare/v2.0.1...v2.1.0 181 | [2.0.1]: https://github.com/sparkpost/node-sparkpost/compare/v2.0.0...v2.0.1 182 | [2.0.0]: https://github.com/sparkpost/node-sparkpost/compare/1.3.8...v2.0.0 183 | [1.3.8]: https://github.com/sparkpost/node-sparkpost/compare/1.3.7...1.3.8 184 | [1.3.7]: https://github.com/sparkpost/node-sparkpost/compare/1.3.6...1.3.7 185 | [1.3.6]: https://github.com/sparkpost/node-sparkpost/compare/1.3.5...1.3.6 186 | [1.3.5]: https://github.com/sparkpost/node-sparkpost/compare/1.3.4...1.3.5 187 | [1.3.4]: https://github.com/sparkpost/node-sparkpost/compare/1.3.3...1.3.4 188 | [1.3.3]: https://github.com/sparkpost/node-sparkpost/compare/1.3.2...1.3.3 189 | [1.3.2]: https://github.com/sparkpost/node-sparkpost/compare/1.3.1...1.3.2 190 | [1.3.1]: https://github.com/sparkpost/node-sparkpost/compare/1.3.0...1.3.1 191 | [1.3.0]: https://github.com/sparkpost/node-sparkpost/compare/1.2.0...1.3.0 192 | [1.2.0]: https://github.com/sparkpost/node-sparkpost/compare/1.1.0...1.2.0 193 | [1.1.0]: https://github.com/sparkpost/node-sparkpost/compare/1.0.1...1.1.0 194 | [1.0.1]: https://github.com/sparkpost/node-sparkpost/compare/1.0.0...1.0.1 195 | [1.0.0]: https://github.com/sparkpost/node-sparkpost/compare/0.9.0...1.0.0 196 | [0.9.0]: https://github.com/sparkpost/node-sparkpost/compare/0.1.6...0.9.0 197 | [0.1.6]: https://github.com/sparkpost/node-sparkpost/compare/0.1.5...0.1.6 198 | [0.1.5]: https://github.com/sparkpost/node-sparkpost/compare/0.1.4...0.1.5 199 | [0.1.4]: https://github.com/sparkpost/node-sparkpost/compare/0.1.3...0.1.4 200 | [0.1.3]: https://github.com/sparkpost/node-sparkpost/compare/0.1.2...0.1.3 201 | [0.1.2]: https://github.com/sparkpost/node-sparkpost/compare/0.1.1...0.1.2 202 | [0.1.1]: https://github.com/sparkpost/node-sparkpost/compare/0.1.0...0.1.1 203 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at developers@sparkpost.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to SparkPost 2 | 3 | Transparency is one of our core values, and we encourage developers to contribute and become part of the SparkPost developer community. 4 | ## Prerequisite to contribution 5 | 6 | Before writing code, please search for existing issues or [create a new issue](docs/ADDING_ISSUES.markdown) to confirm where your contribution fits into the roadmap. 7 | 8 | Current milestone Pull Requests will receive priority review for merging. 9 | 10 | ## Contribution Steps 11 | 1. Fork this repository 12 | 2. Create a new branch named after the issue you’ll be fixing (include the issue number as the branch name, example: Issue in GH is #8 then the branch name should be ISSUE-8)) 13 | 3. Write corresponding tests and code (only what is needed to satisfy the issue and tests please) 14 | * Include your tests in the 'test' directory in an appropriate test file 15 | * Write code to satisfy the tests 16 | * Run tests using ```npm test``` 17 | 5. Ensure automated tests pass 18 | 6. Submit a new Pull Request applying your feature/fix branch to the develop branch of the SparkPost client library 19 | 20 | ## Releases 21 | If you are a collaborator, when you want release a new version, follow these steps. 22 | 23 | 1. Make sure all the changes are merged into master 24 | 2. Make sure all changes have passed [Travis CI build][1] 25 | 3. Determine type of release. We use [Semantic Versioning](http://semver.org/). 26 | 4. Update [CHANGELOG.md](CHANGELOG.md) with release notes and commit 27 | 5. Run `npm version` command to increment `package.json` version, commit changes, tag changes, and push to upstream. 28 | - Patch -> `npm version patch` 29 | - Minor -> `npm version minor` 30 | - Major -> `npm version major` 31 | 6. Once [Travis CI build][1] (from tag) has completed, make sure you're working directory is clean and run `npm publish` 32 | while in the project root. 33 | 7. Create a new [Github Release](https://github.com/SparkPost/node-sparkpost/releases) using the new tag. Copy release 34 | notes from the [CHANGELOG.md](CHANGELOG.md). 35 | 36 | [1]: https://travis-ci.org/SparkPost/node-sparkpost 37 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2014 Message Systems, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"). 4 | You may not use this software except in compliance with the License. 5 | 6 | A copy of the License is located at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0.html 9 | 10 | or in the "license" file accompanying this software. This file is 11 | distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF 12 | ANY KIND, either express or implied. See the License for the specific 13 | language governing permissions and limitations under the License. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | [Sign up][sparkpost sign up] for a SparkPost account and visit our [Developer Hub](https://developers.sparkpost.com) for even more content. 4 | 5 | # Node.js Client Library 6 | 7 | [![Travis CI](https://travis-ci.org/SparkPost/node-sparkpost.svg?branch=master)](https://travis-ci.org/SparkPost/node-sparkpost) [![Coverage Status](https://coveralls.io/repos/SparkPost/node-sparkpost/badge.svg?branch=master&service=github)](https://coveralls.io/github/SparkPost/node-sparkpost?branch=master) [![NPM version](https://badge.fury.io/js/sparkpost.png)](http://badge.fury.io/js/sparkpost) 8 | 9 | The official Node.js binding for your favorite [SparkPost APIs](https://developers.sparkpost.com/api)! 10 | 11 | ## Prerequisites 12 | 13 | Before using this library, you must have: 14 | 15 | * A shiny new SparkPost Account, [sign up for a new account][sparkpost sign up] or [login to SparkPost](https://app.sparkpost.com/) 16 | * 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) 17 | 18 | ## Installation 19 | 20 | ``` 21 | npm install sparkpost 22 | ``` 23 | 24 | *Note: Node.js versions 0.10 and 0.12 are no longer supported.* 25 | 26 | ## Initialization 27 | **new SparkPost(apiKey[, options])** - Initialization 28 | 29 | * `apiKey` 30 | * Required: yes (unless key is stored in `SPARKPOST_API_KEY` environment variable) 31 | * Type: `String` 32 | * a passed in apiKey will take precedence over an environment variable 33 | * `options.origin` or `options.endpoint` 34 | * Required: no 35 | * Type: `String` 36 | * Default: `https://api.sparkpost.com:443`
37 | *Note: To use the SparkPost EU API you will need to set this to `https://api.eu.sparkpost.com:443`.* 38 | * `options.apiVersion` 39 | * Required: no 40 | * Type: `String` 41 | * Default: `v1` 42 | * `options.stackIdentity` 43 | * Required: no 44 | * Type: `String` 45 | * An optional identifier to include in the User-Agent header. e.g. `product/1.0.0` 46 | * `options.headers` 47 | * Required: no 48 | * Type: `Object` 49 | * set headers that apply to all requests 50 | * `options.debug` 51 | * Required: no 52 | * Type: `Boolean` 53 | * Default: `false` 54 | * appends full response from request client as `debug` when `true` for debugging purposes
55 | *Note: This will expose your api key to the client-side. Do not use in production.* 56 | 57 | ## Methods 58 | 59 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 60 | 61 | * **request(options[, callback])** 62 | * `options` - [see request modules options](https://github.com/mikeal/request#requestoptions-callback) 63 | * `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)) 64 | * `options.debug` - setting to `true` includes full response from request client for debugging purposes 65 | * **get | post | put | delete(options[, callback])** 66 | * `options` - see request options 67 | * Request method will be overwritten and set to the same value as the name of these methods. 68 | 69 | ## Creating a SparkPost Client 70 | 71 | Passing in an API key 72 | ```js 73 | const SparkPost = require('sparkpost'); 74 | const client = new SparkPost('YOUR_API_KEY'); 75 | ``` 76 | 77 | Using an API key stored in an environment variable 78 | ```js 79 | //Create an env var as SPARKPOST_API_KEY 80 | const SparkPost = require('sparkpost'); 81 | const client = new SparkPost(); 82 | ``` 83 | 84 | Specifying non-default options 85 | ```js 86 | const SparkPost = require('sparkpost'); 87 | const options = { 88 | endpoint: 'https://dev.sparkpost.com:443' 89 | }; 90 | const client = new SparkPost('YOUR_API_KEY', options); 91 | ``` 92 | 93 | ## Using the Node Client Library Base Object 94 | We may not wrap every resource available in the SparkPost Client Library, for example the Node Client Library does not wrap the Metrics resource, 95 | but you can use the Node Client Library Base Object to form requests to these unwrapped resources. Here is an example request using the 96 | base object to make requests to the Metrics resource. Here is an example request using the base object to make requests to 97 | the Metrics resource. 98 | 99 | ```js 100 | // Get a list of domains that the Metrics API contains data on. 101 | const options = { 102 | uri: 'metrics/domains' 103 | }; 104 | 105 | client.get(options) 106 | .then(data => { 107 | console.log(data); 108 | }) 109 | .catch(err => { 110 | console.log(err); 111 | }); 112 | ``` 113 | 114 | ## Send An Email "Hello World" Example 115 | Below is an example of how to send a simple email. Sending an email is known as a *transmission*. By using the send 116 | method on the transmissions service that's available from the SparkPost object you instantiate, you can pass in an 117 | object with all the [transmission attributes](https://developers.sparkpost.com/api/transmissions#header-transmission-attributes) 118 | relevant to the email being sent. The send method will return a promise that will let you know if the email was sent 119 | successful and if not information about the error that occurred. If a callback is passed, it will be executed. 120 | 121 | ```javascript 122 | const SparkPost = require('sparkpost'); 123 | const client = new SparkPost(''); 124 | 125 | // If you have a SparkPost EU account you will need to pass a different `origin` via the options parameter: 126 | // const euClient = new SparkPost('', { origin: 'https://api.eu.sparkpost.com:443' }); 127 | 128 | client.transmissions.send({ 129 | options: { 130 | sandbox: true 131 | }, 132 | content: { 133 | from: 'testing@sparkpostbox.com', 134 | subject: 'Hello, World!', 135 | html:'

Testing SparkPost - the world\'s most awesomest email service!

' 136 | }, 137 | recipients: [ 138 | {address: ''} 139 | ] 140 | }) 141 | .then(data => { 142 | console.log('Woohoo! You just sent your first mailing!'); 143 | console.log(data); 144 | }) 145 | .catch(err => { 146 | console.log('Whoops! Something went wrong'); 147 | console.log(err); 148 | }); 149 | ``` 150 | 151 | ## SparkPost API Resources Supported in Node Client Library 152 | Click on the desired API to see usage and more information 153 | 154 | * [Inbound Domains](/docs/resources/inboundDomains.md) - `client.inboundDomains` ([examples](/examples/inboundDomains)) 155 | * [Message Events](/docs/resources/messageEvents.md) - `client.messageEvents` ([examples](/examples/messageEvents)) 156 | * [Events](/docs/resources/events.md) - `client.events` ([examples](/examples/events)) 157 | * [Recipient Lists](/docs/resources/recipientLists.md) - `client.recipientLists` ([examples](/examples/recipientLists)) 158 | * [Relay Webhooks](/docs/resources/relayWebhooks.md) - `client.relayWebhooks` ([examples](/examples/relayWebhooks)) 159 | * [Sending Domains](/docs/resources/sendingDomains.md) - `client.sendingDomains` ([examples](/examples/sendingDomains)) 160 | * [Subaccounts](/docs/resources/subaccounts.md) - `client.subaccounts` ([examples](/examples/subaccounts)) 161 | * [Suppression List](/docs/resources/suppressionList.md) - `client.suppressionList` ([examples](/examples/suppressionList)) 162 | * [Templates](/docs/resources/templates.md) - `client.templates` ([examples](/examples/templates)) 163 | * [Transmissions](/docs/resources/transmissions.md) - `client.transmissions` ([examples](/examples/transmissions)) 164 | * [Webhooks](/docs/resources/webhooks.md) - `client.webhooks` ([examples](/examples/webhooks)) 165 | 166 | 167 | ## Development 168 | 169 | ### Setup 170 | Run `npm install` inside the repository to install all the dev dependencies. 171 | 172 | ### Testing 173 | Once all the dependencies are installed, you can execute the unit tests using `npm test` 174 | 175 | ### Contributing 176 | [Guidelines for adding issues](docs/ADDING_ISSUES.md) 177 | 178 | [Our coding standards](docs/CODE_STYLE_GUIDE.md) 179 | 180 | [Submitting pull requests](CONTRIBUTING.md) 181 | 182 | ### ChangeLog 183 | 184 | [See ChangeLog here](CHANGELOG.md) 185 | 186 | [sparkpost sign up]: https://app.sparkpost.com/join?plan=free-0817?src=Social%20Media&sfdcid=70160000000pqBb&pc=GitHubSignUp&utm_source=github&utm_medium=social-media&utm_campaign=github&utm_content=sign-up 187 | -------------------------------------------------------------------------------- /docs/ADDING_ISSUES.md: -------------------------------------------------------------------------------- 1 | # Guidelines for Adding Issues to SparkPost 2 | Thank you for finding an issue, the SparkPost SDKs are open sourced and focused on helping our developers to be successful. Please review the following checklist before submitting a new issue. 3 | 4 | 1. To prevent duplication, please [search for issues](https://github.com/SparkPost/node-sparkpost/issues) and make sure it doesn’t already exist 5 | 2. Please provide the following information to help our contributors in resolving the issue: 6 | 1. Operating System and Version 7 | 2. Programming language version 8 | 3. Exception, error or issue summary 9 | 4. Recreation steps or Use case summary 10 | 5. Example data (if available) 11 | 6. Expected result or desired functionality 12 | 7. Any extra information you’d like to provide 13 | 3. Please give your issue a meaningful name 14 | 4. Please, no trolling or flaming comments on issues (counter-productive) 15 | 5. Please follow/watch your issue. We expect you to take issue reporting as seriously as we do. 16 | -------------------------------------------------------------------------------- /docs/async.md: -------------------------------------------------------------------------------- 1 | # Async Handling 2 | 3 | Callbacks and promises living together... MASS HYSTERIA. Or not! This library handles both of your favorite async strategies. 4 | 5 | ### Promises 6 | 7 | All our library methods return promises, so if you're a promise-fan, just do what you normally do. 8 | 9 | ```javascript 10 | let client = new SparkPost(key); 11 | 12 | client.templates.get(id) 13 | .then((data) => { 14 | // this the full API response body 15 | }) 16 | .catch((err) => { 17 | // handle the sad error 18 | }); 19 | ``` 20 | 21 | ### Callbacks 22 | 23 | If you're more of a callbacker, that works too. Pass a callback as the last argument and it'll be handled like a regular, error-first callback. 24 | 25 | ```javascript 26 | let client = new SparkPost(key); 27 | 28 | client.templates.get(id, (err, data) => { 29 | if (err) { 30 | // handle the sad error 31 | return; 32 | } 33 | 34 | // this is the full API response body 35 | }); 36 | ``` 37 | -------------------------------------------------------------------------------- /docs/resources/events.md: -------------------------------------------------------------------------------- 1 | # Events 2 | 3 | This library provides easy access to the [Events](https://developers.sparkpost.com/api/events) resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **searchMessage([params, callback])**
9 | Search for events of type message using the given parameters (NOTE: all params are optional): 10 | * `params` - a hash of [Events URI Parameters](https://developers.sparkpost.com/api/events.html#events-get-search-for-message-events) 11 | 12 | ## Date/Time Parameter Format 13 | 14 | The `from` and `to` search parameters accept date stamps of the form: 15 | 16 | `YYYY-MM-DDTHH:MM` 17 | 18 | For example: `2016-11-14T16:15`. 19 | 20 | Note: timestamps are expressed in the timezone specified by the `timezone` parameter or UTC by default. 21 | 22 | ## Examples 23 | 24 | Visit our examples section to see all of [our events resource examples](/examples/events). 25 | -------------------------------------------------------------------------------- /docs/resources/inboundDomains.md: -------------------------------------------------------------------------------- 1 | # Inbound Domains 2 | 3 | This library provides easy access to the [Inbound Domains](https://developers.sparkpost.com/api/inbound-domains) Resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **list()**
9 | List an overview of all inbound domains in the account. 10 | * **get(domain)**
11 | Get an inbound domain by its domain name 12 | * `domain` - the name of the domain you want to look up **required** 13 | * **create(createOpts)**
14 | Create a new inbound domain 15 | * `createOpts` - a hash of [inbound domain attributes](https://developers.sparkpost.com/api/inbound-domains#header-inbound-domains-attributes) **required** 16 | * **delete(domain)**
17 | Delete an existing inbound domain 18 | * `domain` - the name of the domain you want to delete **required** 19 | 20 | ## Examples 21 | 22 | Visit our examples section to see all of [our inbound domains resource examples](/examples/inboundDomains). 23 | -------------------------------------------------------------------------------- /docs/resources/messageEvents.md: -------------------------------------------------------------------------------- 1 | # Message Events 2 | 3 | This library provides easy access to the [Message Events](https://developers.sparkpost.com/api/message-events) resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **search([params, callback])**
9 | Search for message events using the given parameters (NOTE: all params are optional): 10 | * `params` - a hash of [Message Events URI Parameters](https://developers.sparkpost.com/api/message-events.html#message-events-message-events-get) 11 | 12 | ## Date/Time Parameter Format 13 | 14 | The `from` and `to` search parameters accept date stamps of the form: 15 | 16 | `YYYY-MM-DDTHH:MM` 17 | 18 | For example: `2016-11-14T16:15`. 19 | 20 | Note: timestamps are expressed in the timezone specified by the `timezone` parameter or UTC by default. 21 | 22 | ## Examples 23 | 24 | Visit our examples section to see all of [our message events resource examples](/examples/messageEvents). 25 | -------------------------------------------------------------------------------- /docs/resources/recipientLists.md: -------------------------------------------------------------------------------- 1 | # Recipient Lists 2 | 3 | This library provides easy access to the [Recipient Lists](https://developers.sparkpost.com/api/recipient-lists) Resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **list()**
9 | List a summary of all recipient lists. 10 | 11 | * **get(id[, options])**
12 | Retrieve details about a specified recipient list by its id 13 | * `options.show_recipients` - specifies whether to retrieve the recipients | Default: `false` 14 | 15 | * **create(recipientList)**
16 | Create a new recipient list 17 | * `recipientList` - an object of [recipient list](https://developers.sparkpost.com/api/recipient-lists#header-recipient-list-attributes) **required** 18 | * `recipientList.num_rcpt_errors` - limit the number of recipient errors returned | Default: all errors returned 19 | 20 | * **update(id, recipientList)**
21 | Update an existing recipient list 22 | * `recipientList` - an object of [recipient list](https://developers.sparkpost.com/api/recipient-lists#header-recipient-list-attributes) **required** 23 | * `recipientList.num_rcpt_errors` - limit the number of recipient errors returned | Default: all errors returned 24 | 25 | * **delete(id)**
26 | Delete an existing recipient list 27 | * `id` - the id of the recipient list you want to delete **required** 28 | 29 | ## Examples 30 | 31 | Visit our examples section to see all of [our recipient list resource examples](/examples/recipientLists). 32 | -------------------------------------------------------------------------------- /docs/resources/relayWebhooks.md: -------------------------------------------------------------------------------- 1 | # Relay Webhooks 2 | 3 | This library provides easy access to the [Relay Webhooks](https://developers.sparkpost.com/api/relay-webhooks) Resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **list()**
9 | List all relay webhooks. 10 | * **get(id)**
11 | Get details about a specified relay webhook by its id 12 | * `id` - the id of the relay webhook you want to look up **required** 13 | * **create(webhook)**
14 | Create a new relay webhook 15 | * `webhook` - an object of [relay webhook attributes](https://developers.sparkpost.com/api/relay-webhooks#header-relay-webhooks-object-properties) **required** 16 | * **update(id, webhook)**
17 | Update an existing relay webhook 18 | * `id` - the id of the relay webhook you want to update **required** 19 | * `webhook` - an object of [relay webhook attributes](https://developers.sparkpost.com/api/relay-webhooks#header-relay-webhooks-object-properties) **required** 20 | * **delete(id)**
21 | Delete an existing relay webhook 22 | * `id` - the id of the relay webhook you want to delete **required** 23 | 24 | ## Examples 25 | 26 | Visit our examples section to see all of [our relay webhook resource examples](/examples/relayWebhooks). 27 | -------------------------------------------------------------------------------- /docs/resources/sendingDomains.md: -------------------------------------------------------------------------------- 1 | # Sending Domains 2 | 3 | This library provides easy access to the [Sending Domains](https://developers.sparkpost.com/api/sending-domains) Resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **list()**
9 | List an overview of all sending domains in the account. 10 | 11 | * **get(domain)**
12 | Retrieve a sending domain by its domain name 13 | * `domain` - the domain you want to look up **required** 14 | 15 | * **create(createOpts)**
16 | Create a new sending domain 17 | * `createOpts` - a hash of [sending domain attributes](https://developers.sparkpost.com/api/sending-domains#header-sending-domain-attributes) **required** 18 | 19 | * **update(domain, updateOpts)**
20 | Update an existing sending domain 21 | * `domain` - the domain you want to update **required** 22 | * `updateOpts` - a hash of [sending domain attributes](https://developers.sparkpost.com/api/sending-domains#header-sending-domain-attributes) **required** 23 | 24 | * **delete(domain)**
25 | Delete an existing sending domain 26 | * `domain` - the domain you want to delete **required** 27 | 28 | * **verify(domain, options)**
29 | Validate the specified verification field types for a sending domain 30 | * `domain` - the domain you want to verify **required** 31 | * `options` - a hash of [verify attributes](https://developers.sparkpost.com/api/sending-domains#header-verify-attributes) **required** 32 | 33 | ## Examples 34 | 35 | Visit our examples section to see all of [our sending domains resource examples](/examples/sendingDomains). 36 | -------------------------------------------------------------------------------- /docs/resources/subaccounts.md: -------------------------------------------------------------------------------- 1 | # Subaccounts 2 | 3 | This library provides easy access to the [Subaccounts](https://developers.sparkpost.com/api/subaccounts) Resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **list([callback])**
9 | List a summary of all subaccounts. 10 | * **get(id[, callback])**
11 | Get details about a specified subaccount by its id 12 | * `id` - the id of the subaccount you want to look up **required** 13 | * **create(subaccount[, callback])**
14 | Create a new subaccount 15 | * `subaccount` - an object of [subaccount attributes](https://developers.sparkpost.com/api/subaccounts#header-request-body-attributes) **required** 16 | * **update(id, subaccount[, callback])**
17 | Updates an existing subaccount 18 | * `id` - the id of the subaccount you want to update **required** 19 | * `subaccount` - an object of [updatable subaccount attributes](https://developers.sparkpost.com/api/subaccounts#header-request-body-attributes-1) **required** 20 | 21 | ## Examples 22 | 23 | 24 | Visit our examples section to see all of [our subaccount resource examples](/examples/subaccounts). 25 | -------------------------------------------------------------------------------- /docs/resources/suppressionList.md: -------------------------------------------------------------------------------- 1 | # Suppression List 2 | 3 | This library provides easy access to the [Suppression List](https://developers.sparkpost.com/api/suppression-list) Resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **list([parameters])**
9 | List all entries in your suppression list, filtered by an optional set of search parameters. 10 | * `parameters` - an object of [search parameters](https://developers.sparkpost.com/api/suppression-list#suppression-list-search-get) 11 | * **get(email)**
12 | Retrieve an entry by recipient email. 13 | * `email` - `String` email address to check **required** 14 | * **upsert(listEntries)**
15 | Insert or update one or many entries. 16 | * `listEntries` - an object [entry list attributes](https://developers.sparkpost.com/api/suppression-list#header-list-entry-attributes) or `Array` of entry list attribute objects 17 | * **delete(email)**
18 | Remove an entry by recipient email. 19 | * `email` - `String` email address to remove **required** 20 | 21 | ## Examples 22 | 23 | Visit our examples section to see all of [our suppression list resource examples](/examples/suppressionList). 24 | -------------------------------------------------------------------------------- /docs/resources/templates.md: -------------------------------------------------------------------------------- 1 | # Templates 2 | 3 | This library provides easy access to the [Templates](https://developers.sparkpost.com/api/templates) Resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **list()**
9 | List a summary of all templates. 10 | * **get(id[, options])**
11 | Get details about a specified template by its id 12 | * `options.id` - the id of the template you want to look up **required** 13 | * `options.draft` - specifies a draft or published template 14 | * **create(template)**
15 | Create a new template 16 | * `template` - an object of [template attributes](https://developers.sparkpost.com/api/templates#header-template-attributes) **required** 17 | * **update(id, template[, options])**
18 | Update an existing template 19 | * `id` - the id of the template you want to update **required** 20 | * `template` - an object of [template attributes](https://developers.sparkpost.com/api/templates#header-template-attributes) **required** 21 | * `options.update_published` - If true, directly overwrite the existing published template. If false, create a new draft. 22 | * **delete(id)**
23 | Delete an existing template 24 | * `id` - the id of the template you want to delete **required** 25 | * **preview(id[, options])**
26 | Preview the most recent version of an existing template by id 27 | * `id` - the id of the template you want to look up **required** 28 | * `options.substitution_data` - Object of substitution data 29 | * `options.draft` - specifies a draft or published template 30 | 31 | ## Examples 32 | 33 | 34 | Visit our examples section to see all of [our template resource examples](/examples/templates). 35 | -------------------------------------------------------------------------------- /docs/resources/transmissions.md: -------------------------------------------------------------------------------- 1 | # Transmissions 2 | 3 | This library provides easy access to the [Transmissions](https://developers.sparkpost.com/api/transmissions) Resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **list(options[, callback])**
9 | List an overview of all transmissions in the account 10 | * `options.campaign_id` - id of the campaign used by the transmission 11 | * `options.template_id` - id of the template used by the transmission 12 | * **get(id[, callback])**
13 | Retrieve the details about a transmission by its ID 14 | * `id` - id of the transmission you want to look up **required** 15 | * **send(transmission[, options, callback])**
16 | Sends a message by creating a new transmission 17 | * `transmission` - an object of [transmission attributes](https://developers.sparkpost.com/api/transmissions#header-transmission-attributes) 18 | * `transmission.cc` - Recipients to receive a carbon copy of the transmission 19 | * `transmission.bcc` - Recipients to discreetly receive a carbon copy of the transmission 20 | * `options.num_rcpt_errors` - maximum number of recipient errors returned 21 | 22 | ## Examples 23 | 24 | Visit our examples section to see all of [our transmissions resource examples](/examples/transmissions). 25 | 26 | ## Tips and Tricks 27 | * If you specify a stored recipient list and inline recipients in a Transmission, you will receive an error. 28 | * If you specify HTML and/or Plain Text content and then provide RFC-822 encoded content, you will receive an error. 29 | * RFC-822 content is not valid with any other content type. 30 | * If you specify a stored template and also provide inline content, you will receive an error. 31 | * By default, open and click tracking are enabled for a transmission. 32 | * By default, a transmission will use the published version of a stored template. 33 | -------------------------------------------------------------------------------- /docs/resources/webhooks.md: -------------------------------------------------------------------------------- 1 | # Webhooks 2 | 3 | This library provides easy access to the [Webhooks](https://developers.sparkpost.com/api/webhooks) Resource. 4 | 5 | *Note: All methods return promises and accept an optional last argument callback. [Read about how we handle callbacks and promises](/docs/async.md).* 6 | 7 | ## Methods 8 | * **list([options])**
9 | Lists all webhooks. 10 | * `options.timezone` - the timezone to use for the `last_successful` and `last_failure` properties | Default: `UTC` 11 | 12 | * **get(id[, options])**
13 | Get a single webhook by ID. 14 | * `id` - the id of the webhook to get **required** 15 | * `options.timezone` - the timezone to use for the `last_successful` and `last_failure` properties | Default: `UTC` 16 | 17 | * **create(webhook)**
18 | Create a new webhook. 19 | * `webhook` - a hash of [webhook attributes](https://developers.sparkpost.com/api/webhooks#header-webhooks-object-properties) **required** 20 | 21 | client.webhooks.all() 22 | .then(data => { 23 | console.log('Congrats you can use our client library!'); 24 | console.log(data); 25 | }) 26 | .catch(err => { 27 | console.log('Whoops! Something went wrong'); 28 | console.log(err); 29 | }); 30 | 31 | * **update(id, webhook)**
32 | Update an existing webhook. 33 | * `id` - the id of the webhook to update **required** 34 | * `webhook` - a hash of [webhook attributes](https://developers.sparkpost.com/api/webhooks#header-webhooks-object-properties) **required** 35 | * **delete(id)**
36 | Delete an existing webhook. 37 | * `id` - the id of the webhook to delete **required** 38 | 39 | * **validate(id, options)**
40 | Sends an example message event batch from the Webhook API to the target URL. 41 | * `id` - the id of the webhook to validate **required** 42 | * `options.message` - the message (payload) to send to the webhook consumer **required** 43 | 44 | * **getBatchStatus(id[, options])**
45 | Gets recent status information about a webhook. 46 | * `id` - the id of the webhook **required** 47 | * `options.limit` - maximum number of results to return | Default: `1000` 48 | 49 | * **getDocumentation()**
50 | Lists descriptions of the events, event types, and event fields that could be included in a webhooks post to your target URL. 51 | 52 | * **getSamples(options)**
53 | Lists examples of the event data that will be posted to a webhook consumer. 54 | * `options.events` - [event types](https://support.sparkpost.com/customer/portal/articles/1976204) for which to get a sample payload | Default: all event types returned 55 | 56 | 57 | Visit our examples section to see all of [our webhooks resource examples](/examples/webhooks). 58 | -------------------------------------------------------------------------------- /docs/sparkpost_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SparkPost/node-sparkpost/e41ecd697ae99e711ee8a96c49156aa4b3e90bf3/docs/sparkpost_logo.png -------------------------------------------------------------------------------- /examples/baseObject/getDomainsList.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | uri: 'metrics/domains' 8 | }; 9 | 10 | client.get(options) 11 | .then(data => { 12 | console.log('Congrats you can use our client library!'); 13 | console.log(data); 14 | }) 15 | .catch(err => { 16 | console.log('Whoops! Something went wrong'); 17 | console.log(err); 18 | }); 19 | 20 | // Using a callback 21 | client.get(options, function(err, data) { 22 | if(err) { 23 | console.log('Whoops! Something went wrong'); 24 | console.log(err); 25 | } else { 26 | console.log('Congrats you can use our client library!'); 27 | console.log(data); 28 | } 29 | }); 30 | -------------------------------------------------------------------------------- /examples/events/search_message.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Returns 1000 message events for the last hour 8 | 9 | // Promise 10 | client.events.searchMessage({}) 11 | .then(data => { 12 | console.log('Congrats you can use our client library!'); 13 | console.log(data); 14 | }) 15 | .catch(err => { 16 | console.log('Whoops! Something went wrong'); 17 | console.log(err); 18 | }); 19 | 20 | // Callback 21 | client.events.searchMessage({}, function(err, data) { 22 | if (err) { 23 | console.log('Whoops! Something went wrong'); 24 | console.log(err); 25 | } else { 26 | console.log('Congrats you can use our client library!'); 27 | console.log(data); 28 | } 29 | }); 30 | -------------------------------------------------------------------------------- /examples/inboundDomains/create.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , createOpts = {domain: 'example1.com'}; 7 | 8 | // Promise 9 | client.inboundDomains.create(createOpts) 10 | .then(data => { 11 | console.log('Congrats you can use our client library!'); 12 | console.log(data); 13 | }) 14 | .catch(err => { 15 | console.log('Whoops! Something went wrong'); 16 | console.log(err); 17 | }); 18 | 19 | // Callback 20 | client.inboundDomains.create(createOpts, function(err, data) { 21 | if (err) { 22 | console.log('Whoops! Something went wrong'); 23 | console.log(err); 24 | } else { 25 | console.log('Congrats you can use our client library!'); 26 | console.log(data); 27 | } 28 | }); 29 | -------------------------------------------------------------------------------- /examples/inboundDomains/delete.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.inboundDomains.delete('example1.com') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.inboundDomains.delete('example1.com', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/inboundDomains/get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.inboundDomains.get('example1.com') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.inboundDomains.get('example1.com', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/inboundDomains/list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.inboundDomains.list() 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.inboundDomains.list(function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/messageEvents/search_campaignClicks.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , searchParams = { 7 | events: 'click', 8 | campaign_ids: 'monday_mailshot' 9 | }; 10 | 11 | // Promise 12 | client.messageEvents.search(searchParams) 13 | .then(data => { 14 | console.log('Congrats you can use our client library!'); 15 | console.log(data); 16 | }) 17 | .catch(err => { 18 | console.log('Whoops! Something went wrong'); 19 | console.log(err); 20 | }); 21 | 22 | // Callback 23 | client.messageEvents.search(searchParams, function(err, data) { 24 | if (err) { 25 | console.log('Whoops! Something went wrong'); 26 | console.log(err); 27 | } else { 28 | console.log('Congrats you can use our client library!'); 29 | console.log(data); 30 | } 31 | }); 32 | -------------------------------------------------------------------------------- /examples/messageEvents/search_default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Returns 1000 events for the last hour 8 | 9 | // Promise 10 | client.messageEvents.search({}) 11 | .then(data => { 12 | console.log('Congrats you can use our client library!'); 13 | console.log(data); 14 | }) 15 | .catch(err => { 16 | console.log('Whoops! Something went wrong'); 17 | console.log(err); 18 | }); 19 | 20 | // Callback 21 | client.messageEvents.search({}, function(err, data) { 22 | if (err) { 23 | console.log('Whoops! Something went wrong'); 24 | console.log(err); 25 | } else { 26 | console.log('Congrats you can use our client library!'); 27 | console.log(data); 28 | } 29 | }); 30 | -------------------------------------------------------------------------------- /examples/messageEvents/search_messageEvents.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , searchParams = { 7 | from: '2016-01-01T00:00', 8 | to: '2016-01-02T23:59', 9 | page: 1, 10 | per_page: 5, 11 | events: ['bounce', 'out_of_band'], 12 | bounce_classes: [10] 13 | }; 14 | 15 | // Promise 16 | client.messageEvents.search(searchParams) 17 | .then(data => { 18 | console.log('Congrats you can use our client library!'); 19 | console.log(data); 20 | }) 21 | .catch(err => { 22 | console.log('Whoops! Something went wrong'); 23 | console.log(err); 24 | }); 25 | 26 | // Callback 27 | client.messageEvents.search(searchParams, function(err, data) { 28 | if (err) { 29 | console.log('Whoops! Something went wrong'); 30 | console.log(err); 31 | } else { 32 | console.log('Congrats you can use our client library!'); 33 | console.log(data); 34 | } 35 | }); 36 | -------------------------------------------------------------------------------- /examples/recipientLists/create.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , recipientList = { 7 | id: 'UNIQUE_TEST_ID' 8 | , name: 'Test Recipient List' 9 | , recipients: [ 10 | { 11 | address: { 12 | email: 'test1@test.com' 13 | } 14 | } 15 | , { 16 | address: { 17 | email: 'test2@test.com' 18 | } 19 | } 20 | , { 21 | address: { 22 | email: 'test3@test.com' 23 | } 24 | } 25 | ] 26 | }; 27 | 28 | // Promise 29 | client.recipientLists.create(recipientList) 30 | .then(data => { 31 | console.log('Congrats you can use our client library!'); 32 | console.log(data); 33 | }) 34 | .catch(err => { 35 | console.log('Whoops! Something went wrong'); 36 | console.log(err); 37 | }); 38 | 39 | // Callback 40 | client.recipientLists.create(recipientList, function(err, data) { 41 | if (err) { 42 | console.log('Whoops! Something went wrong'); 43 | console.log(err); 44 | } else { 45 | console.log('Congrats you can use our client library!'); 46 | console.log(data); 47 | } 48 | }); 49 | -------------------------------------------------------------------------------- /examples/recipientLists/delete.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.recipientLists.delete('UNIQUE_TEST_ID') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.recipientLists.delete('UNIQUE_TEST_ID', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/recipientLists/get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | show_recipients: true 8 | }; 9 | 10 | // Promise 11 | client.recipientLists.get('UNIQUE_TEST_ID', options) 12 | .then(data => { 13 | console.log('Congrats you can use our client library!'); 14 | console.log(data); 15 | }) 16 | .catch(err => { 17 | console.log('Whoops! Something went wrong'); 18 | console.log(err); 19 | }); 20 | 21 | // Callback 22 | client.recipientLists.get('UNIQUE_TEST_ID', options, function(err, data) { 23 | if (err) { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | } else { 27 | console.log('Congrats you can use our client library!'); 28 | console.log(data); 29 | } 30 | }); 31 | -------------------------------------------------------------------------------- /examples/recipientLists/list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.recipientLists.list() 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.recipientLists.list(function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/recipientLists/update.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , recipientList = { 7 | id: 'EXISTING_TEST_ID' 8 | , name: 'Test Recipient List' 9 | , recipients: [ 10 | { 11 | address: { 12 | email: 'test1@test.com' 13 | } 14 | }, 15 | { 16 | address: { 17 | email: 'test2@test.com' 18 | } 19 | }, 20 | { 21 | address: { 22 | email: 'test3@test.com' 23 | } 24 | } 25 | ] 26 | }; 27 | 28 | // Promise 29 | client.recipientLists.update(recipientList) 30 | .then(data => { 31 | console.log('Congrats you can use our client library!'); 32 | console.log(data); 33 | }) 34 | .catch(err => { 35 | console.log(err); 36 | }); 37 | 38 | // Callback 39 | client.recipientLists.update(recipientList, function(err, data) { 40 | if (err) { 41 | console.log('Whoops! Something went wrong'); 42 | console.log(err); 43 | } else { 44 | console.log('Congrats you can use our client library!'); 45 | console.log(data); 46 | } 47 | }); 48 | -------------------------------------------------------------------------------- /examples/relayWebhooks/create.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , webhook = { 7 | name: 'Test Relay Webhook', 8 | target: 'http://client.test.com/test-webhook', 9 | match: { 10 | domain: 'inbound.example.com' 11 | } 12 | }; 13 | 14 | // Promise 15 | client.relayWebhooks.create(webhook) 16 | .then(data => { 17 | console.log('Congrats you can use our client library!'); 18 | console.log(data); 19 | }) 20 | .catch(err => { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | }); 24 | 25 | // Callback 26 | client.relayWebhooks.create(webhook, function(err, data) { 27 | if (err) { 28 | console.log('Whoops! Something went wrong'); 29 | console.log(err); 30 | } else { 31 | console.log('Congrats you can use our client library!'); 32 | console.log(data); 33 | } 34 | }); 35 | -------------------------------------------------------------------------------- /examples/relayWebhooks/delete.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.relayWebhooks.delete('123456789') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.relayWebhooks.delete('123456789', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/relayWebhooks/get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.relayWebhooks.get('123456789') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.relayWebhooks.get('123456789', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/relayWebhooks/list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.relayWebhooks.list() 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.relayWebhooks.list(function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/relayWebhooks/update.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , webhook = { 7 | target: 'http://client.test.com/test-webhook' 8 | }; 9 | 10 | // Promise 11 | client.relayWebhooks.update('123456789', webhook) 12 | .then(data => { 13 | console.log('Congrats you can use our client library!'); 14 | console.log(data); 15 | }) 16 | .catch(err => { 17 | console.log('Whoops! Something went wrong'); 18 | console.log(err); 19 | }); 20 | 21 | // Callback 22 | client.relayWebhooks.update('123456789', webhook, function(err, data) { 23 | if (err) { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | } else { 27 | console.log('Congrats you can use our client library!'); 28 | console.log(data); 29 | } 30 | }); 31 | -------------------------------------------------------------------------------- /examples/sendingDomains/create.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , createOpts = { 7 | domain: 'example1.com', 8 | dkim: { 9 | 'private': 'MIICXgIBAAKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQABAoGBAITb3BCRPBi5lGhHdn+1RgC7cjUQEbSb4eFHm+ULRwQ0UIPWHwiVWtptZ09usHq989fKp1g/PfcNzm8c78uTS6gCxfECweFCRK6EdO6cCCr1cfWvmBdSjzYhODUdQeyWZi2ozqd0FhGWoV4VHseh4iLj36DzleTLtOZj3FhAo1WJAkEA68T+KkGeDyWwvttYtuSiQCCTrXYAWTQnkIUxduCp7Ap6tVeIDn3TaXTj74UbEgaNgLhjG4bX//fdeDW6PaK9YwJBAM6xJmwHLPMgwNVjiz3u/6fhY3kaZTWcxtMkXCjh1QE82KzDwqyrCg7EFjTtFysSHCAZxXZMcivGl4TZLHnydJUCQQCx16+M+mAatuiCnvxlQUMuMiSTNK6Amzm45u9v53nlZeY3weYMYFdHdfe1pebMiwrT7MI9clKebz6svYJVmdtXAkApDAc8VuR3WB7TgdRKNWdyGJGfoD1PO1ZE4iinOcoKV+IT1UCY99Kkgg6C7j62n/8T5OpRBvd5eBPpHxP1F9BNAkEA5Nf2VO9lcTetksHdIeKK+F7sio6UZn0Rv7iUo3ALrN1D1cGfWIh2dj3ko1iSreyNVSwGW0ePP27qDmU+u6/Y1g==', 10 | 'public': 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQAB', 11 | selector: 'brisbane', 12 | headers: 'from:to:subject:date' 13 | } 14 | }; 15 | 16 | // Promise 17 | client.sendingDomains.create(createOpts) 18 | .then(data => { 19 | console.log('Congrats you can use our client library!'); 20 | console.log(data); 21 | }) 22 | .catch(err => { 23 | console.log('Whoops! Something went wrong'); 24 | console.log(err); 25 | }); 26 | 27 | // Callback 28 | client.sendingDomains.create(createOpts, function(err, data) { 29 | if (err) { 30 | console.log('Whoops! Something went wrong'); 31 | console.log(err); 32 | } else { 33 | console.log('Congrats you can use our client library!'); 34 | console.log(data); 35 | } 36 | }); 37 | -------------------------------------------------------------------------------- /examples/sendingDomains/delete.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.sendingDomains.delete('example1.com') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.sendingDomains.delete('example1.com', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/sendingDomains/get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.sendingDomains.get('example1.com') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.sendingDomains.get('example1.com', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/sendingDomains/list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.sendingDomains.list() 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.sendingDomains.list(function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/sendingDomains/update.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , updateOpts = { 7 | dkim: { 8 | 'private': 'MIICXgIBAAKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQABAoGBAITb3BCRPBi5lGhHdn+1RgC7cjUQEbSb4eFHm+ULRwQ0UIPWHwiVWtptZ09usHq989fKp1g/PfcNzm8c78uTS6gCxfECweFCRK6EdO6cCCr1cfWvmBdSjzYhODUdQeyWZi2ozqd0FhGWoV4VHseh4iLj36DzleTLtOZj3FhAo1WJAkEA68T+KkGeDyWwvttYtuSiQCCTrXYAWTQnkIUxduCp7Ap6tVeIDn3TaXTj74UbEgaNgLhjG4bX//fdeDW6PaK9YwJBAM6xJmwHLPMgwNVjiz3u/6fhY3kaZTWcxtMkXCjh1QE82KzDwqyrCg7EFjTtFysSHCAZxXZMcivGl4TZLHnydJUCQQCx16+M+mAatuiCnvxlQUMuMiSTNK6Amzm45u9v53nlZeY3weYMYFdHdfe1pebMiwrT7MI9clKebz6svYJVmdtXAkApDAc8VuR3WB7TgdRKNWdyGJGfoD1PO1ZE4iinOcoKV+IT1UCY99Kkgg6C7j62n/8T5OpRBvd5eBPpHxP1F9BNAkEA5Nf2VO9lcTetksHdIeKK+F7sio6UZn0Rv7iUo3ALrN1D1cGfWIh2dj3ko1iSreyNVSwGW0ePP27qDmU+u6/Y1g==', 9 | 'public': 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+W6scd3XWwvC/hPRksfDYFi3ztgyS9OSqnnjtNQeDdTSD1DRx/xFar2wjmzxp2+SnJ5pspaF77VZveN3P/HVmXZVghr3asoV9WBx/uW1nDIUxU35L4juXiTwsMAbgMyh3NqIKTNKyMDy4P8vpEhtH1iv/BrwMdBjHDVCycB8WnwIDAQAB', 10 | selector: 'hello_selector', 11 | headers: 'from:to:subject:date' 12 | } 13 | }; 14 | 15 | client.sendingDomains.update('example1.com', updateOpts) 16 | .then(data => { 17 | console.log('Congrats you can use our client library!'); 18 | console.log(data); 19 | }) 20 | .catch(err => { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | }); 24 | 25 | // Using a callback 26 | client.sendingDomains.update('example1.com', updateOpts, function(err, data) { 27 | if (err) { 28 | console.log('Whoops! Something went wrong'); 29 | console.log(err); 30 | } else { 31 | console.log('Congrats you can use our client library!'); 32 | console.log(data); 33 | } 34 | }); 35 | -------------------------------------------------------------------------------- /examples/sendingDomains/verify.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | dkim_verify: true, 8 | spf_verify: true, 9 | abuse_at_verify: true, 10 | postmaster_at_verify: true 11 | }; 12 | 13 | // Promise 14 | client.sendingDomains.verify('example1.com', options) 15 | .then(data => { 16 | console.log('Congrats you can use our client library!'); 17 | console.log(data); 18 | }) 19 | .catch(err => { 20 | console.log('Whoops! Something went wrong'); 21 | console.log(err); 22 | }); 23 | 24 | // Callback 25 | client.sendingDomains.verify('example1.com', options, function(err, data) { 26 | if (err) { 27 | console.log('Whoops! Something went wrong'); 28 | console.log(err); 29 | } else { 30 | console.log('Congrats you can use our client library!'); 31 | console.log(data); 32 | } 33 | }); 34 | -------------------------------------------------------------------------------- /examples/subaccounts/create.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , subaccount = { 7 | name: 'Test Subaccount', 8 | key_label: 'Test Subaccount key', 9 | key_grants: [ 10 | 'smtp/inject', 11 | 'transmissions/modify' 12 | ] 13 | }; 14 | 15 | // Promise 16 | client.subaccounts.create(subaccount) 17 | .then(data => { 18 | console.log('Congrats you can use our client library!'); 19 | console.log(data); 20 | }) 21 | .catch(err => { 22 | console.log('Whoops! Something went wrong'); 23 | console.log(err); 24 | }); 25 | 26 | // Callback 27 | client.subaccounts.create(subaccount, function(err, data) { 28 | if (err) { 29 | console.log('Whoops! Something went wrong'); 30 | console.log(err); 31 | } else { 32 | console.log('Congrats you can use our client library!'); 33 | console.log(data); 34 | } 35 | }); 36 | -------------------------------------------------------------------------------- /examples/subaccounts/get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.subaccounts.get('123') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.subaccounts.get('123', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/subaccounts/list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.subaccounts.list() 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.subaccounts.list(function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/subaccounts/update.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , subaccount = { 7 | name: 'Test Subaccount', 8 | status: 'suspended' 9 | }; 10 | 11 | // Promise 12 | client.subaccounts.update('123', subaccount) 13 | .then(data => { 14 | console.log('Congrats you can use our client library!'); 15 | console.log(data); 16 | }) 17 | .catch(err => { 18 | console.log('Whoops! Something went wrong'); 19 | console.log(err); 20 | }); 21 | 22 | // Callback 23 | client.subaccounts.update('123', subaccount, function(err, data) { 24 | if (err) { 25 | console.log('Whoops! Something went wrong'); 26 | console.log(err); 27 | } else { 28 | console.log('Congrats you can use our client library!'); 29 | console.log(data); 30 | } 31 | }); 32 | -------------------------------------------------------------------------------- /examples/suppressionList/delete.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.suppressionList.delete('test@test.com') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.suppressionList.delete('test@test.com', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/suppressionList/get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.suppressionList.get('test@test.com') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.suppressionList.get('test@test.com', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/suppressionList/list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , parameters = { 7 | from: '2015-05-07T00:00:00+0000', 8 | to: '2015-05-07T23:59:59+0000', 9 | limit: 5 10 | }; 11 | 12 | // Promise 13 | client.suppressionList.list(parameters) 14 | .then(data => { 15 | console.log('Congrats you can use our client library!'); 16 | console.log(data); 17 | }) 18 | .catch(err => { 19 | console.log('Whoops! Something went wrong'); 20 | console.log(err); 21 | }); 22 | 23 | // Callback 24 | client.suppressionList.list(parameters, function(err, data) { 25 | if (err) { 26 | console.log('Whoops! Something went wrong'); 27 | console.log(err); 28 | } else { 29 | console.log('Congrats you can use our client library!'); 30 | console.log(data); 31 | } 32 | }); 33 | -------------------------------------------------------------------------------- /examples/suppressionList/upsert.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , listEntry = { 7 | recipient: 'test1@test.com', 8 | transactional: false, 9 | non_transactional: true, 10 | description: 'Test description 1' 11 | }; 12 | 13 | // Promise 14 | client.suppressionList.upsert(listEntry) 15 | .then(data => { 16 | console.log('Congrats you can use our client library!'); 17 | console.log(data); 18 | }) 19 | .catch(err => { 20 | console.log('Whoops! Something went wrong'); 21 | console.log(err); 22 | }); 23 | 24 | // Callback 25 | client.suppressionList.upsert(listEntry, function(err, data) { 26 | if (err) { 27 | console.log('Whoops! Something went wrong'); 28 | console.log(err); 29 | } else { 30 | console.log('Congrats you can use our client library!'); 31 | console.log(data); 32 | } 33 | }); 34 | -------------------------------------------------------------------------------- /examples/suppressionList/upsert_bulk.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , listEntries = [ 7 | { 8 | recipient: 'test1@test.com', 9 | transactional: false, 10 | non_transactional: true, 11 | description: 'Test description 1' 12 | }, 13 | { 14 | recipient: 'test2@test.com', 15 | transactional: true, 16 | non_transactional: true, 17 | description: 'Test description 2' 18 | }, 19 | { 20 | recipient: 'test3@test.com', 21 | transactional: true, 22 | non_transactional: false, 23 | description: 'Test description 3' 24 | } 25 | ]; 26 | 27 | // Promise 28 | client.suppressionList.upsert(listEntries) 29 | .then(data => { 30 | console.log('Congrats you can use our client library!'); 31 | console.log(data); 32 | }) 33 | .catch(err => { 34 | console.log('Whoops! Something went wrong'); 35 | console.log(err); 36 | }); 37 | 38 | // Callback 39 | client.suppressionList.upsert(listEntries, function(err, data) { 40 | if (err) { 41 | console.log('Whoops! Something went wrong'); 42 | console.log(err); 43 | } else { 44 | console.log('Congrats you can use our client library!'); 45 | console.log(data); 46 | } 47 | }); 48 | -------------------------------------------------------------------------------- /examples/templates/create.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , template = { 7 | id: 'TEST_ID', 8 | name: 'Test Template', 9 | content: { 10 | from: 'test@test.com', 11 | subject: 'Test email template!', 12 | html: 'This is a test email template!' 13 | } 14 | }; 15 | 16 | // Promise 17 | client.templates.create(template) 18 | .then(data => { 19 | console.log('Congrats you can use our client library!'); 20 | console.log(data); 21 | }) 22 | .catch(err => { 23 | console.log('Whoops! Something went wrong'); 24 | console.log(err); 25 | }); 26 | 27 | // Callback 28 | client.templates.create(template, function(err, data) { 29 | if (err) { 30 | console.log('Whoops! Something went wrong'); 31 | console.log(err); 32 | } else { 33 | console.log('Congrats you can use our client library!'); 34 | console.log(data); 35 | } 36 | }); 37 | -------------------------------------------------------------------------------- /examples/templates/delete.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.templates.delete('TEST_ID') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.templates.delete('TEST_ID', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/templates/get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.templates.get('TEST_ID') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.templates.get('TEST_ID', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/templates/get_draft.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | draft: true 8 | }; 9 | 10 | // Promise 11 | client.templates.get('TEST_ID', options) 12 | .then(data => { 13 | console.log('Congrats you can use our client library!'); 14 | console.log(data); 15 | }) 16 | .catch(err => { 17 | console.log('Whoops! Something went wrong'); 18 | console.log(err); 19 | }); 20 | 21 | // Callback 22 | client.templates.get('TEST_ID', options, function(err, data) { 23 | if (err) { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | } else { 27 | console.log('Congrats you can use our client library!'); 28 | console.log(data); 29 | } 30 | }); 31 | -------------------------------------------------------------------------------- /examples/templates/list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.templates.list() 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.templates.list(function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/templates/preview.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , templateID = 'TEST_ID' 7 | , options = { 8 | substitution_data: {} 9 | }; 10 | 11 | client.templates.preview(templateID, options) 12 | .then(data => { 13 | console.log('Congrats you can use our client library!'); 14 | console.log(data); 15 | }) 16 | .catch(err => { 17 | console.log('Whoops! Something went wrong'); 18 | console.log(err); 19 | }); 20 | 21 | // Using a callback 22 | client.templates.preview(templateID, options, function(err, data) { 23 | if (err) { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | } else { 27 | console.log('Congrats you can use our client library!'); 28 | console.log(data); 29 | } 30 | }); 31 | -------------------------------------------------------------------------------- /examples/templates/update.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , template = { 7 | content: { 8 | from: 'test@test.com', 9 | subject: 'Updated Test email template!', 10 | html: 'This is a test email template! Updated!' 11 | } 12 | }; 13 | 14 | // Promise 15 | client.templates.update('TEST_ID', template) 16 | .then(data => { 17 | console.log('Congrats you can use our client library!'); 18 | console.log(data); 19 | }) 20 | .catch(err => { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | }); 24 | 25 | // Callback 26 | client.templates.update('TEST_ID', template, function(err, data) { 27 | if (err) { 28 | console.log('Whoops! Something went wrong'); 29 | console.log(err); 30 | } else { 31 | console.log('Congrats you can use our client library!'); 32 | console.log(data); 33 | } 34 | }); 35 | -------------------------------------------------------------------------------- /examples/templates/update_published.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , template = { 7 | content: { 8 | from: 'test@test.com', 9 | subject: 'Updated Published Test email template!', 10 | html: 'This is a published test email template! Updated!' 11 | } 12 | } 13 | , options = { 14 | update_published: true 15 | }; 16 | 17 | // Promise 18 | client.templates.update('TEST_ID', template, options) 19 | .then(data => { 20 | console.log('Congrats you can use our client library!'); 21 | console.log(data); 22 | }) 23 | .catch(err => { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | }); 27 | 28 | // Callback 29 | client.templates.update('TEST_ID', template, options, function(err, data) { 30 | if (err) { 31 | console.log('Whoops! Something went wrong'); 32 | console.log(err); 33 | } else { 34 | console.log('Congrats you can use our client library!'); 35 | console.log(data); 36 | } 37 | }); 38 | -------------------------------------------------------------------------------- /examples/transmissions/get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.transmissions.get('YOUR-TRANSMISSION-KEY') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.transmissions.get('YOUR-TRANSMISSION-KEY', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/transmissions/list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.transmissions.list() 9 | .then(data => { 10 | console.log(data); 11 | console.log('Congrats you can use our client library!'); 12 | }) 13 | .catch(err => { 14 | console.log(err); 15 | }); 16 | 17 | // Callback 18 | client.transmissions.list(function(err, data) { 19 | if (err) { 20 | console.log(err); 21 | } else { 22 | console.log(data); 23 | console.log('Congrats you can use our client library!'); 24 | } 25 | }); 26 | -------------------------------------------------------------------------------- /examples/transmissions/list_by_campaign.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | campaign_id: 'my_campaign' 8 | }; 9 | 10 | client.transmissions.list(options) 11 | .then(data => { 12 | console.log('Congrats you can use our client library!'); 13 | console.log(data); 14 | }) 15 | .catch(err => { 16 | console.log('Whoops! Something went wrong'); 17 | console.log(err); 18 | }); 19 | 20 | // Using a callback 21 | client.transmissions.list(options, function(err, data) { 22 | if (err) { 23 | console.log('Whoops! Something went wrong'); 24 | console.log(err); 25 | } else { 26 | console.log('Congrats you can use our client library!'); 27 | console.log(data); 28 | } 29 | }); 30 | -------------------------------------------------------------------------------- /examples/transmissions/list_by_template.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | template_id: 'my_template' 8 | }; 9 | 10 | // Promise 11 | client.transmissions.list(options) 12 | .then(data => { 13 | console.log('Congrats you can use our client library!'); 14 | console.log(data); 15 | }) 16 | .catch(err => { 17 | console.log('Whoops! Something went wrong'); 18 | console.log(err); 19 | }); 20 | 21 | // Callback 22 | client.transmissions.list(options, function(err, data) { 23 | if (err) { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | } else { 27 | console.log('Congrats you can use our client library!'); 28 | console.log(data); 29 | } 30 | }); 31 | -------------------------------------------------------------------------------- /examples/transmissions/send_all_fields.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | options: { 8 | open_tracking: true, 9 | click_tracking: true, 10 | transactional: true 11 | }, 12 | campaign_id: 'christmas_campaign', 13 | metadata: { 14 | user_type: 'students' 15 | }, 16 | substitution_data: { 17 | sender: 'Big Store Team' 18 | }, 19 | recipients: [ 20 | { 21 | address: { 22 | email: 'wilma@flintstone.com', 23 | name: 'Wilma Flintstone' 24 | }, 25 | tags: [ 26 | 'greeting', 27 | 'prehistoric', 28 | 'fred', 29 | 'flintstone' 30 | ], 31 | metadata: { 32 | place: 'Bedrock' 33 | }, 34 | substitution_data: { 35 | customer_type: 'Platinum' 36 | } 37 | } 38 | ], 39 | content: { 40 | from: { 41 | name: 'Fred Flintstone', 42 | email: 'fred@flintstone.com' 43 | }, 44 | subject: 'Big Christmas savings!', 45 | reply_to: 'Christmas Sales ', 46 | headers: { 47 | 'X-Customer-Campaign-ID': 'christmas_campaign' 48 | }, 49 | text: 'Hi {{address.name}} \nSave big this Christmas in your area {{place}}! \nClick http://www.mysite.com and get huge discount\n Hurry, this offer is only to {{customer_type}}\n {{sender}}', 50 | html: '

Hi {{address.name}} \nSave big this Christmas in your area {{place}}! \nClick http://www.mysite.com and get huge discount\n

Hurry, this offer is only to {{customer_type}}\n

{{sender}}

' 51 | } 52 | }; 53 | 54 | // Promise 55 | client.transmissions.send(transmission) 56 | .then(data => { 57 | console.log('Congrats you can use our client library!'); 58 | console.log(data); 59 | }) 60 | .catch(err => { 61 | console.log('Whoops! Something went wrong'); 62 | console.log(err); 63 | }); 64 | 65 | // Callback 66 | client.transmissions.send(tranmission, function(err, data) { 67 | if (err) { 68 | console.log('Whoops! Something went wrong'); 69 | console.log(err); 70 | } else { 71 | console.log('Congrats you can use our client library!'); 72 | console.log(data); 73 | } 74 | }); 75 | -------------------------------------------------------------------------------- /examples/transmissions/send_inline_image.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const key = 'YOURAPIKEY'; 4 | const SparkPost = require('sparkpost'); 5 | const client = new SparkPost(key); 6 | const transmission = { 7 | recipients: [{address: {email: 'john.doe@example.com'}}], 8 | content: { 9 | from: 'From Envelope ', 10 | subject: 'Example Email for Inline Image', 11 | html: '

My fantastic HTML content.

SparkPost

', 12 | text: 'Simple text content', 13 | inline_images: [ 14 | { 15 | type: 'image/png', 16 | name: 'AnImage.png', 17 | data: 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAAXxJREFUOBFjvJVg84P5718WBjLAX2bmPyxMf/+xMDH8YyZDPwPDXwYGJkIaOXTNGdiUtHAqI2jA/18/GUQzGsg3gMfKg4FVQo6BiYcPqyF4XcChaczA4+DP8P//f4b/P3+SZgAzvxCDSGYjAyMjI8PvZw+AoYXdLuyiQLtE0uoZWAREwLb+fnKXQTipkngXcJu7MnACQx8G2FX1GHgs3bDGBlYX8HlFM/z9+JbhzewWhmf1CQyfti9j+PfzBwO/ZxTMTDiNmQKBfmZX1GB42V/K8P38YbDCX/dvMDAwMzPwuYbBNcIYmC4AhfjvXwx/376AqQHTf96+ZPj34xuKGIiDaQBQ8PPBTQwCoZkMjJzcYA3MgqIMAr7xDJ/3rAHzkQnGO7FWf5gZ/qLmBSZmBoHgNAZee1+Gf18/MzCyczJ83LyQ4fPetch6Gf4xMP3FbgBMGdAgJqAr/n37zABMTTBROA0ygAWUJUG5Civ4B8xwX78CpbD6FJiHmf4AAFicbTMTr5jAAAAAAElFTkSuQmCC' 18 | } 19 | ] 20 | } 21 | }; 22 | 23 | // Promise 24 | client.transmissions.send(transmission) 25 | .then((data) => { 26 | console.log('Congrats you can use our client library!'); 27 | console.log(data); 28 | }) 29 | .catch((err) => { 30 | console.log('Whoops! Something went wrong'); 31 | console.log(err); 32 | }); 33 | 34 | // Callback 35 | client.transmissions.send(transmission, function(err, data) { 36 | if (err) { 37 | console.log('Whoops! Something went wrong'); 38 | console.log(err); 39 | } else { 40 | console.log('Congrats you can use our client library!'); 41 | console.log(data); 42 | } 43 | }); 44 | -------------------------------------------------------------------------------- /examples/transmissions/send_mime_parts.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | recipients: [{ address: { email: 'john.doe@example.com' } }], 8 | content: { 9 | from: 'From Envelope ', 10 | subject: 'Example Email for MIME Parts', 11 | html: '

Hello World!

', 12 | text: 'Hello World!' 13 | }, 14 | options: { 15 | open_tracking: true, 16 | click_tracking: true 17 | } 18 | }; 19 | 20 | // Promise 21 | client.transmissions.send(transmission) 22 | .then(data => { 23 | console.log('Congrats you can use our client library!'); 24 | console.log(data); 25 | }) 26 | .catch(err => { 27 | console.log('Whoops! Something went wrong'); 28 | console.log(err); 29 | }); 30 | 31 | // Callback 32 | client.transmissions.send(transmission, function(err, data) { 33 | if (err) { 34 | console.log('Whoops! Something went wrong'); 35 | console.log(err); 36 | } else { 37 | console.log('Congrats you can use our client library!'); 38 | console.log(data); 39 | } 40 | }); 41 | -------------------------------------------------------------------------------- /examples/transmissions/send_rfc822.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | recipients: [{address: {email: 'john.doe@example.com'}}], 8 | content: { 9 | email_rfc822: 'Content-Type: text/plain\nFrom: From Envelope \nSubject: Example Email\n\nHello World' 10 | } 11 | }; 12 | 13 | // Promise 14 | client.transmissions.send(transmission) 15 | .then(data => { 16 | console.log('Congrats you can use our client library!'); 17 | onsole.log(data); 18 | }) 19 | .catch(err => { 20 | console.log('Whoops! Something went wrong'); 21 | console.log(err); 22 | }); 23 | 24 | // Callback 25 | client.transmissions.send(transmission, function(err, data) { 26 | if (err) { 27 | console.log('Whoops! Something went wrong'); 28 | console.log(err); 29 | } else { 30 | console.log('Congrats you can use our client library!'); 31 | console.log(data); 32 | } 33 | }); 34 | -------------------------------------------------------------------------------- /examples/transmissions/send_stored_recipients_inline_content.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | recipients: { 8 | list_id: 'example-list' 9 | }, 10 | content: { 11 | from: 'From Envelope ', 12 | subject: 'Example Email for Stored List and Inline Content', 13 | html: '

Hello World

', 14 | text: 'Hello World!' 15 | } 16 | }; 17 | 18 | // Promise 19 | client.transmissions.send(transmission) 20 | .then(data => { 21 | console.log('Congrats you can use our client library!'); 22 | console.log(data); 23 | }) 24 | .catch(err => { 25 | console.log('Whoops! Something went wrong'); 26 | console.log(err); 27 | }); 28 | 29 | // Callback 30 | client.transmissions.send(transmission, function(err, data) { 31 | if (err) { 32 | console.log('Whoops! Something went wrong'); 33 | console.log(err); 34 | } else { 35 | console.log('Congrats you can use our client library!'); 36 | console.log(data); 37 | } 38 | }); 39 | -------------------------------------------------------------------------------- /examples/transmissions/send_stored_recipients_stored_content.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | recipients: { 8 | list_id: 'example-list' 9 | }, 10 | content: { 11 | from: 'From Envelope ', 12 | subject: 'Example Email for Stored List and Template', 13 | template_id: 'my-template' 14 | } 15 | }; 16 | 17 | // Promise 18 | client.transmissions.send(transmission) 19 | .then(data => { 20 | console.log('Congrats you can use our client library!'); 21 | console.log(data); 22 | }) 23 | .catch(err => { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | }); 27 | 28 | // Callback 29 | client.transmissions.send(transmission, function(err, data) { 30 | if (err) { 31 | console.log('Whoops! Something went wrong'); 32 | console.log(err); 33 | } else { 34 | console.log('Congrats you can use our client library!'); 35 | console.log(data); 36 | } 37 | }); 38 | -------------------------------------------------------------------------------- /examples/transmissions/send_stored_template.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | campaign_id: 'ricks-campaign', 8 | content: { 9 | template_id: 'ricks-template' 10 | }, 11 | recipients: [{ address: { email: 'rick.sanchez@rickandmorty100years.com', name: 'Rick Sanchez' } }] 12 | } 13 | , options = { 14 | num_rcpt_errors: 3 15 | }; 16 | 17 | // Promise 18 | client.transmissions.send(transmission, options) 19 | .then(data => { 20 | console.log('What up my glib globs! SparkPost!'); 21 | console.log(data); 22 | }) 23 | .catch(err => { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | }); 27 | 28 | // Callback 29 | client.transmissions.send(transmission, options, function(err, data) { 30 | if (err) { 31 | console.log('Whoops! Something went wrong'); 32 | console.log(err); 33 | } else { 34 | console.log('What up my glib globs! SparkPost!'); 35 | console.log(data); 36 | } 37 | }); 38 | -------------------------------------------------------------------------------- /examples/transmissions/send_with_bcc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | recipients: [ 8 | { 9 | address: { 10 | email: 'original.recipient@example.com', 11 | name: 'Original Recipient' 12 | }, 13 | substitution_data: { 14 | recipient_type: 'Original' 15 | } 16 | }, 17 | { 18 | address: { 19 | email: 'bcc.recipient@example.com', 20 | header_to: '"Original Recipient" ' 21 | }, 22 | substitution_data: { 23 | recipient_type: 'BCC' 24 | } 25 | } 26 | ], 27 | content: { 28 | from: { 29 | name: 'Node BCC Test', 30 | email: 'from@example.com' 31 | }, 32 | subject: 'Example email using bcc', 33 | text: 'An example email using bcc with SparkPost to the {{recipient_type}} recipient.', 34 | html: '

An example email using bcc with SparkPost to the {{recipient_type}} recipient.

' 35 | } 36 | }; 37 | 38 | // Promise 39 | client.transmissions.send(transmission) 40 | .then(data => { 41 | console.log(data); 42 | console.log('Congrats! You sent an email with bcc using SparkPost!'); 43 | }) 44 | .catch(err => { 45 | console.log('Whoops! Something went wrong'); 46 | console.log(err); 47 | }); 48 | 49 | // Callback 50 | client.transmissions.send(transmission, function(err, data) { 51 | if (err) { 52 | console.log('Whoops! Something went wrong'); 53 | console.log(err); 54 | } else { 55 | console.log('Congrats! You sent an email with bcc using SparkPost!'); 56 | console.log(data); 57 | } 58 | }); 59 | -------------------------------------------------------------------------------- /examples/transmissions/send_with_bcc_sugar.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | recipients: [ 8 | { 9 | address: { 10 | email: 'original.recipient@example.com', 11 | name: 'Original Recipient' 12 | }, 13 | substitution_data: { 14 | recipient_type: 'Original' 15 | } 16 | } 17 | ], 18 | bcc: [ 19 | { 20 | address: { 21 | email: 'bcc.recipient@example.com', 22 | }, 23 | substitution_data: { 24 | recipient_type: 'BCC' 25 | } 26 | } 27 | ], 28 | content: { 29 | from: { 30 | name: 'Node BCC Test', 31 | email: 'from@example.com' 32 | }, 33 | subject: 'Example email using bcc', 34 | text: 'An example email using bcc with SparkPost to the {{recipient_type}} recipient.', 35 | html: '

An example email using bcc with SparkPost to the {{recipient_type}} recipient.

' 36 | } 37 | }; 38 | 39 | // Promise 40 | client.transmissions.send(transmission) 41 | .then(data => { 42 | console.log('Congrats! You sent an email with bcc using SparkPost!'); 43 | console.log(data); 44 | }) 45 | .catch(err => { 46 | console.log('Whoops! Something went wrong'); 47 | console.log(err); 48 | }); 49 | 50 | // Callback 51 | client.transmissions.send(transmission, function(err, data) { 52 | if (err) { 53 | console.log('Whoops! Something went wrong'); 54 | console.log(err); 55 | } else { 56 | console.log('Congrats! You sent an email with bcc using SparkPost!'); 57 | console.log(data); 58 | } 59 | }); 60 | -------------------------------------------------------------------------------- /examples/transmissions/send_with_cc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | recipients: [ 8 | { 9 | address: { 10 | email: 'original.recipient@example.com', 11 | name: 'Original Recipient' 12 | }, 13 | substitution_data: { 14 | recipient_type: 'Original' 15 | } 16 | }, 17 | { 18 | address: { 19 | email: 'cc.recipient@example.com', 20 | name: 'Carbon Copy Recipient', 21 | header_to: '"Original Recipient" ' 22 | }, 23 | substitution_data: { 24 | recipient_type: 'CC' 25 | } 26 | } 27 | ], 28 | content: { 29 | from: { 30 | name: 'Node CC Test', 31 | email: 'from@example.com' 32 | }, 33 | headers: { 34 | 'CC': '"Carbon Copy Recipient" ' 35 | }, 36 | subject: 'Example email using cc', 37 | text: 'An example email using cc with SparkPost to the {{recipient_type}} recipient.', 38 | html: '

An example email using cc with SparkPost to the {{recipient_type}} recipient.

' 39 | } 40 | }; 41 | 42 | // Promise 43 | client.transmissions.send(transmission) 44 | .then(data => { 45 | console.log('Congrats! You sent an email with cc using SparkPost!'); 46 | console.log(data); 47 | }) 48 | .catch(err => { 49 | console.log('Whoops! Something went wrong'); 50 | console.log(err); 51 | }); 52 | 53 | // Callback 54 | client.transmissions.send(transmission, function(err, data) { 55 | if (err) { 56 | console.log('Whoops! Something went wrong'); 57 | console.log(err); 58 | } else { 59 | console.log('Congrats! You sent an email with cc using SparkPost!'); 60 | console.log(data); 61 | } 62 | }); -------------------------------------------------------------------------------- /examples/transmissions/send_with_cc_sugar.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , transmission = { 7 | recipients: [ 8 | { 9 | address: { 10 | email: 'original.recipient@example.com', 11 | name: 'Original Recipient' 12 | }, 13 | substitution_data: { 14 | recipient_type: 'Original' 15 | } 16 | }, 17 | ], 18 | cc: [ 19 | { 20 | address: { 21 | email: 'cc.recipient@example.com', 22 | name: 'Carbon Copy Recipient', 23 | }, 24 | substitution_data: { 25 | recipient_type: 'CC' 26 | } 27 | } 28 | ], 29 | content: { 30 | from: { 31 | name: 'Node CC Test', 32 | email: 'from@example.com' 33 | }, 34 | subject: 'Example email using cc', 35 | text: 'An example email using cc with SparkPost to the {{recipient_type}} recipient.', 36 | html: '

An example email using cc with SparkPost to the {{recipient_type}} recipient.

' 37 | } 38 | }; 39 | 40 | // Promise 41 | client.transmissions.send(transmission) 42 | .then(data => { 43 | console.log('Congrats! You sent an email with cc using SparkPost!'); 44 | console.log(data); 45 | }) 46 | .catch(err => { 47 | console.log('Whoops! Something went wrong'); 48 | console.log(err); 49 | }); 50 | 51 | // Callback 52 | client.transmissions.send(transmission, function(err, data) { 53 | if (err) { 54 | console.log('Whoops! Something went wrong'); 55 | console.log(err); 56 | } else { 57 | console.log('Congrats! You sent an email with cc using SparkPost!'); 58 | console.log(data); 59 | } 60 | }); 61 | -------------------------------------------------------------------------------- /examples/webhooks/create.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , webhook = { 7 | name: 'Test Webhook', 8 | target: 'http://client.test.com/test-webhook', 9 | auth_token: 'AUTH_TOKEN', 10 | events: [ 11 | 'delivery', 12 | 'injection', 13 | 'open', 14 | 'click' 15 | ] 16 | }; 17 | 18 | // Promise 19 | client.webhooks.create(webhook) 20 | .then(data => { 21 | console.log('Congrats you can use our client library!'); 22 | console.log(data); 23 | }) 24 | .catch(err => { 25 | console.log('Whoops! Something went wrong'); 26 | console.log(err); 27 | }); 28 | 29 | // Callback 30 | client.webhooks.create(webhook, function(err, data) { 31 | if (err) { 32 | console.log('Whoops! Something went wrong'); 33 | console.log(err); 34 | } else { 35 | console.log('Congrats you can use our client library!'); 36 | console.log(data); 37 | } 38 | }); 39 | -------------------------------------------------------------------------------- /examples/webhooks/delete.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.webhooks.delete('TEST_WEBHOOK_UUID') 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.webhooks.delete('TEST_WEBHOOK_UUID', function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/webhooks/get.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | timezone: 'America/New_York' 8 | }; 9 | 10 | // Promise 11 | client.webhooks.get('TEST_WEBHOOK_UUID', options) 12 | .then(data => { 13 | console.log('Congrats you can use our client library!'); 14 | console.log(data); 15 | }) 16 | .catch(err => { 17 | console.log('Whoops! Something went wrong'); 18 | console.log(err); 19 | }); 20 | 21 | // Callback 22 | client.webhooks.get('TEST_WEBHOOK_UUID', options, function(err, data) { 23 | if (err) { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | } else { 27 | console.log('Congrats you can use our client library!'); 28 | console.log(data); 29 | } 30 | }); 31 | -------------------------------------------------------------------------------- /examples/webhooks/getBatchStatus.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | limit: 1000 8 | }; 9 | 10 | // Promise 11 | client.webhooks.getBatchStatus('TEST_WEBHOOK_UUID', options) 12 | .then(data => { 13 | console.log('Congrats you can use our client library!'); 14 | console.log(data); 15 | }) 16 | .catch(err => { 17 | console.log('Whoops! Something went wrong'); 18 | console.log(err); 19 | }); 20 | 21 | // Callback 22 | client.webhooks.getBatchStatus('TEST_WEBHOOK_UUID', options, function(err, data) { 23 | if (err) { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | } else { 27 | console.log('Congrats you can use our client library!'); 28 | console.log(data); 29 | } 30 | }); 31 | -------------------------------------------------------------------------------- /examples/webhooks/getDocumentation.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.webhooks.getDocumentation() 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.webhooks.getDocumentation(function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/webhooks/getSamples.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | events: 'bounce' 8 | }; 9 | 10 | // Promise 11 | client.webhooks.getSamples(options) 12 | .then(data => { 13 | console.log('Congrats you can use our client library!'); 14 | console.log(data); 15 | }) 16 | .catch(err => { 17 | console.log('Whoops! Something went wrong'); 18 | console.log(err); 19 | }); 20 | 21 | // Callback 22 | client.webhooks.getSamples(options, function(err, data) { 23 | if (err) { 24 | console.log('Whoops! Something went wrong'); 25 | console.log(err); 26 | } else { 27 | console.log('Congrats you can use our client library!'); 28 | console.log(data); 29 | } 30 | }); 31 | -------------------------------------------------------------------------------- /examples/webhooks/list.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key); 6 | 7 | // Promise 8 | client.webhooks.list() 9 | .then(data => { 10 | console.log('Congrats you can use our client library!'); 11 | console.log(data); 12 | }) 13 | .catch(err => { 14 | console.log('Whoops! Something went wrong'); 15 | console.log(err); 16 | }); 17 | 18 | // Callback 19 | client.webhooks.list(function(err, data) { 20 | if (err) { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | } else { 24 | console.log('Congrats you can use our client library!'); 25 | console.log(data); 26 | } 27 | }); 28 | -------------------------------------------------------------------------------- /examples/webhooks/update.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , webhook = { 7 | name: 'Renamed Test Webhook', 8 | events: [ 9 | 'policy_rejection', 10 | 'delay' 11 | ] 12 | }; 13 | 14 | // Promise 15 | client.webhooks.update('TEST_WEBHOOK_UUID', webhook) 16 | .then(data => { 17 | console.log('Congrats you can use our client library!'); 18 | console.log(data); 19 | }) 20 | .catch(err => { 21 | console.log('Whoops! Something went wrong'); 22 | console.log(err); 23 | }); 24 | 25 | // Callback 26 | client.webhooks.update('TEST_WEBHOOK_UUID', webhook, function(err, data) { 27 | if (err) { 28 | console.log('Whoops! Something went wrong'); 29 | console.log(err); 30 | } else { 31 | console.log('Congrats you can use our client library!'); 32 | console.log(data); 33 | } 34 | }); 35 | -------------------------------------------------------------------------------- /examples/webhooks/validate.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var key = 'YOURAPIKEY' 4 | , SparkPost = require('sparkpost') 5 | , client = new SparkPost(key) 6 | , options = { 7 | message: { 8 | msys: {} 9 | } 10 | }; 11 | 12 | // Promise 13 | client.webhooks.validate('TEST_WEBHOOK_UUID', options) 14 | .then(data => { 15 | console.log('Congrats you can use our client library!'); 16 | console.log(data); 17 | }) 18 | .catch(err => { 19 | console.log('Whoops! Something went wrong'); 20 | console.log(err); 21 | }); 22 | 23 | // Callback 24 | client.webhooks.validate('TEST_WEBHOOK_UUID', options, function(err, data) { 25 | if (err) { 26 | console.log('Whoops! Something went wrong'); 27 | console.log(err); 28 | } else { 29 | console.log('Congrats you can use our client library!'); 30 | console.log(data); 31 | } 32 | }); 33 | -------------------------------------------------------------------------------- /lib/events.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'events'; 4 | 5 | /* 6 | * "Class" declaration, Events API exposes one function: 7 | * - search: retrieves list of message events according to given params 8 | */ 9 | module.exports = function(client) { 10 | return { 11 | /** 12 | * Search for events using given parameters 13 | * 14 | * @param {Object} parameters 15 | * @param {RequestCb} [callback] 16 | * @returns {Promise} 17 | */ 18 | searchMessage: function(parameters, callback) { 19 | const options = { 20 | uri: `${api}/message` 21 | , qs: {} 22 | }; 23 | 24 | Object.keys(parameters).forEach(function(paramname) { 25 | if (Array.isArray(parameters[paramname])) { 26 | options.qs[paramname] = parameters[paramname].join(','); 27 | } else { 28 | options.qs[paramname] = parameters[paramname]; 29 | } 30 | }); 31 | return client.get(options, callback); 32 | } 33 | }; 34 | }; 35 | -------------------------------------------------------------------------------- /lib/inboundDomains.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'inbound-domains'; 4 | 5 | module.exports = function(client) { 6 | return { 7 | /** 8 | * List an overview of all inbound domains in the account. 9 | * 10 | * @param {RequestCb} [callback] 11 | * @returns {Promise} 12 | */ 13 | list: function(callback) { 14 | const options = { 15 | uri: api 16 | }; 17 | return client.get(options, callback); 18 | }, 19 | /** 20 | * Get an inbound domain by its domain name 21 | * 22 | * @param {string} domain 23 | * @param {RequestCb} [callback] 24 | * @returns {Promise} 25 | */ 26 | get: function(domain, callback) { 27 | if (!domain || typeof domain !== 'string') { 28 | return client.reject(new Error('domain is required'), callback); 29 | } 30 | 31 | const options = { 32 | uri: `${api}/${domain}` 33 | }; 34 | return client.get(options, callback); 35 | }, 36 | /** 37 | * Create a new inbound domain 38 | * 39 | * @param {Object} createOpts 40 | * @param {RequestCb} [callback] 41 | * @returns {Promise} 42 | */ 43 | create: function(createOpts, callback) { 44 | if (!createOpts || typeof createOpts !== 'object') { 45 | return client.reject(new Error('create options are required'), callback); 46 | } 47 | 48 | const options = { 49 | uri: api 50 | , json: createOpts 51 | }; 52 | return client.post(options, callback); 53 | }, 54 | /** 55 | * Delete an existing inbound domain 56 | * 57 | * @param {string} domain 58 | * @param {RequestCb} [callback] 59 | * @returns {Promise} 60 | */ 61 | delete: function(domain, callback) { 62 | if (!domain || typeof domain !== 'string') { 63 | return client.reject(new Error('domain is required'), callback); 64 | } 65 | 66 | const options = { 67 | uri: `${api}/${domain}` 68 | }; 69 | return client.delete(options, callback); 70 | } 71 | }; 72 | }; 73 | -------------------------------------------------------------------------------- /lib/messageEvents.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'message-events'; 4 | 5 | /* 6 | * "Class" declaration, Message Events API exposes one function: 7 | * - search: retrieves list of message events according to given params 8 | */ 9 | module.exports = function(client) { 10 | return { 11 | /** 12 | * Search for message events using given parameters 13 | * 14 | * @param {Object} parameters 15 | * @param {RequestCb} [callback] 16 | * @returns {Promise} 17 | */ 18 | search: function(parameters, callback) { 19 | const options = { 20 | uri: api 21 | , qs: {} 22 | }; 23 | 24 | Object.keys(parameters).forEach(function(paramname) { 25 | if (Array.isArray(parameters[paramname])) { 26 | options.qs[paramname] = parameters[paramname].join(','); 27 | } else { 28 | options.qs[paramname] = parameters[paramname]; 29 | } 30 | }); 31 | return client.get(options, callback); 32 | } 33 | }; 34 | }; 35 | -------------------------------------------------------------------------------- /lib/recipientLists.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'recipient-lists'; 4 | 5 | module.exports = function(client) { 6 | return { 7 | /** 8 | * Get a list of all your recipient lists 9 | * https://developers.sparkpost.com/api/recipient-lists#recipient-lists-retrieve-get 10 | * 11 | * @param {RequestCb} [callback] 12 | * @return {Promise} 13 | */ 14 | list: function(callback) { 15 | const reqOpts = { 16 | uri: api 17 | }; 18 | return client.get(reqOpts, callback); 19 | }, 20 | 21 | /** 22 | * Get a list of all your recipient lists 23 | * https://developers.sparkpost.com/api/recipient-lists#recipient-lists-list-get 24 | * 25 | * @param {string} id - Unique ID of the list to return 26 | * @param {Object} options - Hash of request options 27 | * @param {RequestCb} [callback] 28 | * @return {Promise} 29 | */ 30 | get: function(id, options, callback) { 31 | options = options || {}; 32 | 33 | // Handle optional options argument 34 | if (typeof options === 'function') { 35 | callback = options; 36 | options = {}; 37 | } 38 | 39 | if (!id) { 40 | return client.reject(new Error('id is required'), callback); 41 | } 42 | 43 | const reqOpts = { 44 | uri: `${api}/${id}`, 45 | qs: options 46 | }; 47 | 48 | return client.get(reqOpts, callback); 49 | }, 50 | 51 | 52 | /** 53 | * Create a new recipient list 54 | * https://developers.sparkpost.com/api/recipient-lists#recipient-lists-create-post 55 | * 56 | * @param {Object} recipientList - recipient list object 57 | * @param {Array} recipientList.recipients - Array of recipient objects 58 | * @param {RequestCb} callback 59 | * @return {Promise} 60 | */ 61 | create: function(recipientList, callback) { 62 | if (!recipientList || typeof recipientList !== 'object' || !recipientList.recipients) { 63 | return client.reject(new Error('recipient list is required'), callback); 64 | } 65 | 66 | const reqOpts = { 67 | uri: api, 68 | json: recipientList, 69 | qs: { 70 | num_rcpt_errors: recipientList.num_rcpt_errors 71 | } 72 | }; 73 | 74 | return client.post(reqOpts, callback); 75 | }, 76 | 77 | /** 78 | * Update an existing list 79 | * https://developers.sparkpost.com/api/recipient-lists#recipient-lists-update-put 80 | * 81 | * @param {string} id - Unique ID of the list to be updated 82 | * @param {Object} recipientList - recipient list object 83 | * @param {Array} recipientList.recipients - Array of recipient objects 84 | * @param {RequestCb} callback 85 | * @return {Promise} 86 | * 87 | */ 88 | update: function(id, recipientList, callback) { 89 | if (!id) { 90 | return client.reject(new Error('recipient list id is required'), callback); 91 | } 92 | 93 | if (!recipientList || typeof recipientList === 'function') { 94 | return client.reject(new Error('recipient list is required'), callback); 95 | } 96 | 97 | const reqOpts = { 98 | uri: `${api}/${id}`, 99 | json: recipientList, 100 | qs: { 101 | num_rcpt_errors: recipientList.num_rcpt_errors 102 | } 103 | }; 104 | 105 | return client.put(reqOpts, callback); 106 | }, 107 | 108 | /** 109 | * Delete an existing recipient list 110 | * https://developers.sparkpost.com/api/recipient-lists#recipient-lists-delete-delete 111 | * 112 | * @param {string} id - ID of the list to be updated 113 | * @param {RequestCb} callback 114 | * @return {Promise} 115 | * 116 | */ 117 | delete: function(id, callback) { 118 | if (!id || typeof id !== 'string') { 119 | return client.reject(new Error('id is required'), callback); 120 | } 121 | 122 | const reqOpts = { 123 | uri: `${api}/${id}` 124 | }; 125 | 126 | return client.delete(reqOpts, callback); 127 | } 128 | }; 129 | 130 | }; 131 | -------------------------------------------------------------------------------- /lib/relayWebhooks.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'relay-webhooks'; 4 | 5 | module.exports = function(client) { 6 | return { 7 | /** 8 | * List all relay webhooks 9 | * 10 | * @param {RequestCb} [callback] 11 | * @returns {Promise} 12 | */ 13 | list: function(callback) { 14 | const reqOpts = { 15 | uri: api 16 | }; 17 | return client.get(reqOpts, callback); 18 | }, 19 | /** 20 | * Get details about a specified relay webhook by its id 21 | * 22 | * @param {string} id - the id of the relay webhook you want to look up 23 | * @param {RequestCb} [callback] 24 | * @returns {Promise} 25 | */ 26 | get: function(id, callback) { 27 | if (!id || typeof id !== 'string') { 28 | return client.reject(new Error('id is required'), callback); 29 | } 30 | 31 | const options = { 32 | uri: `${api}/${id}` 33 | }; 34 | 35 | return client.get(options, callback); 36 | }, 37 | /** 38 | * Create a new relay webhook 39 | * 40 | * @param {Object} webhook - an object of [relay webhook attributes]{https://developers.sparkpost.com/api/relay-webhooks#header-relay-webhooks-object-properties} 41 | * @param {RequestCb} [callback] 42 | * @returns {Promise} 43 | */ 44 | create: function(webhook, callback) { 45 | if (!webhook || typeof webhook !== 'object') { 46 | return client.reject(new Error('webhook object is required'), callback); 47 | } 48 | 49 | const reqOpts = { 50 | uri: api 51 | , json: webhook 52 | }; 53 | 54 | return client.post(reqOpts, callback); 55 | }, 56 | /** 57 | * Update an existing relay webhook 58 | * 59 | * @param {string} id - the id of the relay webhook you want to update 60 | * @param {Object} webhook - an object of [relay webhook attributes]{https://developers.sparkpost.com/api/relay-webhooks#header-relay-webhooks-object-properties} 61 | * @param {RequestCb} [callback] 62 | * @returns {Promise} 63 | */ 64 | update: function(id, webhook, callback) { 65 | if (!id || typeof id !== 'string') { 66 | return client.reject(new Error('id is required'), callback); 67 | } 68 | 69 | if (!webhook || typeof webhook !== 'object') { 70 | return client.reject(new Error('webhook object is required'), callback); 71 | } 72 | 73 | const reqOpts = { 74 | uri: `${api}/${id}` 75 | , json: webhook 76 | }; 77 | 78 | return client.put(reqOpts, callback); 79 | }, 80 | /** 81 | * Delete an existing relay webhook 82 | * 83 | * @param {string} id - the id of the relay webhook you want to delete 84 | * @param {RequestCb} [callback] 85 | * @returns {Promise} 86 | */ 87 | delete: function(id, callback) { 88 | if (!id || typeof id !== 'string') { 89 | return client.reject(new Error('id is required'), callback); 90 | } 91 | 92 | const options = { 93 | uri: `${api}/${id}` 94 | }; 95 | 96 | return client.delete(options, callback); 97 | } 98 | }; 99 | }; 100 | -------------------------------------------------------------------------------- /lib/sendingDomains.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'sending-domains'; 4 | 5 | module.exports = function(client) { 6 | return { 7 | /** 8 | * Lists all sending domains 9 | * 10 | * @param {RequestCb} [callback] 11 | * @return {Promise} 12 | */ 13 | list: function(callback) { 14 | const options = { 15 | uri: api 16 | }; 17 | 18 | return client.get(options, callback); 19 | }, 20 | 21 | /** 22 | * Get a single sending domain, by domain 23 | * 24 | * @param {string} domain - The domain name to get 25 | * @param {RequestCb} [callback] 26 | * @return {Promise} 27 | */ 28 | get: function(domain, callback) { 29 | if (!domain || typeof domain === 'function') { 30 | return client.reject(new Error('domain is required'), callback); 31 | } 32 | 33 | const options = { 34 | uri: `${api}/${domain}` 35 | }; 36 | 37 | return client.get(options, callback); 38 | }, 39 | 40 | /** 41 | * Creates a new sending domain 42 | * 43 | * @param {Object} createOpts - attributes used to create the new domain 44 | * @param {RequestCb} [callback] 45 | * @return {Promise} 46 | */ 47 | create: function(createOpts, callback) { 48 | if (!createOpts || typeof createOpts !== 'object') { 49 | return client.reject(new Error('create options are required'), callback); 50 | } 51 | 52 | const options = { 53 | uri: api, 54 | json: createOpts 55 | }; 56 | 57 | return client.post(options, callback); 58 | }, 59 | 60 | /** 61 | * Update an existing sending domain 62 | * 63 | * @param {string} domain - The domain to update 64 | * @param {Object} updateOpts - Hash of the sending domain attributes to update 65 | * @param {RequestCb} [callback] 66 | * @return {Promise} 67 | */ 68 | update: function(domain, updateOpts, callback) { 69 | if (typeof domain !== 'string') { 70 | return client.reject(new Error('domain is required'), callback); 71 | } 72 | 73 | if (!updateOpts || typeof updateOpts !== 'object') { 74 | return client.reject(new Error('update options are required'), callback); 75 | } 76 | 77 | const options = { 78 | uri: `${api}/${domain}`, 79 | json: updateOpts 80 | }; 81 | 82 | return client.put(options, callback); 83 | }, 84 | 85 | /** 86 | * Delete an existing sending domain 87 | * 88 | * @param {string} domain - The domain to delete 89 | * @param {RequestCb} [callback] 90 | * @return {Promise} 91 | */ 92 | delete: function(domain, callback) { 93 | if (typeof domain !== 'string') { 94 | return client.reject(new Error('domain is required'), callback); 95 | } 96 | 97 | const options = { 98 | uri: `${api}/${domain}` 99 | }; 100 | 101 | return client.delete(options, callback); 102 | }, 103 | 104 | /** 105 | * Verify an existing sending domain 106 | * 107 | * @param {string} domain - The domain to verify 108 | * @param {Object} options - Hash of options to include in verification request 109 | * @param {RequestCb} [callback] 110 | * @return {Promise} 111 | */ 112 | verify: function(domain, options, callback) { 113 | if (typeof domain !== 'string') { 114 | return client.reject(new Error('domain is required'), callback); 115 | } 116 | 117 | if (!options || typeof options !== 'object') { 118 | return client.reject(new Error('verification options are required'), callback); 119 | } 120 | 121 | const reqOpts = { 122 | uri: `${api}/${domain}/verify`, 123 | json: options 124 | }; 125 | 126 | return client.post(reqOpts, callback); 127 | } 128 | }; 129 | 130 | }; 131 | -------------------------------------------------------------------------------- /lib/sparkpost.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const version = require('../package.json').version; 4 | const url = require('url'); 5 | const withCallback = require('./withCallback'); 6 | const request = require('request'); 7 | const _ = require('lodash'); 8 | 9 | //REST API Config Defaults 10 | const defaults = { 11 | origin: 'https://api.sparkpost.com:443', 12 | apiVersion: 'v1', 13 | debug: false 14 | }; 15 | 16 | const resolveUri = function(origin, uri) { 17 | if (!/^http/.test(uri)) { 18 | uri = url.resolve(origin, uri); 19 | } 20 | return uri; 21 | }; 22 | 23 | const handleOptions = function(apiKey, options) { 24 | if (typeof apiKey === 'object') { 25 | options = apiKey; 26 | options.key = process.env.SPARKPOST_API_KEY; 27 | } else { 28 | options = options || {}; 29 | options.key = apiKey; 30 | } 31 | 32 | options.origin = options.origin || options.endpoint || defaults.origin; 33 | 34 | return options; 35 | }; 36 | 37 | const createSparkPostError = function(res, body) { 38 | const err = new Error(res.statusMessage); 39 | body = body || {}; 40 | err.name = 'SparkPostError'; 41 | err.errors = body.errors; 42 | err.statusCode = res.statusCode; 43 | 44 | return err; 45 | }; 46 | 47 | const createVersionStr = function(version, options) { 48 | let versionStr = `node-sparkpost/${version} node.js/${process.version}`; 49 | if (options.stackIdentity) { 50 | versionStr += `${options.stackIdentity} ${versionStr}`; 51 | } 52 | return versionStr; 53 | }; 54 | 55 | const SparkPost = function(apiKey, options) { 56 | 57 | options = handleOptions(apiKey, options); 58 | 59 | this.apiKey = options.key || process.env.SPARKPOST_API_KEY; 60 | 61 | if (typeof this.apiKey === 'undefined') { 62 | throw new Error('Client requires an API Key.'); 63 | } 64 | 65 | // adding version to object 66 | this.version = version; 67 | 68 | // setting up default headers 69 | this.defaultHeaders = _.merge({ 70 | 'User-Agent': createVersionStr(version, options) 71 | , 'Content-Type': 'application/json' 72 | }, options.headers); 73 | 74 | //Optional client config 75 | this.origin = options.origin; 76 | this.apiVersion = options.apiVersion || defaults.apiVersion; 77 | this.debug = (typeof options.debug === 'boolean') ? options.debug : defaults.debug; 78 | 79 | this.inboundDomains = require('./inboundDomains')(this); 80 | this.messageEvents = require('./messageEvents')(this); 81 | this.events = require('./events')(this); 82 | this.recipientLists = require('./recipientLists')(this); 83 | this.relayWebhooks = require('./relayWebhooks')(this); 84 | this.sendingDomains = require('./sendingDomains')(this); 85 | this.subaccounts = require('./subaccounts')(this); 86 | this.suppressionList = require('./suppressionList')(this); 87 | this.templates = require('./templates')(this); 88 | this.transmissions = require('./transmissions')(this); 89 | this.webhooks = require('./webhooks')(this); 90 | }; 91 | 92 | SparkPost.prototype.request = function(options, callback) { 93 | const baseUrl = `${this.origin}/api/${this.apiVersion}/`; 94 | 95 | // we need options 96 | if (!_.isPlainObject(options)) { 97 | throw new TypeError('options argument is required'); 98 | } 99 | 100 | // if we don't have a fully qualified URL let's make one 101 | options.uri = resolveUri(baseUrl, options.uri); 102 | 103 | // merge headers 104 | options.headers = _.merge({}, this.defaultHeaders, options.headers); 105 | 106 | // add Authorization with API Key 107 | options.headers.Authorization = this.apiKey; 108 | 109 | // set Strict SSL (Always true) 110 | options.strictSSL = true; 111 | 112 | // default to accepting gzipped responses 113 | if (typeof options.gzip === 'undefined') { 114 | options.gzip = true; 115 | } 116 | 117 | // set debug 118 | options.debug = (typeof options.debug === 'boolean') ? options.debug : this.debug; 119 | 120 | return withCallback(new Promise(function(resolve, reject) { 121 | request(options, function(err, res, body) { 122 | const invalidCodeRegex = /(5|4)[0-9]{2}/; 123 | let response; 124 | 125 | if (err) { 126 | reject(err); 127 | } else if (invalidCodeRegex.test(res.statusCode)) { 128 | err = createSparkPostError(res, body); 129 | reject(err); 130 | } else { 131 | response = body; 132 | if (options.debug) { 133 | response.debug = res; 134 | } 135 | resolve(response); 136 | } 137 | }); 138 | }), callback); 139 | }; 140 | 141 | SparkPost.prototype.get = function(options, callback) { 142 | options.method = 'GET'; 143 | options.json = true; 144 | 145 | return this.request(options, callback); 146 | }; 147 | 148 | SparkPost.prototype.post = function(options, callback) { 149 | options.method = 'POST'; 150 | 151 | return this.request(options, callback); 152 | }; 153 | 154 | SparkPost.prototype.put = function(options, callback) { 155 | options.method = 'PUT'; 156 | 157 | return this.request(options, callback); 158 | }; 159 | 160 | SparkPost.prototype.delete = function(options, callback) { 161 | options.method = 'DELETE'; 162 | 163 | return this.request(options, callback); 164 | }; 165 | 166 | SparkPost.prototype.reject = function(error, callback) { 167 | return withCallback(Promise.reject(error), callback); 168 | }; 169 | 170 | module.exports = SparkPost; 171 | 172 | /** 173 | * Standard error-first callback for HTTP requests 174 | 175 | * @callback RequestCb 176 | * @param {Error} err - Any error that occurred 177 | * @param {Object} [data] - API response body (or just the value of `body.results`, if it exists) 178 | */ 179 | -------------------------------------------------------------------------------- /lib/subaccounts.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'subaccounts'; 4 | 5 | module.exports = function(client) { 6 | const subaccounts = { 7 | /** 8 | * List a summary of all subaccounts 9 | * 10 | * @param {RequestCb} [callback] 11 | * @return {Promise} 12 | */ 13 | list: function(callback) { 14 | const options = { 15 | uri: api 16 | }; 17 | return client.get(options, callback); 18 | }, 19 | /** 20 | * Get details about a specified subaccount by its id 21 | * 22 | * @param {string} id - the id of the subaccount you want to look up 23 | * @param {RequestCb} [callback] 24 | * @returns {Promise} 25 | */ 26 | get: function(id, callback) { 27 | if (!id || typeof id !== 'string') { 28 | return client.reject(new Error('id is required'), callback); 29 | } 30 | 31 | const options = { 32 | uri: `${api}/${id}` 33 | }; 34 | return client.get(options, callback); 35 | }, 36 | /** 37 | * Create a new subaccount 38 | * 39 | * @param subaccount - an object of [subaccount attributes]{https://developers.sparkpost.com/api/subaccounts#header-request-body-attributes} 40 | * @param {RequestCb} [callback] 41 | * @returns {Promise} 42 | */ 43 | create: function(subaccount, callback) { 44 | if (!subaccount || typeof subaccount !== 'object') { 45 | return client.reject(new Error('subaccount object is required'), callback); 46 | } 47 | 48 | const reqOpts = { 49 | uri: api, 50 | json: subaccount 51 | }; 52 | return client.post(reqOpts, callback); 53 | }, 54 | /** 55 | * Update existing subaccount by id 56 | * 57 | * @param {string} id - the id of the subaccount you want to update 58 | * @param {Object} subaccount - an object of [subaccount attributes]{https://developers.sparkpost.com/api/subaccounts#header-request-body-attributes-1} 59 | * @param {RequestCb} callback 60 | * @returns {Promise} 61 | */ 62 | update: function(id, subaccount, callback) { 63 | if (!id || typeof id !== 'string') { 64 | return client.reject(new Error('id is required'), callback); 65 | } 66 | 67 | if (!subaccount || typeof subaccount !== 'object') { 68 | return client.reject(new Error('subaccount object is required'), callback); 69 | } 70 | 71 | const reqOpts = { 72 | uri: `${api}/${id}`, 73 | json: subaccount 74 | }; 75 | 76 | return client.put(reqOpts, callback); 77 | } 78 | }; 79 | 80 | return subaccounts; 81 | }; 82 | -------------------------------------------------------------------------------- /lib/suppressionList.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'suppression-list'; 4 | 5 | module.exports = function(client) { 6 | 7 | return { 8 | /** 9 | * Lists all entries in your suppression list, 10 | * filtered by an optional set of parameters 11 | * 12 | * @param {Object} [parameters] - Hash of parameters to filter results 13 | * @param {RequestCb} [callback] 14 | * @return {Promise} 15 | */ 16 | list: function(parameters, callback) { 17 | const options = { 18 | uri: api 19 | , qs: parameters 20 | }; 21 | return client.get(options, callback); 22 | }, 23 | 24 | /** 25 | * Gets a single entry by email address ID 26 | * 27 | * @param {String} email 28 | * @param {RequestCb} [callback] 29 | * @return {Promise} 30 | */ 31 | get: function(email, callback) { 32 | if (!email || typeof email === 'function') { 33 | return client.reject(new Error('email is required'), callback); 34 | } 35 | 36 | const options = { 37 | uri: `${api}/${email}` 38 | }; 39 | return client.get(options, callback); 40 | }, 41 | 42 | /** 43 | * Updates existing entries, or creates entries 44 | * if they don't exist for that email address ID 45 | * 46 | * @param {Array|Object} listEntries - List of suppression entry objects to upsert 47 | * @param {RequestCb} [callback] 48 | * @return {Promise} 49 | */ 50 | upsert: function(listEntries, callback) { 51 | if (!listEntries || typeof listEntries === 'function') { 52 | return client.reject(new Error('list entries is required'), callback); 53 | } 54 | 55 | if (!Array.isArray(listEntries)) { 56 | listEntries = [listEntries]; 57 | } 58 | 59 | const options = { 60 | uri: api, 61 | json: { recipients: listEntries } 62 | }; 63 | 64 | return client.put(options, callback); 65 | }, 66 | 67 | /** 68 | * Deletes a single entry, by email address ID 69 | * 70 | * @param {String} email 71 | * @param {RequestCb} [callback] 72 | * @return {Promise} 73 | */ 74 | delete: function(email, callback) { 75 | if (!email || typeof email === 'function') { 76 | return client.reject(new Error('email is required'), callback); 77 | } 78 | 79 | const options = { 80 | uri: `${api}/${email}` 81 | }; 82 | return client.delete(options, callback); 83 | } 84 | }; 85 | 86 | }; 87 | -------------------------------------------------------------------------------- /lib/templates.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'templates'; 4 | 5 | const _ = require('lodash'); 6 | 7 | module.exports = function(client) { 8 | return { 9 | /** 10 | * List an overview of all templates. 11 | * 12 | * @param {RequestCb} [callback] 13 | * @returns {Promise} 14 | */ 15 | list: function(callback) { 16 | const options = { 17 | uri: api 18 | }; 19 | return client.get(options, callback); 20 | }, 21 | /** 22 | * Get details about a specified template by its id. 23 | * 24 | * @param {string} id 25 | * @param {Object} options 26 | * @param {RequestCb} [callback] 27 | * @returns {Promise} 28 | */ 29 | get: function(id, options, callback) { 30 | options = options || {}; 31 | 32 | if (typeof options === 'function') { 33 | callback = options; 34 | options = {}; 35 | } 36 | 37 | if (!id) { 38 | return client.reject(new Error('template id is required'), callback); 39 | } 40 | 41 | const reqOpts = { 42 | uri: `${api}/${id}` 43 | , qs: options 44 | }; 45 | 46 | return client.get(reqOpts, callback); 47 | }, 48 | /** 49 | * Create a new template. 50 | * 51 | * @param {Object} template 52 | * @param {RequestCb} [callback] 53 | * @returns {Promise} 54 | */ 55 | create: function(template, callback) { 56 | if (!template || typeof template !== 'object') { 57 | return client.reject(new Error('template object is required'), callback); 58 | } 59 | 60 | const reqOpts = { 61 | uri: api 62 | , json: template 63 | }; 64 | 65 | return client.post(reqOpts, callback); 66 | }, 67 | /** 68 | * Update an existing template. 69 | * 70 | * @param {String} id 71 | * @param {Object} template 72 | * @param {Object} options 73 | * @param {RequestCb} callback 74 | * @returns {Promise} 75 | */ 76 | update: function(id, template, options, callback) { 77 | // Handle optional options argument 78 | if (typeof options === 'function') { 79 | callback = options; 80 | options = {}; 81 | } 82 | 83 | if (!id) { 84 | return client.reject(new Error('template id is required'), callback); 85 | } 86 | 87 | if (!template || typeof template !== 'object') { 88 | return client.reject(new Error('template object is required'), callback); 89 | } 90 | 91 | const reqOpts = { 92 | uri: `${api}/${id}` 93 | , json: template 94 | , qs: options 95 | }; 96 | 97 | return client.put(reqOpts, callback); 98 | }, 99 | /** 100 | * Delete an existing template. 101 | * 102 | * @param {String} id 103 | * @param {RequestCb} [callback] 104 | * @returns {Promise} 105 | */ 106 | delete: function(id, callback) { 107 | if (!id || typeof id !== 'string') { 108 | return client.reject(new Error('template id is required'), callback); 109 | } 110 | 111 | const options = { 112 | uri: `${api}/${id}` 113 | }; 114 | return client.delete(options, callback); 115 | }, 116 | /** 117 | * Preview the most recent version of an existing template by id. 118 | * 119 | * @param {String} id 120 | * @param {Object} options 121 | * @param {RequestCb} [callback] 122 | * @returns {Promise} 123 | */ 124 | preview: function(id, options, callback) { 125 | options = options || {}; 126 | 127 | // Handle optional options argument 128 | if (typeof options === 'function') { 129 | callback = options; 130 | options = {}; 131 | } 132 | 133 | if (!id) { 134 | return client.reject(new Error('template id is required'), callback); 135 | } 136 | 137 | const reqOpts = { 138 | uri: `${api}/${id}/preview` 139 | , json: _.cloneDeep(options) 140 | , qs: {} 141 | }; 142 | 143 | // Add draft to query params 144 | if (reqOpts.json.hasOwnProperty('draft')) { 145 | reqOpts.qs.draft = reqOpts.json.draft; 146 | delete reqOpts.json.draft; 147 | } 148 | 149 | return client.post(reqOpts, callback); 150 | } 151 | }; 152 | }; 153 | -------------------------------------------------------------------------------- /lib/transmissions.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const _ = require('lodash'); 4 | const api = 'transmissions'; 5 | 6 | /* 7 | * "Class" declaration, Transmissions exposes three functions, one for sending a transmission, 8 | * another for getting a list of transmissions that have been sent, and another for getting 9 | * info about a specific transmission 10 | */ 11 | module.exports = function(client) { 12 | return { 13 | /** 14 | * List an overview of all transmissions in the account 15 | * 16 | * @param {Object} options 17 | * @param {RequestCb} [callback] 18 | * @returns {Promise} 19 | */ 20 | list: function(options, callback) { 21 | // Handle optional options argument 22 | if (typeof options === 'function') { 23 | callback = options; 24 | options = {}; 25 | } 26 | 27 | const reqOpts = { 28 | uri: api, 29 | qs: options 30 | }; 31 | 32 | return client.get(reqOpts, callback); 33 | }, 34 | /** 35 | * Retrieve the details about a transmission by its id 36 | * 37 | * @param {String} id 38 | * @param {RequestCb} [callback] 39 | * @returns {Promise} 40 | */ 41 | get: function(id, callback) { 42 | if (typeof id !== 'string') { 43 | return client.reject(new Error('id is required'), callback); 44 | } 45 | 46 | const options = { 47 | uri: `${api}/${id}` 48 | }; 49 | 50 | return client.get(options, callback); 51 | }, 52 | /** 53 | * Sends a message by creating a new transmission 54 | * 55 | * @param {Object} transmission 56 | * @param {Object} options 57 | * @param {RequestCb} [callback] 58 | * @returns {Promise} 59 | */ 60 | send: function(transmission, options, callback) { 61 | // Handle optional options argument 62 | if (typeof options === 'function') { 63 | callback = options; 64 | options = {}; 65 | } 66 | 67 | if (!transmission || typeof transmission !== 'object') { 68 | return client.reject(new Error('transmission object is required'), callback); 69 | } 70 | 71 | transmission = formatPayload(transmission); 72 | 73 | const reqOpts = { 74 | uri: api, 75 | json: transmission, 76 | qs: options 77 | }; 78 | 79 | return client.post(reqOpts, callback); 80 | } 81 | }; 82 | 83 | }; 84 | 85 | function formatPayload(originalTransmission) { 86 | const transmission = _.cloneDeep(originalTransmission); 87 | 88 | // don't format the payload if we are not given an array of recipients 89 | if (!_.isArray(transmission.recipients)) { 90 | return transmission; 91 | } 92 | 93 | // format all the original recipients to be in the object format 94 | transmission.recipients = _.map(transmission.recipients, (recipient) => { 95 | recipient.address = addressToObject(recipient.address); 96 | 97 | return recipient; 98 | }); 99 | 100 | // add the CC headers 101 | if (_.isArray(transmission.cc) && transmission.cc.length > 0) { 102 | _.set(transmission, 'content.headers.CC', generateCCHeader(transmission)); 103 | } 104 | 105 | const headerTo = generateHeaderTo(transmission.recipients); 106 | 107 | transmission.recipients = addListToRecipients(transmission, 'cc', headerTo); 108 | transmission.recipients = addListToRecipients(transmission, 'bcc', headerTo); 109 | 110 | delete transmission.cc; 111 | delete transmission.bcc; 112 | 113 | return transmission; 114 | } 115 | 116 | function addListToRecipients(transmission, listName, headerTo) { 117 | if (!_.isArray(transmission[listName])) { 118 | return transmission.recipients; 119 | } 120 | 121 | return transmission.recipients.concat(_.map(transmission[listName], (recipient) => { 122 | recipient.address = addressToObject(recipient.address); 123 | 124 | recipient.address.header_to = headerTo; 125 | 126 | // remove name from address - name is only put in the header for cc and not at all for bcc 127 | if (_.has(recipient.address, 'name')) { 128 | delete recipient.address.name; 129 | } 130 | 131 | return recipient; 132 | })); 133 | } 134 | 135 | function generateCCHeader(transmission) { 136 | return _.map(transmission.cc, (ccRecipient) => addressToString(ccRecipient.address)).join(', '); 137 | } 138 | 139 | function generateHeaderTo(recipients) { 140 | // if a recipient has a header_to then it is cc'd or bcc'd and we don't want it in the header_to value 141 | const originalRecipients = _.filter(recipients, (recipient) => !_.has(recipient.address, 'header_to')); 142 | 143 | return _.map(originalRecipients, (recipient) => addressToString(recipient.address)).join(', '); 144 | } 145 | 146 | function addressToString(address) { 147 | if (_.isPlainObject(address)) { 148 | if (_.has(address, 'name')) { 149 | address = `"${address.name}" <${address.email}>`; 150 | } else { 151 | address = address.email; 152 | } 153 | } 154 | 155 | return address; 156 | } 157 | 158 | function addressToObject(address) { 159 | let addressObject = address; 160 | 161 | if (_.isString(address)) { 162 | addressObject = {}; 163 | 164 | const matches = /"?(.[^"]*)?"?\s*<(.+)>/gi.exec(address); 165 | 166 | if (matches) { 167 | addressObject.name = matches[1]; 168 | addressObject.email = matches[2]; 169 | } else { 170 | addressObject.email = address; 171 | } 172 | } 173 | 174 | return addressObject; 175 | } 176 | -------------------------------------------------------------------------------- /lib/webhooks.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const api = 'webhooks'; 4 | 5 | module.exports = function(client) { 6 | return { 7 | 8 | /** 9 | * Lists all webhooks 10 | * 11 | * @param {Object} [options] - Hash of options 12 | * @param {string} [options.timezone] - The timezone to use for the last_successful and last_failure properties. 13 | * @param {RequestCb} [callback] 14 | * @returns {Promise} 15 | */ 16 | list: function(options, callback) { 17 | const reqOpts = { 18 | uri: api, 19 | qs: {} 20 | }; 21 | 22 | if (!options || typeof options === 'function') { 23 | callback = options; 24 | options = {}; 25 | } 26 | 27 | if (options.timezone) { 28 | reqOpts.qs.timezone = options.timezone; 29 | } 30 | 31 | return client.get(reqOpts, callback); 32 | }, 33 | 34 | /** 35 | * Get a single webhook by ID 36 | * 37 | * @param {string} id - The ID of the webhook to get 38 | * @param {Object} [options] - Hash of options 39 | * @param {string} [options.timezone] - The timezone to use for the last_successful and last_failure properties. 40 | * @param {RequestCb} [callback] 41 | * @returns {Promise} 42 | */ 43 | get: function(id, options, callback) { 44 | if (!options || typeof options === 'function') { 45 | callback = options; 46 | options = {}; 47 | } 48 | 49 | if (typeof id !== 'string') { 50 | return client.reject(new Error('id is required'), callback); 51 | } 52 | 53 | const reqOpts = { 54 | uri: `${api}/${id}`, 55 | qs: {} 56 | }; 57 | 58 | if (options.timezone) { 59 | reqOpts.qs.timezone = options.timezone; 60 | } 61 | 62 | return client.get(reqOpts, callback); 63 | }, 64 | 65 | /** 66 | * Creates a new webhook 67 | * 68 | * @param {Object} webhook - attributes used to create the new webhook 69 | * @param {RequestCb} [callback] 70 | * @returns {Promise} 71 | */ 72 | create: function(webhook, callback) { 73 | if (!webhook || typeof webhook === 'function') { 74 | return client.reject(new Error('webhook object is required'), callback); 75 | } 76 | 77 | const options = { 78 | uri: api, 79 | json: webhook 80 | }; 81 | 82 | return client.post(options, callback); 83 | }, 84 | 85 | /** 86 | * Update an existing webhook 87 | * 88 | * @param {string} id - The ID of the webhook to update 89 | * @param {Object} webhook - Hash of the webhook attributes to update 90 | * @param {RequestCb} [callback] 91 | * @returns {Promise} 92 | */ 93 | update: function(id, webhook, callback) { 94 | if (!id) { 95 | return client.reject(new Error('id is required'), callback); 96 | } 97 | 98 | if (!webhook || typeof webhook === 'function') { 99 | return client.reject(new Error('webhook object is required'), callback); 100 | } 101 | 102 | const options = { 103 | uri: `${api}/${id}`, 104 | json: webhook 105 | }; 106 | 107 | delete options.json.id; 108 | 109 | return client.put(options, callback); 110 | }, 111 | 112 | /** 113 | * Delete an existing webhook 114 | * 115 | * @param {string} id - The ID of the webhook to delete 116 | * @param {RequestCb} [callback] 117 | * @returns {Promise} 118 | */ 119 | delete: function(id, callback) { 120 | if (!id || typeof id === 'function') { 121 | return client.reject(new Error('id is required'), callback); 122 | } 123 | 124 | const options = { 125 | uri: `${api}/${id}` 126 | }; 127 | 128 | return client.delete(options, callback); 129 | }, 130 | 131 | /** 132 | * Sends an example message event batch from the Webhook API to the target URL. 133 | * 134 | * @param {string} id - The ID of the webhook to validate 135 | * @param {Object} options - Hash of options used to validate the webhook 136 | * @param {string} options.message - The message (payload) to send to the webhook consumer. 137 | * @param {RequestCb} [callback] 138 | * @returns {Promise} 139 | */ 140 | validate: function(id, options, callback) { 141 | if (typeof id !== 'string') { 142 | return client.reject(new Error('id is required'), callback); 143 | } 144 | 145 | if (!options || typeof options === 'function' || !options.message) { 146 | return client.reject(new Error('message is required'), callback); 147 | } 148 | 149 | const reqOpts = { 150 | uri: `${api}/${id}/validate`, 151 | json: { 152 | message: options.message 153 | } 154 | }; 155 | 156 | return client.post(reqOpts, callback); 157 | }, 158 | 159 | /** 160 | * Gets recent status information about a webhook. 161 | * 162 | * @param {string} id - The ID of the webhook to check 163 | * @param {Object} [options] - Hash of options 164 | * @param {string} [options.limit] - The maximum number of results to return. 165 | * @param {RequestCb} [callback] 166 | * @returns {Promise} 167 | */ 168 | getBatchStatus: function(id, options, callback) { 169 | if (!options || typeof options === 'function') { 170 | callback = options; 171 | options = {}; 172 | } 173 | 174 | if (typeof id !== 'string') { 175 | return client.reject(new Error('id is required'), callback); 176 | } 177 | 178 | const reqOpts = { 179 | uri: `${api}/${id}/batch-status`, 180 | qs: {} 181 | }; 182 | 183 | if (options.limit) { 184 | reqOpts.qs.limit = options.limit; 185 | } 186 | 187 | return client.get(reqOpts, callback); 188 | }, 189 | 190 | /** 191 | * Lists descriptions of the events, event types, and event fields that could be included in a Webhooks post to your target URL. 192 | * 193 | * @param {RequestCb} [callback] 194 | * @returns {Promise} 195 | */ 196 | getDocumentation: function(callback) { 197 | const reqOpts = { 198 | uri: `${api}/events/documentation` 199 | }; 200 | return client.get(reqOpts, callback); 201 | }, 202 | 203 | /** 204 | * Lists examples of the event data that will be posted to a webhook consumer. 205 | * 206 | * @param {Object} [options] - Hash of options 207 | * @param {string} [options.events] - A comma delimited list of events to get samples of. 208 | * @param {RequestCb} [callback] 209 | * @returns {Promise} 210 | */ 211 | getSamples: function(options, callback) { 212 | const reqOpts = { 213 | uri: `${api}/events/samples`, 214 | qs: {} 215 | }; 216 | 217 | if (!options || typeof options === 'function') { 218 | callback = options; 219 | options = {}; 220 | } 221 | 222 | if (options.events) { 223 | reqOpts.qs.events = options.events; 224 | } 225 | 226 | return client.get(reqOpts, callback); 227 | } 228 | }; 229 | }; 230 | -------------------------------------------------------------------------------- /lib/withCallback.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * withCallback handles an optional nodeback 5 | * and return the promise object 6 | * 7 | * @param promise {Promise} 8 | * @param cb {Function} 9 | * @return Promise 10 | * 11 | * @example 12 | * function eitherOr(options, callback) { 13 | * return withCallback(promiseThingy(options), callback); 14 | * } 15 | */ 16 | 17 | function withCallback(promise, cb) { 18 | if (typeof cb !== 'function') { 19 | cb = noop; 20 | } 21 | 22 | promise.then((result) => { 23 | cb(null, result); 24 | }).catch((err) => { 25 | cb(err); 26 | }); 27 | 28 | return promise; 29 | } 30 | 31 | function noop() {} 32 | 33 | module.exports = withCallback; 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sparkpost", 3 | "version": "2.1.4", 4 | "description": "A Node.js wrapper for interfacing with your favorite SparkPost APIs", 5 | "main": "./lib/sparkpost.js", 6 | "scripts": { 7 | "coveralls": "cat ./test/reports/lcov.info | coveralls", 8 | "pretest": "eslint lib/**", 9 | "test": "istanbul cover --report lcov --dir test/reports/ _mocha --recursive ./test/spec --grep ./test/**/*.spec.js -- --colors --reporter spec", 10 | "postversion": "git push upstream && git push --tags upstream" 11 | }, 12 | "keywords": [ 13 | "email", 14 | "messaging" 15 | ], 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/SparkPost/node-sparkpost" 19 | }, 20 | "author": "Message Systems, Inc.", 21 | "license": "Apache-2.0", 22 | "bugs": { 23 | "url": "https://github.com/SparkPost/node-sparkpost/issues" 24 | }, 25 | "homepage": "https://github.com/SparkPost/node-sparkpost", 26 | "engines": { 27 | "node": ">=4" 28 | }, 29 | "devDependencies": { 30 | "chai": "^3.5.0", 31 | "chai-as-promised": "^6.0.0", 32 | "coveralls": "^3.0.2", 33 | "eslint": "^3.12.2", 34 | "eslint-config-sparkpost": "1.3.1", 35 | "istanbul": "^0.4.5", 36 | "mocha": "^5.2.0", 37 | "nock": "^9.0.0", 38 | "sinon": "^1.17.5", 39 | "sinon-as-promised": "^4.0.2", 40 | "sinon-chai": "^2.8.0" 41 | }, 42 | "dependencies": { 43 | "lodash": "^4.17.2", 44 | "request": "^2.79.0" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --recursive 2 | --reporter spec 3 | --timeout 20000 4 | -------------------------------------------------------------------------------- /test/spec/events.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai') 4 | , expect = chai.expect 5 | , sinon = require('sinon'); 6 | 7 | require('sinon-as-promised'); 8 | 9 | chai.use(require('sinon-chai')); 10 | chai.use(require('chai-as-promised')); 11 | 12 | describe('Events Library', function() { 13 | let client, events, callback; 14 | 15 | beforeEach(function() { 16 | client = { 17 | get: sinon.stub().resolves({}) 18 | }; 19 | 20 | callback = function() {}; 21 | 22 | events = require('../../lib/events')(client); 23 | }); 24 | 25 | describe('Message Tests', function(){ 26 | describe('search Method', function() { 27 | it('should call client get method with the appropriate parameters', function() { 28 | var options = { 29 | bounce_classes: '10,50', 30 | campaign_ids: 'test_campaign', 31 | events: 'bounce', 32 | event_ids: '1234, 5678', 33 | friendly_froms: 'bob@example.com', 34 | from: '2015-11-14T16:15', 35 | message_ids: '0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e', 36 | page: 1, 37 | per_page: 5, 38 | reason: '%5.2.0%', 39 | recipients: 'jim@example.com', 40 | template_ids: 'newsletter_template', 41 | timezone: 'America/New_York', 42 | to: '2016-11-14T16:15', 43 | transmission_ids: '65832150921904138' 44 | }; 45 | return events.searchMessage(options, callback) 46 | .then(function() { 47 | Object.keys(options).forEach(function(key) { 48 | expect(client.get.firstCall.args[0].qs).to.have.property(key).and.equal(options[key]); 49 | }); 50 | expect(client.get.firstCall.args[1]).to.equal(callback); 51 | }); 52 | }); 53 | 54 | it('should accept arrays as parameters where appropriate', function() { 55 | var arroptions = { 56 | bounce_classes: [10,50], 57 | campaign_ids: ['campaign1', 'campaignTwo'], 58 | events: ['bounce', 'out_of_band'], 59 | friendly_froms: ['bob@example.com', 'jim@example.com'], 60 | message_ids: ['0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e', '338ac622-4321-5678-0123456789'], 61 | recipients: ['jim@example.com', 'bob@example.com'], 62 | template_ids: ['newsletter_template', 'newsflash_template'], 63 | transmission_ids: ['65832150921904138', '54673829032039839'], 64 | page: 1, 65 | per_page: 5, 66 | timezone: 'America/New_York' 67 | }; 68 | return events.searchMessage(arroptions) 69 | .then(function() { 70 | Object.keys(arroptions).forEach(function(key) { 71 | var opt = arroptions[key] 72 | , firstCallQS = client.get.firstCall.args[0].qs; 73 | if (Array.isArray(opt)) { 74 | expect(firstCallQS).to.have.property(key).and.equal(opt.toString()); 75 | } 76 | }); 77 | }); 78 | }); 79 | }); 80 | }); 81 | 82 | }); 83 | -------------------------------------------------------------------------------- /test/spec/inboundDomains.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai') 4 | , expect = chai.expect 5 | , SparkPost = require('../../lib/sparkpost') 6 | , sinon = require('sinon'); 7 | 8 | require('sinon-as-promised'); 9 | 10 | chai.use(require('sinon-chai')); 11 | chai.use(require('chai-as-promised')); 12 | 13 | describe('Inbound Domains Library', function() { 14 | let client, inboundDomains, callback; 15 | 16 | beforeEach(function() { 17 | client = { 18 | get: sinon.stub().resolves({}), 19 | post: sinon.stub().resolves({}), 20 | delete: sinon.stub().resolves({}), 21 | reject: SparkPost.prototype.reject 22 | }; 23 | 24 | callback = function() {}; 25 | 26 | inboundDomains = require('../../lib/inboundDomains')(client); 27 | }); 28 | 29 | describe('list Method', function() { 30 | it('should call client get method with the appropriate uri', function() { 31 | 32 | return inboundDomains.list(callback) 33 | .then(function() { 34 | expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'inbound-domains'}); 35 | expect(client.get.firstCall.args[1]).to.equal(callback); 36 | }); 37 | }); 38 | }); 39 | 40 | describe('get Method', function() { 41 | it('should call client get method with the appropriate uri', function() { 42 | return inboundDomains.get('test', callback) 43 | .then(function() { 44 | expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'inbound-domains/test'}); 45 | expect(client.get.firstCall.args[1]).to.equal(callback); 46 | }); 47 | }); 48 | 49 | 50 | it('should throw an error if domain is missing', function() { 51 | return expect(inboundDomains.get()).to.be.rejectedWith('domain is required'); 52 | }); 53 | }); 54 | 55 | describe('create Method', function() { 56 | it('should call client post method with the appropriate uri and payload', function() { 57 | let createOpts = {domain: 'test'}; 58 | return inboundDomains.create(createOpts, callback) 59 | .then(function() { 60 | expect(client.post.firstCall.args[0].uri).to.equal('inbound-domains'); 61 | expect(client.post.firstCall.args[0].json).to.deep.equal(createOpts); 62 | expect(client.post.firstCall.args[1]).to.equal(callback); 63 | }); 64 | }); 65 | 66 | it('should throw an error if domain is missing', function() { 67 | return expect(inboundDomains.create()).to.be.rejectedWith('create options are required'); 68 | }); 69 | }); 70 | 71 | describe('delete Method', function() { 72 | it('should call client delete method with the appropriate uri', function() { 73 | return inboundDomains.delete('test', callback) 74 | .then(function () { 75 | expect(client.delete.firstCall.args[0].uri).to.equal('inbound-domains/test'); 76 | expect(client.delete.firstCall.args[1]).to.equal(callback); 77 | }); 78 | }); 79 | 80 | it('should throw an error if domain is missing', function() { 81 | return expect(inboundDomains.delete()).to.be.rejectedWith('domain is required'); 82 | }); 83 | }); 84 | }); 85 | -------------------------------------------------------------------------------- /test/spec/messageEvents.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai') 4 | , expect = chai.expect 5 | , sinon = require('sinon'); 6 | 7 | require('sinon-as-promised'); 8 | 9 | chai.use(require('sinon-chai')); 10 | chai.use(require('chai-as-promised')); 11 | 12 | describe('Message Events Library', function() { 13 | let client, messageEvents, callback; 14 | 15 | beforeEach(function() { 16 | client = { 17 | get: sinon.stub().resolves({}) 18 | }; 19 | 20 | callback = function() {}; 21 | 22 | messageEvents = require('../../lib/messageEvents')(client); 23 | }); 24 | 25 | describe('search Method', function() { 26 | it('should call client get method with the appropriate parameters', function() { 27 | var options = { 28 | bounce_classes: '10,50', 29 | campaign_ids: 'test_campaign', 30 | events: 'bounce', 31 | friendly_froms: 'bob@example.com', 32 | from: '2015-11-14T16:15', 33 | message_ids: '0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e', 34 | page: 1, 35 | per_page: 5, 36 | reason: '%5.2.0%', 37 | recipients: 'jim@example.com', 38 | template_ids: 'newsletter_template', 39 | timezone: 'America/New_York', 40 | to: '2016-11-14T16:15', 41 | transmission_ids: '65832150921904138' 42 | }; 43 | return messageEvents.search(options, callback) 44 | .then(function() { 45 | Object.keys(options).forEach(function(key) { 46 | expect(client.get.firstCall.args[0].qs).to.have.property(key).and.equal(options[key]); 47 | }); 48 | expect(client.get.firstCall.args[1]).to.equal(callback); 49 | }); 50 | }); 51 | 52 | it('should accept arrays as parameters where appropriate', function() { 53 | var arroptions = { 54 | bounce_classes: [10,50], 55 | campaign_ids: ['campaign1', 'campaignTwo'], 56 | events: ['bounce', 'out_of_band'], 57 | friendly_froms: ['bob@example.com', 'jim@example.com'], 58 | message_ids: ['0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e', '338ac622-4321-5678-0123456789'], 59 | recipients: ['jim@example.com', 'bob@example.com'], 60 | template_ids: ['newsletter_template', 'newsflash_template'], 61 | transmission_ids: ['65832150921904138', '54673829032039839'], 62 | page: 1, 63 | per_page: 5, 64 | timezone: 'America/New_York' 65 | }; 66 | return messageEvents.search(arroptions) 67 | .then(function() { 68 | Object.keys(arroptions).forEach(function(key) { 69 | var opt = arroptions[key] 70 | , firstCallQS = client.get.firstCall.args[0].qs; 71 | if (Array.isArray(opt)) { 72 | expect(firstCallQS).to.have.property(key).and.equal(opt.toString()); 73 | } 74 | }); 75 | }); 76 | }); 77 | }); 78 | }); 79 | -------------------------------------------------------------------------------- /test/spec/recipientLists.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var _ = require('lodash') 4 | , chai = require('chai') 5 | , expect = chai.expect 6 | , SparkPost = require('../../lib/sparkpost') 7 | , sinon = require('sinon'); 8 | 9 | require('sinon-as-promised'); 10 | 11 | chai.use(require('sinon-chai')); 12 | chai.use(require('chai-as-promised')); 13 | 14 | describe('Recipient Lists Library', function() { 15 | var client, recipientLists, callback; 16 | 17 | beforeEach(function() { 18 | client = { 19 | get: sinon.stub().resolves({}), 20 | post: sinon.stub().resolves({}), 21 | put: sinon.stub().resolves({}), 22 | delete: sinon.stub().resolves({}), 23 | reject: SparkPost.prototype.reject 24 | }; 25 | 26 | callback = function() {}; 27 | 28 | recipientLists = require('../../lib/recipientLists')(client); 29 | }); 30 | 31 | describe('list', function() { 32 | 33 | it('should call client get method with the appropriate uri', function() { 34 | return recipientLists.list(callback) 35 | .then(function() { 36 | expect(client.get.firstCall.args[0].uri).to.equal('recipient-lists'); 37 | expect(client.get.firstCall.args[1]).to.equal(callback); 38 | }); 39 | }); 40 | }); 41 | 42 | describe('get', function() { 43 | 44 | it('should call client get method with the appropriate uri', function() { 45 | return recipientLists.get('test-id', callback) 46 | .then(function() { 47 | expect(client.get.firstCall.args[0].uri).to.equal('recipient-lists/test-id'); 48 | expect(client.get.firstCall.args[1]).to.equal(callback); 49 | }); 50 | }); 51 | 52 | it('should throw an error if id is missing', function() { 53 | return expect(recipientLists.get()).to.be.rejectedWith('id is required'); 54 | }); 55 | 56 | it('should not throw an error if optional 2nd argument is a function (callback)', function() { 57 | let cb = sinon.stub(); 58 | 59 | client.get.yields(); 60 | 61 | return recipientLists.get('test-id', cb).then(() => { 62 | expect(cb.callCount).to.equal(1); 63 | }); 64 | }); 65 | 66 | it('should allow show_recipients to be set in options', function() { 67 | var options = { 68 | show_recipients: true 69 | }; 70 | 71 | return recipientLists.get('test-id', options) 72 | .then(function() { 73 | expect(client.get.firstCall.args[0].qs).to.deep.equal({show_recipients: true}); 74 | }); 75 | }); 76 | 77 | }); 78 | 79 | describe('create', function() { 80 | 81 | it('should call client post method with the appropriate uri and payload', function() { 82 | let testList = { 83 | id: 'test_list', 84 | recipients: [ 85 | { 86 | address: { 87 | email: 'test@test.com', 88 | name: 'test' 89 | } 90 | } 91 | ] 92 | }; 93 | 94 | return recipientLists.create(testList, callback) 95 | .then(function() { 96 | expect(client.post.firstCall.args[0].uri).to.equal('recipient-lists'); 97 | expect(client.post.firstCall.args[0].json).to.deep.equal(testList); 98 | expect(client.post.firstCall.args[1]).to.equal(callback); 99 | }); 100 | }); 101 | 102 | it('should throw an error if no recipients are provided', function() { 103 | return Promise.all([ 104 | expect(recipientLists.create(), 'no recipient list hash at all').to.be.rejectedWith('recipient list is required'), 105 | expect(recipientLists.create({}), 'no recipients key').to.be.rejectedWith('recipient list is required'), 106 | expect(recipientLists.create(function() {}), 'recipient list is actually a callback').to.be.rejectedWith('recipient list is required') 107 | ]); 108 | }); 109 | 110 | it('should allow num_rcpt_errors to be set in options', function() { 111 | var testList = { 112 | id: 'test_list', 113 | recipients: [ 114 | { 115 | address: { 116 | email: 'test@test.com', 117 | name: 'test' 118 | } 119 | } 120 | ], 121 | num_rcpt_errors: 3 122 | }; 123 | 124 | return recipientLists.create(testList) 125 | .then(function() { 126 | expect(client.post.firstCall.args[0].qs).to.deep.equal({num_rcpt_errors: 3}); 127 | }); 128 | }); 129 | 130 | }); 131 | 132 | describe('update', function() { 133 | 134 | it('should call client put method with the appropriate uri and payload', function() { 135 | const testList = { 136 | recipients: [ 137 | { 138 | address: { 139 | email: 'test@test.com', 140 | name: 'test' 141 | } 142 | } 143 | ] 144 | }; 145 | const testId = 'test-id'; 146 | 147 | return recipientLists.update(testId, testList, callback) 148 | .then(function() { 149 | expect(client.put.firstCall.args[0].uri).to.equal('recipient-lists/' + testId); 150 | expect(client.put.firstCall.args[0].json).to.deep.equal(testList); 151 | expect(client.put.firstCall.args[1]).to.equal(callback); 152 | }); 153 | }); 154 | 155 | it('should throw an error if recipient list is missing', function() { 156 | return expect(recipientLists.update('test-id')).to.be.rejectedWith('recipient list is required'); 157 | }); 158 | 159 | it('should throw an error if id is missing', function() { 160 | return expect(recipientLists.update()).to.be.rejectedWith('recipient list id is required'); 161 | }); 162 | 163 | it('should allow num_rcpt_errors to be set in options', function() { 164 | var testList = { 165 | recipients: [ 166 | { 167 | address: { 168 | email: 'test@test.com', 169 | name: 'test' 170 | } 171 | } 172 | ], 173 | num_rcpt_errors: 3 174 | }; 175 | 176 | return recipientLists.update('test-id', testList) 177 | .then(function() { 178 | expect(client.put.firstCall.args[0].qs).to.deep.equal({num_rcpt_errors: 3}); 179 | }); 180 | }); 181 | 182 | }); 183 | 184 | describe('delete', function() { 185 | 186 | it('should call client delete method with the appropriate uri', function() { 187 | return recipientLists.delete('test', callback) 188 | .then(function() { 189 | expect(client.delete.firstCall.args[0].uri).to.equal('recipient-lists/test'); 190 | expect(client.delete.firstCall.args[1]).to.equal(callback); 191 | }); 192 | }); 193 | 194 | it('should throw an error if id is missing', function() { 195 | return expect(recipientLists.delete()).to.be.rejectedWith('id is required'); 196 | }); 197 | 198 | }); 199 | 200 | }); 201 | -------------------------------------------------------------------------------- /test/spec/relayWebhooks.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai') 4 | , expect = chai.expect 5 | , SparkPost = require('../../lib/sparkpost') 6 | , sinon = require('sinon'); 7 | 8 | require('sinon-as-promised'); 9 | 10 | chai.use(require('sinon-chai')); 11 | chai.use(require('chai-as-promised')); 12 | 13 | describe('Relay Webhooks Library', function() { 14 | let client, relayWebhooks, callback; 15 | 16 | beforeEach(function() { 17 | client = { 18 | get: sinon.stub().resolves({}), 19 | post: sinon.stub().resolves({}), 20 | put: sinon.stub().resolves({}), 21 | delete: sinon.stub().resolves({}), 22 | reject: SparkPost.prototype.reject 23 | }; 24 | 25 | callback = function() {}; 26 | 27 | relayWebhooks = require('../../lib/relayWebhooks')(client); 28 | }); 29 | 30 | describe('list Method', function() { 31 | it('should call client get method with the appropriate uri', function() { 32 | return relayWebhooks.list(callback) 33 | .then(function() { 34 | expect(client.get.firstCall.args[0].uri).to.equal('relay-webhooks'); 35 | expect(client.get.firstCall.args[1]).to.equal(callback); 36 | }); 37 | }); 38 | }); 39 | 40 | describe('get Method', function() { 41 | it('should call client get method with the appropriate uri', function() { 42 | return relayWebhooks.get('test', callback) 43 | .then(function() { 44 | expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'relay-webhooks/test'}); 45 | expect(client.get.firstCall.args[1]).to.equal(callback); 46 | }); 47 | }); 48 | 49 | it('should throw an error if id is missing', function() { 50 | return expect(relayWebhooks.get()).to.be.rejectedWith('id is required'); 51 | }); 52 | }); 53 | 54 | describe('create Method', function() { 55 | it('should call client post method with the appropriate uri and payload', function() { 56 | let webhook = { 57 | target: 'test', 58 | domain: 'inbound.example.com' 59 | }; 60 | 61 | return relayWebhooks.create(webhook, callback) 62 | .then(function() { 63 | expect(client.post.firstCall.args[0].uri).to.equal('relay-webhooks'); 64 | expect(client.post.firstCall.args[0].json).to.deep.equal(webhook); 65 | expect(client.post.firstCall.args[1]).to.equal(callback); 66 | }); 67 | }); 68 | 69 | it('should throw an error if webhook object is missing', function() { 70 | return expect(relayWebhooks.create()).to.be.rejectedWith('webhook object is required'); 71 | }); 72 | }); 73 | 74 | describe('update Method', function() { 75 | it('should call client put method with the appropriate uri and payload', function() { 76 | let webhook = { 77 | name: 'New Replies Webhook' 78 | }; 79 | 80 | return relayWebhooks.update('test', webhook, callback) 81 | .then(function() { 82 | expect(client.put.firstCall.args[0].uri).to.equal('relay-webhooks/test'); 83 | expect(client.put.firstCall.args[0].json).to.deep.equal(webhook); 84 | expect(client.put.firstCall.args[1]).to.equal(callback); 85 | }); 86 | }); 87 | 88 | it('should throw an error if webhook.id is missing', function() { 89 | return expect(relayWebhooks.update()).to.be.rejectedWith('id is required'); 90 | }); 91 | 92 | it('should throw an error if webhook object is missing', function() { 93 | return expect(relayWebhooks.update('test')).to.be.rejectedWith('webhook object is required'); 94 | }); 95 | }); 96 | 97 | describe('delete Method', function() { 98 | it('should call client delete method with the appropriate uri', function() { 99 | return relayWebhooks.delete('test', callback) 100 | .then(function() { 101 | expect(client.delete.firstCall.args[0].uri).to.equal('relay-webhooks/test'); 102 | expect(client.delete.firstCall.args[1]).to.equal(callback); 103 | }); 104 | }); 105 | 106 | it('should throw an error if id is missing', function() { 107 | return expect(relayWebhooks.delete()).to.be.rejectedWith('id is required'); 108 | }); 109 | }); 110 | }); 111 | -------------------------------------------------------------------------------- /test/spec/sendingDomains.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var _ = require('lodash') 4 | , chai = require('chai') 5 | , expect = chai.expect 6 | , SparkPost = require('../../lib/sparkpost') 7 | , sinon = require('sinon'); 8 | 9 | require('sinon-as-promised'); 10 | 11 | chai.use(require('sinon-chai')); 12 | chai.use(require('chai-as-promised')); 13 | 14 | describe('Sending Domains Library', function() { 15 | var client, sendingDomains, callback; 16 | 17 | beforeEach(function() { 18 | client = { 19 | get: sinon.stub().resolves({}), 20 | post: sinon.stub().resolves({}), 21 | put: sinon.stub().resolves({}), 22 | delete: sinon.stub().resolves({}), 23 | reject: SparkPost.prototype.reject 24 | }; 25 | 26 | callback = function() {}; 27 | 28 | sendingDomains = require('../../lib/sendingDomains')(client); 29 | }); 30 | 31 | describe('list', function() { 32 | it('should call client get method with the appropriate uri', function() { 33 | return sendingDomains.list(callback) 34 | .then(function() { 35 | expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'sending-domains'}); 36 | expect(client.get.firstCall.args[1]).to.equal(callback); 37 | }); 38 | }); 39 | }); 40 | 41 | describe('get', function() { 42 | it('should call client get method with the appropriate uri', function() { 43 | return sendingDomains.get('test', callback) 44 | .then(function() { 45 | expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'sending-domains/test'}); 46 | expect(client.get.firstCall.args[1]).to.equal(callback); 47 | }); 48 | }); 49 | 50 | it('should throw an error if domain is missing', function() { 51 | return expect(sendingDomains.get()).to.be.rejectedWith('domain is required'); 52 | }); 53 | }); 54 | 55 | describe('create', function() { 56 | it('should call client post method with the appropriate uri and payload', function() { 57 | var sendingDomain = { 58 | domain: 'test' 59 | }; 60 | 61 | return sendingDomains.create(sendingDomain, callback).then(function() { 62 | expect(client.post.firstCall.args[0].uri).to.equal('sending-domains'); 63 | expect(client.post.firstCall.args[0].json).to.deep.equal(sendingDomain); 64 | expect(client.post.firstCall.args[1]).to.equal(callback); 65 | }); 66 | }); 67 | 68 | it('should throw an error if create options are missing', function() { 69 | return expect(sendingDomains.create()).to.be.rejectedWith('create options are required'); 70 | }); 71 | }); 72 | 73 | describe('update', function() { 74 | it('should call client put method with the appropriate uri and payload', function() { 75 | var sendingDomain = { 76 | tracking_domain: 'click.example1.com' 77 | }; 78 | 79 | return sendingDomains.update('test', sendingDomain, callback) 80 | .then(function() { 81 | expect(client.put.firstCall.args[0].uri).to.equal('sending-domains/test'); 82 | expect(client.put.firstCall.args[0].json).to.deep.equal(_.omit(sendingDomain, 'domain')); 83 | expect(client.put.firstCall.args[1]).to.equal(callback); 84 | }); 85 | }); 86 | 87 | it('should throw an error if update options are missing', function() { 88 | return expect(sendingDomains.update('test')).to.be.rejectedWith('update options are required'); 89 | }); 90 | 91 | it('should throw an error if domain is missing', function() { 92 | return expect(sendingDomains.update()).to.be.rejectedWith('domain is required'); 93 | }); 94 | }); 95 | 96 | describe('delete', function() { 97 | it('should call client delete method with the appropriate uri', function() { 98 | return sendingDomains.delete('test', callback) 99 | .then(function() { 100 | expect(client.delete.firstCall.args[0].uri).to.equal('sending-domains/test'); 101 | expect(client.delete.firstCall.args[1]).to.equal(callback); 102 | }); 103 | }); 104 | 105 | it('should throw an error if domain is missing', function() { 106 | return expect(sendingDomains.delete()).to.be.rejectedWith('domain is required'); 107 | }); 108 | }); 109 | 110 | describe('verify', function() { 111 | it('should call client post method with the appropriate uri and payload', function() { 112 | var options = { 113 | dkim_verify: true, 114 | spf_verify: true 115 | }; 116 | 117 | return sendingDomains.verify('test', options, callback) 118 | .then(function() { 119 | expect(client.post.firstCall.args[0].uri).to.equal('sending-domains/test/verify'); 120 | expect(client.post.firstCall.args[0].json).to.deep.equal(_.omit(options, 'domain')); 121 | expect(client.post.firstCall.args[1]).to.equal(callback); 122 | }); 123 | }); 124 | 125 | it('should throw an error if domain is missing', function() { 126 | return expect(sendingDomains.verify()).to.be.rejectedWith('domain is required'); 127 | }); 128 | 129 | it('should throw an error if verification options are missing', function() { 130 | return expect(sendingDomains.verify('test')).to.be.rejectedWith('verification options are required'); 131 | }); 132 | }); 133 | 134 | }); 135 | -------------------------------------------------------------------------------- /test/spec/subaccounts.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai') 4 | , expect = chai.expect 5 | , SparkPost = require('../../lib/sparkpost') 6 | , sinon = require('sinon'); 7 | 8 | require('sinon-as-promised'); 9 | 10 | chai.use(require('sinon-chai')); 11 | chai.use(require('chai-as-promised')); 12 | 13 | describe('Subaccounts Library', function() { 14 | let client, subaccounts, callback; 15 | 16 | beforeEach(function() { 17 | client = { 18 | get: sinon.stub().resolves({}), 19 | post: sinon.stub().resolves({}), 20 | put: sinon.stub().resolves({}), 21 | reject: SparkPost.prototype.reject 22 | }; 23 | 24 | callback = function() {}; 25 | 26 | subaccounts = require('../../lib/subaccounts')(client); 27 | }); 28 | 29 | describe('list Method', function() { 30 | it('should call client get method with the appropriate uri', function() { 31 | return subaccounts.list(callback) 32 | .then(function() { 33 | expect(client.get.firstCall.args[0].uri).to.equal('subaccounts'); 34 | expect(client.get.firstCall.args[1]).to.equal(callback); 35 | }); 36 | }); 37 | }); 38 | 39 | describe('get Method', function() { 40 | it('should call client get method with the appropriate uri', function() { 41 | return subaccounts.get('test', callback) 42 | .then(function() { 43 | expect(client.get.firstCall.args[0].uri).to.equal('subaccounts/test'); 44 | expect(client.get.firstCall.args[1]).to.equal(callback); 45 | }); 46 | }); 47 | 48 | it('should throw an error if id is missing', function() { 49 | return expect(subaccounts.get()).to.be.rejectedWith('id is required'); 50 | }); 51 | }); 52 | 53 | describe('create Method', function() { 54 | it('should call client post method with the appropriate uri and payload', function() { 55 | var subaccount = { 56 | name: 'test', 57 | key_label: 'test', 58 | key_grants: [] 59 | }; 60 | 61 | return subaccounts.create(subaccount, callback) 62 | .then(function() { 63 | expect(client.post.firstCall.args[0].uri).to.equal('subaccounts'); 64 | expect(client.post.firstCall.args[0].json).to.deep.equal(subaccount); 65 | expect(client.post.firstCall.args[1]).to.equal(callback); 66 | }); 67 | }); 68 | 69 | it('should throw an error if subaccount object is missing', function() { 70 | return expect(subaccounts.create()).to.be.rejectedWith('subaccount object is required'); 71 | }); 72 | }); 73 | 74 | describe('update Method', function() { 75 | it('should call client put method with the appropriate uri and payload', function() { 76 | var subaccount = { 77 | name: 'Hey Joe! Garage and Parts', 78 | status: 'suspended', 79 | ip_pool: '' 80 | }; 81 | 82 | return subaccounts.update('test', subaccount, callback) 83 | .then(function() { 84 | expect(client.put.firstCall.args[0].uri).to.equal('subaccounts/test'); 85 | expect(client.put.firstCall.args[0].json).to.deep.equal(subaccount); 86 | expect(client.put.firstCall.args[1]).to.equal(callback); 87 | }); 88 | }); 89 | 90 | it('should throw an error if subaccount id is missing from options', function() { 91 | return expect(subaccounts.update()).to.be.rejectedWith('id is required'); 92 | }); 93 | 94 | it('should throw an error if subaccount object is missing', function() { 95 | return expect(subaccounts.update('test')).to.be.rejectedWith('subaccount object is required'); 96 | }); 97 | }); 98 | }); 99 | -------------------------------------------------------------------------------- /test/spec/suppressionList.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai') 4 | , expect = chai.expect 5 | , SparkPost = require('../../lib/sparkpost') 6 | , sinon = require('sinon'); 7 | 8 | require('sinon-as-promised'); 9 | 10 | chai.use(require('sinon-chai')); 11 | chai.use(require('chai-as-promised')); 12 | 13 | describe('Suppression List Library', function() { 14 | let client, suppressionList, callback; 15 | 16 | beforeEach(function() { 17 | client = { 18 | get: sinon.stub().resolves({}), 19 | post: sinon.stub().resolves({}), 20 | put: sinon.stub().resolves({}), 21 | delete: sinon.stub().resolves({}), 22 | reject: SparkPost.prototype.reject 23 | }; 24 | 25 | callback = function() {}; 26 | 27 | suppressionList = require('../../lib/suppressionList')(client); 28 | }); 29 | 30 | describe('list', function() { 31 | it('should call client get method with the appropriate uri', function() { 32 | return suppressionList.list({limit: 5}, callback) 33 | .then(function() { 34 | expect(client.get.firstCall.args[0].uri).to.equal('suppression-list'); 35 | expect(client.get.firstCall.args[1]).to.equal(callback); 36 | }); 37 | }); 38 | }); 39 | 40 | describe('get', function() { 41 | it('should call client get method with the appropriate uri', function() { 42 | return suppressionList.get('test@test.com', callback) 43 | .then(function() { 44 | expect(client.get.firstCall.args[0].uri).to.equal('suppression-list/test@test.com'); 45 | expect(client.get.firstCall.args[1]).to.equal(callback); 46 | }); 47 | }); 48 | 49 | it('should throw an error if email is missing', function() { 50 | return expect(suppressionList.get()).to.be.rejectedWith('email is required'); 51 | }); 52 | }); 53 | 54 | describe('upsert', function() { 55 | it('should accept a single list entry', function() { 56 | var listEntry = { email: 'test@test.com' }; 57 | 58 | return suppressionList.upsert(listEntry, callback) 59 | .then(function() { 60 | expect(client.put.firstCall.args[0].uri).to.equal('suppression-list'); 61 | expect(client.put.firstCall.args[0].json.recipients).to.deep.equal([listEntry]); 62 | expect(client.put.firstCall.args[1]).to.equal(callback); 63 | }); 64 | }); 65 | 66 | it('should accept an array of list entries', function() { 67 | var listEntries = [ 68 | { email: 'test1@test.com' }, 69 | { email: 'test2@test.com' } 70 | ]; 71 | 72 | return suppressionList.upsert(listEntries) 73 | .then(function() { 74 | expect(client.put.firstCall.args[0].uri).to.equal('suppression-list'); 75 | expect(client.put.firstCall.args[0].json.recipients).to.deep.equal(listEntries); 76 | }); 77 | }); 78 | 79 | it('should throw an error if recipient is missing', function() { 80 | return expect(suppressionList.upsert()).to.be.rejectedWith('list entries is required'); 81 | }); 82 | }); 83 | 84 | describe('delete', function() { 85 | it('should call client delete method with the appropriate uri', function() { 86 | return suppressionList.delete('test@test.com', callback) 87 | .then(function() { 88 | expect(client.delete.firstCall.args[0].uri).to.equal('suppression-list/test@test.com'); 89 | expect(client.delete.firstCall.args[1]).to.equal(callback); 90 | }); 91 | }); 92 | 93 | it('should throw an error if email deleteEntry missing', function() { 94 | return expect(suppressionList.delete()).to.be.rejectedWith('email is required'); 95 | }); 96 | }); 97 | 98 | }); 99 | -------------------------------------------------------------------------------- /test/spec/templates.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var _ = require('lodash') 3 | , chai = require('chai') 4 | , expect = chai.expect 5 | , SparkPost = require('../../lib/sparkpost') 6 | , sinon = require('sinon'); 7 | 8 | require('sinon-as-promised'); 9 | 10 | chai.use(require('sinon-chai')); 11 | chai.use(require('chai-as-promised')); 12 | 13 | describe('Templates Library', function() { 14 | var client, templates, callback; 15 | 16 | beforeEach(function() { 17 | client = { 18 | get: sinon.stub().resolves({}), 19 | post: sinon.stub().resolves({}), 20 | put: sinon.stub().resolves({}), 21 | delete: sinon.stub().resolves({}), 22 | reject: SparkPost.prototype.reject 23 | }; 24 | 25 | callback = function() {}; 26 | 27 | templates = require('../../lib/templates')(client); 28 | }); 29 | 30 | describe('list Method', function() { 31 | it('should call client get method with the appropriate uri', function() { 32 | return templates.list(callback) 33 | .then(function() { 34 | expect(client.get.firstCall.args[0].uri).to.equal('templates'); 35 | expect(client.get.firstCall.args[1]).to.equal(callback); 36 | }); 37 | }); 38 | }); 39 | 40 | describe('get Method', function() { 41 | it('should call client get method with the appropriate uri', function() { 42 | var id = 'test'; 43 | return templates.get(id, callback) 44 | .then(function() { 45 | expect(client.get.firstCall.args[0].uri).to.equal('templates/test'); 46 | expect(client.get.firstCall.args[1]).to.equal(callback); 47 | }); 48 | }); 49 | 50 | it('should throw an error if id is missing', function() { 51 | return expect(templates.get()).to.be.rejectedWith('template id is required'); 52 | }); 53 | 54 | it('should allow draft to be set in options', function() { 55 | var id = 'test' 56 | , options = { 57 | draft: true 58 | }; 59 | 60 | 61 | return templates.get(id, options) 62 | .then(function() { 63 | expect(client.get.firstCall.args[0].qs).to.deep.equal({draft: true}); 64 | }); 65 | }); 66 | }); 67 | 68 | describe('create Method', function() { 69 | it('should call client post method with the appropriate uri and payload', function() { 70 | var template = { 71 | id: 'test' 72 | }; 73 | 74 | return templates.create(template, callback) 75 | .then(function() { 76 | expect(client.post.firstCall.args[0].uri).to.equal('templates'); 77 | expect(client.post.firstCall.args[0].json).to.deep.equal(template); 78 | expect(client.post.firstCall.args[1]).to.equal(callback); 79 | }); 80 | }); 81 | 82 | it('should throw an error if template object is missing', function() { 83 | return expect(templates.create()).to.be.rejectedWith('template object is required'); 84 | }); 85 | }); 86 | 87 | describe('update Method', function() { 88 | it('should call client put method with the appropriate uri and payload', function() { 89 | var id = 'test' 90 | , template = { 91 | name: 'A new name!' 92 | }; 93 | 94 | return templates.update(id, template, callback) 95 | .then(function() { 96 | expect(client.put.firstCall.args[0].uri).to.equal('templates/test'); 97 | expect(client.put.firstCall.args[0].json).to.deep.equal(template); 98 | expect(client.put.firstCall.args[1]).to.equal(callback); 99 | }); 100 | }); 101 | 102 | it('should throw an error if template id is missing', function() { 103 | return expect(templates.update()).to.be.rejectedWith('template id is required'); 104 | }); 105 | 106 | it('should throw an error if template object is missing', function() { 107 | return expect(templates.update('test')).to.be.rejectedWith('template object is required'); 108 | }); 109 | 110 | it('should not throw an error if optional 3nd argument is a function (callback)', function() { 111 | let cb = sinon.stub() 112 | , id = 'test' 113 | , template = { 114 | name: 'A new name!' 115 | }; 116 | 117 | client.put.yields(); 118 | 119 | return templates.update(id, template, cb).then(function() { 120 | expect(cb.callCount).to.equal(1); 121 | }); 122 | }); 123 | 124 | it('should allow update_published to be set in options', function() { 125 | var id = 'test' 126 | , template = { 127 | name: 'Test Template' 128 | } 129 | , options = { 130 | update_published: true 131 | }; 132 | 133 | return templates.update(id, template, options) 134 | .then(function() { 135 | expect(client.put.firstCall.args[0].qs).to.deep.equal(options); 136 | }); 137 | }); 138 | }); 139 | 140 | describe('delete Method', function() { 141 | it('should call client delete method with the appropriate uri', function() { 142 | return templates.delete('test', callback) 143 | .then(function() { 144 | expect(client.delete.firstCall.args[0].uri).to.equal('templates/test'); 145 | expect(client.delete.firstCall.args[1]).to.equal(callback); 146 | }); 147 | }); 148 | 149 | it('should throw an error if id is missing', function() { 150 | return expect(templates.delete()).to.be.rejectedWith('template id is required'); 151 | }); 152 | }); 153 | 154 | describe('preview Method', function() { 155 | it('should call client post method with the appropriate uri and payload', function() { 156 | var id = 'test' 157 | , options = { 158 | substitution_data: { 159 | 'name': 'Natalie', 160 | 'age': 35, 161 | 'member': true 162 | } 163 | }; 164 | return templates.preview(id, options, callback) 165 | .then(function() { 166 | expect(client.post.firstCall.args[0].uri).to.equal('templates/test/preview'); 167 | expect(client.post.firstCall.args[0].json).to.deep.equal(options); 168 | expect(client.post.firstCall.args[1]).to.equal(callback); 169 | }); 170 | }); 171 | 172 | it('should throw an error if id is missing', function() { 173 | return expect(templates.preview()).to.be.rejectedWith('template id is required'); 174 | }); 175 | 176 | it('should not throw an error if optional 2nd argument is a function (callback)', function() { 177 | let cb = sinon.stub(); 178 | 179 | client.post.yields(); 180 | 181 | return templates.preview('test', cb).then(function() { 182 | expect(cb.callCount).to.equal(1); 183 | }); 184 | }); 185 | 186 | it('should add the draft option to query params', function() { 187 | var id = 'test' 188 | , options = { 189 | draft: false 190 | }; 191 | 192 | return templates.preview(id, options) 193 | .then(function() { 194 | expect(client.post.firstCall.args[0].qs).to.deep.equal({draft: false}); 195 | }); 196 | }); 197 | }); 198 | }); 199 | -------------------------------------------------------------------------------- /test/spec/transmissions.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai') 4 | , expect = chai.expect 5 | , SparkPost = require('../../lib/sparkpost') 6 | , sinon = require('sinon'); 7 | 8 | require('sinon-as-promised'); 9 | 10 | chai.use(require('sinon-chai')); 11 | chai.use(require('chai-as-promised')); 12 | var ccTransmission = { 13 | recipients: [ 14 | { 15 | address: '"Bob" ' 16 | }, 17 | { 18 | address: { 19 | email: 'recipient2@gmail.com', 20 | name: 'Bertha', 21 | } 22 | }, 23 | { 24 | address: { 25 | email: 'recipient3@gmail.com' 26 | } 27 | }, 28 | { 29 | address: 'recipient4@gmail.com' 30 | }, 31 | ], 32 | cc: [ 33 | { 34 | address: '"John" ' 35 | }, 36 | { 37 | address: { 38 | email: 'cc2@gmail.com', 39 | name: 'Jane', 40 | } 41 | } 42 | ], 43 | content: { 44 | template_id: 'hello-world' 45 | } 46 | } 47 | , ccExpectedRecipients = [ 48 | { 49 | address: { 50 | email: 'recipient1@gmail.com', 51 | name: 'Bob', 52 | } 53 | }, 54 | { 55 | address: { 56 | email: 'recipient2@gmail.com', 57 | name: 'Bertha', 58 | } 59 | }, 60 | { 61 | address: { 62 | email: 'recipient3@gmail.com', 63 | } 64 | }, 65 | { 66 | address: { 67 | email: 'recipient4@gmail.com', 68 | } 69 | }, 70 | { 71 | address: { 72 | email: 'cc1@gmail.com', 73 | header_to: '"Bob" , "Bertha" , recipient3@gmail.com, recipient4@gmail.com' 74 | } 75 | }, 76 | { 77 | address: { 78 | email: 'cc2@gmail.com', 79 | header_to: '"Bob" , "Bertha" , recipient3@gmail.com, recipient4@gmail.com' 80 | } 81 | } 82 | ] 83 | , expectedCCHeader = '"John" , "Jane" '; 84 | 85 | describe('Transmissions Library', function() { 86 | var client, transmissions, callback; 87 | 88 | beforeEach(function() { 89 | client = { 90 | get: sinon.stub().resolves({}), 91 | post: sinon.stub().resolves({}), 92 | reject: SparkPost.prototype.reject 93 | }; 94 | 95 | callback = function() {}; 96 | 97 | transmissions = require('../../lib/transmissions')(client); 98 | }); 99 | 100 | describe('list Method', function() { 101 | it('should call client get method with the appropriate uri', function() { 102 | return transmissions.list(callback) 103 | .then(function() { 104 | expect(client.get.firstCall.args[0].uri).to.equal('transmissions'); 105 | expect(client.get.firstCall.args[1]).to.equal(callback); 106 | }); 107 | }); 108 | 109 | it('should allow campaign_id to be set in options', function() { 110 | var options = { 111 | campaign_id: 'test-campaign' 112 | }; 113 | 114 | return transmissions.list(options) 115 | .then(function() { 116 | expect(client.get.firstCall.args[0].qs).to.deep.equal({campaign_id: 'test-campaign'}); 117 | }); 118 | }); 119 | 120 | it('should allow template_id to be set in options', function() { 121 | var options = { 122 | template_id: 'test-template' 123 | }; 124 | 125 | return transmissions.list(options) 126 | .then(function() { 127 | expect(client.get.firstCall.args[0].qs).to.deep.equal({template_id: 'test-template'}); 128 | }); 129 | }); 130 | }); 131 | 132 | describe('find Method', function() { 133 | it('should call client get method with the appropriate uri', function() { 134 | return transmissions.get('test', callback) 135 | .then(function() { 136 | expect(client.get.firstCall.args[0]).to.deep.equal({uri: 'transmissions/test'}); 137 | expect(client.get.firstCall.args[1]).to.equal(callback); 138 | }); 139 | }); 140 | 141 | it('should throw an error if id is missing', function() { 142 | return expect(transmissions.get()).to.be.rejectedWith('id is required'); 143 | }); 144 | }); 145 | 146 | describe('send Method', function() { 147 | it('should call client post method with the appropriate uri and payload', function() { 148 | var transmission = { 149 | campaign_id: 'test-campaign' 150 | }; 151 | 152 | return transmissions.send(transmission, callback) 153 | .then(function() { 154 | expect(client.post.firstCall.args[0].uri).to.equal('transmissions'); 155 | expect(client.post.firstCall.args[0].json).to.deep.equal(transmission); 156 | expect(client.post.firstCall.args[1]).to.equal(callback); 157 | }); 158 | }); 159 | 160 | it('should throw an error if transmission object is missing', function() { 161 | return expect(transmissions.send(function() {})).to.be.rejectedWith('transmission object is required'); 162 | }); 163 | 164 | it('should allow num_rcpt_errors to be set in options', function() { 165 | var transmission = { 166 | campaign_id: 'test-campaign' 167 | } 168 | , options = { 169 | num_rcpt_errors: 3 170 | }; 171 | 172 | return transmissions.send(transmission, options) 173 | .then(function() { 174 | expect(client.post.firstCall.args[0].qs).to.deep.equal({num_rcpt_errors: 3}); 175 | }); 176 | }); 177 | 178 | it('should not throw an error if optional 2nd argument is a function (callback)', function() { 179 | let cb = sinon.stub() 180 | , transmission = { 181 | campaign_id: 'test-campaign' 182 | }; 183 | 184 | client.post.yields(); 185 | 186 | return transmissions.send(transmission, cb).then(function() { 187 | expect(cb.callCount).to.equal(1); 188 | }); 189 | }); 190 | 191 | it('should leave email_rfc822 content keys intact', function() { 192 | var options = { 193 | content: { 194 | email_rfc822: 'Content-Type: text/plain\nFrom: From Envelope \nSubject: Example Email\n\nHello World' 195 | } 196 | }; 197 | 198 | return transmissions.send(options) 199 | .then(function() { 200 | expect(client.post.firstCall.args[0].json.content).to.have.property('email_rfc822'); 201 | }); 202 | }); 203 | 204 | it('should allow a list_id and template through', function() { 205 | var transmission = { 206 | recipients: { 207 | list_id: 'my-list-id' 208 | }, 209 | content: { 210 | template_id: 'my-template-id' 211 | } 212 | }; 213 | 214 | return transmissions.send(transmission) 215 | .then(function() { 216 | expect(client.post.firstCall.args[0].json).to.deep.equal(transmission); 217 | }); 218 | }); 219 | 220 | it('should ignore empty bcc and cc', function() { 221 | var transmission = { 222 | recipients: [ 223 | { 224 | address: { 225 | name: "Bob", 226 | email: "recipient1@gmail.com" 227 | } 228 | }, 229 | ], 230 | cc: [], 231 | bcc: [], 232 | content: { 233 | template_id: 'my-template-id' 234 | } 235 | }; 236 | 237 | return transmissions.send(transmission) 238 | .then(function() { 239 | delete transmission.cc; 240 | delete transmission.bcc; 241 | 242 | expect(client.post.firstCall.args[0].json).to.deep.equal(transmission); 243 | }); 244 | }); 245 | 246 | it('should convert cc to the correct recipients and headers', function() { 247 | return transmissions.send(ccTransmission) 248 | .then(function() { 249 | expect(client.post.firstCall.args[0].json.recipients).to.deep.equal(ccExpectedRecipients); 250 | expect(client.post.firstCall.args[0].json.content.headers.CC).to.deep.equal(expectedCCHeader); 251 | }); 252 | }); 253 | 254 | it('should convert bcc to the correct recipients and headers', function() { 255 | var bccTransmission = ccTransmission 256 | , bccExpectedRecipients = ccExpectedRecipients; 257 | bccTransmission['bcc'] = bccTransmission['cc']; 258 | delete bccTransmission['cc']; 259 | 260 | return transmissions.send(bccTransmission) 261 | .then(function() { 262 | expect(client.post.firstCall.args[0].json.recipients).to.deep.equal(bccExpectedRecipients); 263 | expect(client.post.firstCall.args[0].json.content.headers).to.be.undefined; 264 | }); 265 | }); 266 | 267 | it('should not modify a transmission using the full cc/bcc syntax', function() { 268 | var transmission = { 269 | recipients: [ 270 | { 271 | address: { 272 | email: 'original.recipient@example.com', 273 | name: 'Original Recipient' 274 | }, 275 | substitution_data: { 276 | recipient_type: 'Original' 277 | } 278 | }, 279 | { 280 | address: { 281 | email: 'bcc.recipient@example.com', 282 | header_to: '"Original Recipient" ' 283 | }, 284 | substitution_data: { 285 | recipient_type: 'BCC' 286 | } 287 | } 288 | ], 289 | content: { 290 | from: { 291 | name: 'Node BCC Test', 292 | email: 'from@example.com' 293 | }, 294 | subject: 'Example email using bcc', 295 | text: 'An example email using bcc with SparkPost to the {{recipient_type}} recipient.', 296 | html: '

An example email using bcc with SparkPost to the {{recipient_type}} recipient.

' 297 | } 298 | }; 299 | 300 | return transmissions.send(transmission) 301 | .then(function() { 302 | expect(client.post.firstCall.args[0].json).to.deep.equal(transmission); 303 | }); 304 | }); 305 | }); 306 | }); 307 | -------------------------------------------------------------------------------- /test/spec/webhooks.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var _ = require('lodash') 4 | , chai = require('chai') 5 | , expect = chai.expect 6 | , SparkPost = require('../../lib/sparkpost') 7 | , sinon = require('sinon'); 8 | 9 | require('sinon-as-promised'); 10 | 11 | chai.use(require('sinon-chai')); 12 | chai.use(require('chai-as-promised')); 13 | 14 | describe('Webhooks Library', function() { 15 | var client, webhooks, callback; 16 | 17 | beforeEach(function() { 18 | client = { 19 | get: sinon.stub().resolves({}), 20 | post: sinon.stub().resolves({}), 21 | put: sinon.stub().resolves({}), 22 | delete: sinon.stub().resolves({}), 23 | reject: SparkPost.prototype.reject 24 | }; 25 | 26 | callback = function() {}; 27 | 28 | webhooks = require('../../lib/webhooks')(client); 29 | }); 30 | 31 | describe('list Method', function() { 32 | it('should call client get method with the appropriate uri', function() { 33 | return webhooks.list(callback) 34 | .then(function() { 35 | expect(client.get.firstCall.args[0].uri).to.equal('webhooks'); 36 | expect(client.get.firstCall.args[1]).to.equal(callback); 37 | }); 38 | }); 39 | 40 | it('should allow timezone to be set in options', function() { 41 | var options = { 42 | timezone: 'America/New_York' 43 | }; 44 | 45 | return webhooks.list(options) 46 | .then(function() { 47 | expect(client.get.firstCall.args[0].qs).to.deep.equal({timezone: 'America/New_York'}); 48 | }); 49 | }); 50 | }); 51 | 52 | describe('get Method', function() { 53 | it('should call client get method with the appropriate uri', function() { 54 | return webhooks.get('test', callback) 55 | .then(function() { 56 | expect(client.get.firstCall.args[0].uri).to.equal('webhooks/test'); 57 | expect(client.get.firstCall.args[1]).to.equal(callback); 58 | }); 59 | }); 60 | 61 | it('should throw an error if id is missing', function() { 62 | return expect(webhooks.get()).to.be.rejectedWith('id is required'); 63 | }); 64 | 65 | it('should allow timezone to be set in options', function() { 66 | var options = { 67 | timezone: 'America/New_York' 68 | }; 69 | 70 | return webhooks.get('test', options) 71 | .then(function() { 72 | expect(client.get.firstCall.args[0].qs).to.deep.equal({timezone: 'America/New_York'}); 73 | }); 74 | }); 75 | }); 76 | 77 | describe('create Method', function() { 78 | it('should call client post method with the appropriate uri and payload', function() { 79 | var webhook = { 80 | name: 'Example webhook', 81 | target: 'http://client.example.com/example-webhook', 82 | events: ['delivery', 'injection', 'open', 'click'] 83 | }; 84 | 85 | return webhooks.create(webhook, callback) 86 | .then(function() { 87 | expect(client.post.firstCall.args[0].uri).to.equal('webhooks'); 88 | expect(client.post.firstCall.args[0].json).to.deep.equal(webhook); 89 | expect(client.post.firstCall.args[1]).to.equal(callback); 90 | }); 91 | }); 92 | 93 | it('should throw an error if webhook is missing', function() { 94 | return expect(webhooks.create()).to.be.rejectedWith('webhook object is required'); 95 | }); 96 | }); 97 | 98 | describe('update Method', function() { 99 | it('should call client put method with the appropriate uri', function() { 100 | var webhook = { 101 | name: 'Renamed webhook', 102 | events: ['rejection', 'delay'], 103 | auth_type: 'none' 104 | }; 105 | 106 | return webhooks.update('test', webhook, callback) 107 | .then(function() { 108 | expect(client.put.firstCall.args[0].uri).to.equal('webhooks/test'); 109 | expect(client.put.firstCall.args[0].json).to.deep.equal(webhook); 110 | expect(client.put.firstCall.args[1]).to.equal(callback); 111 | }); 112 | }); 113 | 114 | it('should throw an error if id is missing', function() { 115 | return expect(webhooks.update()).to.be.rejectedWith('id is required'); 116 | }); 117 | 118 | it('should throw an error if webhook is missing', function() { 119 | return expect(webhooks.update('test')).to.be.rejectedWith('webhook object is required'); 120 | }); 121 | }); 122 | 123 | describe('delete Method', function() { 124 | it('should call client delete method with the appropriate uri', function() { 125 | return webhooks.delete('test', callback) 126 | .then(function() { 127 | expect(client.delete.firstCall.args[0].uri).to.equal('webhooks/test'); 128 | expect(client.delete.firstCall.args[1]).to.equal(callback); 129 | }); 130 | }); 131 | 132 | it('should throw an error if id is missing', function() { 133 | return expect(webhooks.delete()).to.be.rejectedWith('id is required'); 134 | }); 135 | }); 136 | 137 | describe('validate Method', function() { 138 | it('should call client post method with the appropriate uri and payload', function() { 139 | var options = { 140 | message: { 141 | msys: {} 142 | } 143 | }; 144 | 145 | return webhooks.validate('test', options, callback) 146 | .then(function() { 147 | expect(client.post.firstCall.args[0].uri).to.equal('webhooks/test/validate'); 148 | expect(client.post.firstCall.args[0].json).to.deep.equal(options); 149 | expect(client.post.firstCall.args[1]).to.equal(callback); 150 | }); 151 | }); 152 | 153 | it('should throw an error if id is missing', function() { 154 | return expect(webhooks.validate()).to.be.rejectedWith('id is required'); 155 | }); 156 | 157 | it('should throw an error if message is missing', function() { 158 | return expect(webhooks.validate('test')).to.be.rejectedWith('message is required'); 159 | }); 160 | }); 161 | 162 | describe('getBatchStatus Method', function() { 163 | it('should call client get method with the appropriate uri', function() { 164 | return webhooks.getBatchStatus('test', callback) 165 | .then(function() { 166 | expect(client.get.firstCall.args[0].uri).to.equal('webhooks/test/batch-status'); 167 | expect(client.get.firstCall.args[1]).to.equal(callback); 168 | }); 169 | }); 170 | 171 | it('should throw an error if id is missing', function() { 172 | return expect(webhooks.getBatchStatus()).to.be.rejectedWith('id is required'); 173 | }); 174 | 175 | it('should allow limit to be set in options', function() { 176 | var options = { 177 | limit: 1000 178 | }; 179 | 180 | return webhooks.getBatchStatus('test', options) 181 | .then(function() { 182 | expect(client.get.firstCall.args[0].qs).to.deep.equal({limit: 1000}); 183 | }); 184 | }); 185 | }); 186 | 187 | describe('getDocumentation Method', function() { 188 | it('should call client get method with the appropriate uri', function() { 189 | return webhooks.getDocumentation(callback) 190 | .then(function() { 191 | expect(client.get.firstCall.args[0].uri).to.equal('webhooks/events/documentation'); 192 | expect(client.get.firstCall.args[1]).to.equal(callback); 193 | }); 194 | }); 195 | }); 196 | 197 | describe('getSamples Method', function() { 198 | it('should call client get method with the appropriate uri', function() { 199 | return webhooks.getSamples(callback) 200 | .then(function() { 201 | expect(client.get.firstCall.args[0].uri).to.equal('webhooks/events/samples'); 202 | expect(client.get.firstCall.args[1]).to.equal(callback); 203 | }); 204 | }); 205 | 206 | it('should allow events to be set in options', function() { 207 | var options = { 208 | events: 'bounces' 209 | }; 210 | 211 | return webhooks.getSamples(options) 212 | .then(function() { 213 | expect(client.get.firstCall.args[0].qs).to.deep.equal({events: 'bounces'}); 214 | }); 215 | }); 216 | }); 217 | }); 218 | --------------------------------------------------------------------------------