UNPKG

12.7 kBMarkdownView Raw
1
2[mailjet]: http://www.mailjet.com
3[api_credential]: https://app.mailjet.com/account/api_keys
4[api_token]: https://app.mailjet.com/sms
5[eventemitter]: https://nodejs.org/api/events.html
6[doc]: http://dev.mailjet.com/guides/?javascript#
7[api_doc_repo]: https://github.com/mailjet/api-documentation
8
9![alt text](https://www.mailjet.com/images/email/transac/logo_header.png "Mailjet")
10
11# Mailjet NodeJs Wrapper
12
13[![Build Status](https://travis-ci.org/mailjet/mailjet-apiv3-nodejs.svg?branch=master)](https://travis-ci.org/mailjet/mailjet-apiv3-nodejs)
14![Current Version](https://img.shields.io/badge/version-3.3.1-green.svg)
15
16## Overview
17
18Welcome to the [Mailjet][mailjet] official NodeJs API wrapper!
19
20Check out all the resources and NodeJs code examples in the official [Mailjet Documentation][doc].
21
22## Table of contents
23
24- [Mailjet NodeJs Wrapper](#mailjet-nodejs-wrapper)
25 - [Overview](#overview)
26 - [Table of contents](#table-of-contents)
27 - [Compatibility](#compatibility)
28 - [Installation](#installation)
29 - [Authentication](#authentication)
30 - [Make your first call](#make-your-first-call)
31 - [Client / Call configuration specifics](#client--call-configuration-specifics)
32 - [Options](#options)
33 - [API Versioning](#api-versioning)
34 - [Base URL](#base-url)
35 - [Request timeout](#request-timeout)
36 - [Use proxy](#use-proxy)
37 - [Disable API call](#disable-api-call)
38 - [Request examples](#request-examples)
39 - [POST Request](#post-request)
40 - [Simple POST request](#simple-post-request)
41 - [Using actions](#using-actions)
42 - [GET Request](#get-request)
43 - [Retrieve all objects](#retrieve-all-objects)
44 - [Use filtering](#use-filtering)
45 - [Retrieve a single object](#retrieve-a-single-object)
46 - [PUT Request](#put-request)
47 - [DELETE Request](#delete-request)
48 - [SMS API](#sms-api)
49 - [Token authentication](#token-authentication)
50 - [Example request](#example-request)
51 - [Contribute](#contribute)
52
53## Compatibility
54
55This library officially supports the following Node.js versions:
56
57 - v12
58
59## Installation
60
61First, create a project folder:
62
63`mkdir mailjet-project && cd $_`
64
65Then use the following code to install the wrapper:
66
67`npm install node-mailjet`
68
69If you want to do a global installation, add the `-g` flag.
70
71## Authentication
72
73The Mailjet Email API uses your API and Secret keys for authentication. [Grab][api_credential] and save your Mailjet API credentials.
74
75```bash
76export MJ_APIKEY_PUBLIC='your API key'
77export MJ_APIKEY_PRIVATE='your API secret'
78```
79
80> Note: For the SMS API the authorization is based on a Bearer token. See information about it in the [SMS API](#sms-api) section of the readme.
81
82Initialize your [Mailjet][mailjet] Client:
83
84``` javascript
85const mailjet = require ('node-mailjet')
86 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
87```
88
89## Make your first call
90
91Here's an example on how to send an email:
92
93```javascript
94const mailjet = require ('node-mailjet')
95 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
96const request = mailjet
97 .post("send", {'version': 'v3.1'})
98 .request({
99 "Messages":[{
100 "From": {
101 "Email": "pilot@mailjet.com",
102 "Name": "Mailjet Pilot"
103 },
104 "To": [{
105 "Email": "passenger1@mailjet.com",
106 "Name": "passenger 1"
107 }],
108 "Subject": "Your email flight plan!",
109 "TextPart": "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
110 "HTMLPart": "<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"
111 }]
112 })
113request
114 .then((result) => {
115 console.log(result.body)
116 })
117 .catch((err) => {
118 console.log(err.statusCode)
119 })
120```
121
122## Client / Call configuration specifics
123
124To instantiate the library you can use the following constructor:
125
126```javascript
127const mailjet = require ('node-mailjet')
128 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
129const request = mailjet
130 .METHOD(RESOURCE, {OPTIONS})
131```
132
133 - `MJ_APIKEY_PUBLIC` : public Mailjet API key
134 - `MJ_APIKEY_PRIVATE` : private Mailjet API key
135 - `METHOD` - the method you want to use for this call (`post`, `put`, `get`, `delete`)
136 - `RESOURCE` - the API endpoint you want to call
137 - `OPTIONS` : associative array describing the connection options (see Options bellow for full list)
138
139### Options
140
141#### API Versioning
142
143The Mailjet API is spread among three distinct versions:
144
145- `v3` - The Email API
146- `v3.1` - Email Send API v3.1, which is the latest version of our Send API
147- `v4` - SMS API
148
149Since most Email API endpoints are located under `v3`, it is set as the default one and does not need to be specified when making your request. For the others you need to specify the version using `version`. For example, if using Send API `v3.1`:
150
151```javascript
152const mailjet = require ('node-mailjet')
153 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
154const request = mailjet
155 .post("send", {'version': 'v3.1'})
156```
157
158For additional information refer to our [API Reference](https://dev.mailjet.com/reference/overview/versioning/).
159
160#### Base URL
161
162The default base domain name for the Mailjet API is api.mailjet.com. You can modify this base URL by setting a value for `url` in your call:
163
164```javascript
165const request = mailjet
166 .post("send", {'version': 'v3.1', 'url': 'api.us.mailjet.com'})
167```
168
169If your account has been moved to Mailjet's US architecture, the URL value you need to set is `api.us.mailjet.com`.
170
171#### Request timeout
172
173You are able to set a timeout for your request using the `timeout` parameter. The API request timeout is set in milliseconds:
174
175```javascript
176const request = mailjet
177 .post("send", {'version': 'v3.1', 'timeout': 100})
178```
179
180#### Use proxy
181
182The `proxyUrl` parameter allows you to set a HTTPS proxy URL to send the API requests through:
183
184```javascript
185const request = mailjet
186 .post("send", {'version': 'v3.1', 'proxyUrl': 'YOUR_PROXY_URL'})
187```
188
189The proxy URL is passed directly to [superagent-proxy](https://github.com/TooTallNate/superagent-proxy).
190
191
192### Disable API call
193
194By default the API call parameter is always enabled. However, you may want to disable it during testing to prevent unnecessary calls to the Mailjet API. This is done by setting the `perform_api_call` parameter to `false`:
195
196```javascript
197const request = mailjet
198 .post("send", {'version': 'v3.1', 'perform_api_call': false})
199```
200
201## Request examples
202
203### POST Request
204
205Use the `post` method of the Mailjet Client:
206
207```javascript
208const request = mailjet
209 .post($RESOURCE, {$OPTIONS})
210 .id($ID)
211 .request({$PARAMS})
212```
213
214`.request` will contain the body of the POST request. You need to define `.id` if you want to perform an action on a specific object and need to identify it.
215
216#### Simple POST request
217
218```javascript
219/**
220 *
221 * Create a new contact:
222 *
223 */
224const mailjet = require ('node-mailjet')
225 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
226const request = mailjet
227 .post("contact")
228 .request({
229 "Email":"passenger@mailjet.com",
230 "IsExcludedFromCampaigns":"true",
231 "Name":"New Contact"
232 })
233request
234 .then((result) => {
235 console.log(result.body)
236 })
237 .catch((err) => {
238 console.log(err.statusCode)
239 })
240```
241
242#### Using actions
243
244```javascript
245/**
246*
247* Manage the subscription status of a contact to multiple lists
248*
249**/
250const mailjet = require ('node-mailjet')
251 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
252const request = mailjet
253 .post("contact")
254 .id($contact_ID)
255 .action("managecontactslists")
256 .request({
257 "ContactsLists": [{
258 "ListID": 987654321,
259 "Action": "addnoforce"
260 }]
261 })
262request
263 .then((result) => {
264 console.log(result.body)
265 })
266 .catch((err) => {
267 console.log(err.statusCode)
268 })
269```
270
271### GET Request
272
273Use the `get` method of the Mailjet Client:
274
275```javascript
276const request = mailjet
277 .get($RESOURCE, {$OPTIONS})
278 .id($ID)
279 .request({$PARAMS})
280```
281
282`.request` will contain any query parameters applied to the request. You need to define `.id` if you want to retrieve a specific object.
283
284#### Retrieve all objects
285
286```javascript
287/**
288 *
289 * Retrieve all contacts
290 *
291 */
292const mailjet = require ('node-mailjet')
293 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
294const request = mailjet
295 .get("contact")
296 .request()
297request
298 .then((result) => {
299 console.log(result.body)
300 })
301 .catch((err) => {
302 console.log(err.statusCode)
303 })
304```
305
306#### Use filtering
307
308```javascript
309/**
310 *
311 * Retrieve all contacts that are not in the campaign exclusion list :
312 *
313 */
314const mailjet = require ('node-mailjet')
315 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
316const request = mailjet
317 .get("contact")
318 .request({'IsExcludedFromCampaigns': false})
319request
320 .then((result) => {
321 console.log(result.body)
322 })
323 .catch((err) => {
324 console.log(err.statusCode)
325 })
326```
327
328
329#### Retrieve a single object
330
331```javascript
332/**
333 *
334 * Retrieve a specific contact ID :
335 *
336 */
337const mailjet = require ('node-mailjet')
338 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
339const request = mailjet
340 .get("contact")
341 .id(Contact_ID)
342 .request()
343request
344 .then((result) => {
345 console.log(result.body)
346 })
347 .catch((err) => {
348 console.log(err.statusCode)
349 })
350```
351
352### PUT Request
353
354Use the `put` method of the Mailjet Client:
355
356```javascript
357const request = mailjet
358 .put($RESOURCE, {$OPTIONS})
359 .id($ID)
360 .request({$PARAMS})
361```
362
363You need to define `.id` to specify the object you need to edit. `.request` will contain the body of the PUT request.
364
365A `PUT` request in the Mailjet API will work as a `PATCH` request - the update will affect only the specified properties. The other properties of an existing resource will neither be modified, nor deleted. It also means that all non-mandatory properties can be omitted from your payload.
366
367Here's an example of a PUT request:
368
369```javascript
370/**
371 *
372 * Update the contact properties for a contact:
373 *
374 */
375const mailjet = require ('node-mailjet')
376 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
377const request = mailjet
378 .put("contactdata")
379 .id($CONTACT_ID)
380 .request({
381 "Data":[{
382 "first_name": "John",
383 "last_name": "Smith"
384 }]
385 })
386request
387 .then((result) => {
388 console.log(result.body)
389 })
390 .catch((err) => {
391 console.log(err.statusCode)
392 })
393```
394
395### DELETE Request
396
397Use the `delete` method of the Mailjet Client:
398
399```javascript
400const request = mailjet
401 .delete($RESOURCE, {$OPTIONS})
402 .id($ID)
403 .request()
404```
405
406You need to define `.id` to specify the object you want to delete. `.request` should be empty.
407
408Upon a successful `DELETE` request the response will not include a response body, but only a `204 No Content` response code.
409
410Here's an example of a `DELETE` request:
411
412```javascript
413/**
414 *
415 * Delete : Delete an email template.
416 *
417 */
418const mailjet = require ('node-mailjet')
419 .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
420const request = mailjet
421 .delete("template")
422 .id(Template_ID)
423 .request()
424request
425 .then((result) => {
426 console.log(result.body)
427 })
428 .catch((err) => {
429 console.log(err.statusCode)
430 })
431```
432
433## SMS API
434
435### Token authentication
436
437Authentication for the SMS API endpoints is done using a bearer token. The bearer token is generated in the [SMS section](https://app.mailjet.com/sms) of your Mailjet account.
438
439```javascript
440var Mailjet = require('node-mailjet').connect('api token');
441```
442
443### Example request
444
445Here's an example SMS API request:
446
447```javascript
448const mailjet = require ('node-mailjet')
449 .connect(process.env.MJ_TOKEN)
450const request = mailjet
451 .post("sms-send", {'version': 'v4'})
452 .request({
453 "Text": "Have a nice SMS flight with Mailjet !",
454 "To": "+33600000000",
455 "From": "MJPilot"
456 })
457
458request
459 .then((result) => {
460 console.log(result.body)
461 })
462 .catch((err) => {
463 console.log(err.statusCode)
464 })
465```
466
467## Contribute
468
469Mailjet loves developers. You can be part of this project!
470
471This wrapper is a great introduction to the open source world, check out the code!
472
473Feel free to ask anything, and contribute:
474
475- Fork the project.
476- Create a new branch.
477- Implement your feature or bug fix.
478- Add documentation to it.
479- Commit, push, open a pull request and voila.
480
481If you have suggestions on how to improve the guides, please submit an issue in our [Official API Documentation repo](https://github.com/mailjet/api-documentation).