• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

mailjet/mailjet-apiv3-nodejs: [API v3] Official Mailjet API v3 NodeJS wrapper

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

mailjet/mailjet-apiv3-nodejs

开源软件地址:

https://github.com/mailjet/mailjet-apiv3-nodejs

开源编程语言:

JavaScript 100.0%

开源软件介绍:

alt text

Mailjet NodeJs Wrapper

Build Status Current Version

Overview

Welcome to the Mailjet official NodeJs API wrapper!

Check out all the resources and NodeJs code examples in the official Mailjet Documentation.

Table of contents

Compatibility

This library officially supports the following Node.js versions:

  • v12

Installation

First, create a project folder:

mkdir mailjet-project && cd $_

Then use the following code to install the wrapper:

npm install node-mailjet

If you want to do a global installation, add the -g flag.

Authentication

The Mailjet Email API uses your API and Secret keys for authentication. Grab and save your Mailjet API credentials.

export MJ_APIKEY_PUBLIC='your API key'
export MJ_APIKEY_PRIVATE='your API secret'

Note: For the SMS API the authorization is based on a Bearer token. See information about it in the SMS API section of the readme.

Initialize your Mailjet Client:

const mailjet = require ('node-mailjet')
    .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)

Make your first call

Here's an example on how to send an email:

const mailjet = require ('node-mailjet')
    .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
    .post("send", {'version': 'v3.1'})
    .request({
        "Messages":[{
            "From": {
                "Email": "[email protected]",
                "Name": "Mailjet Pilot"
            },
            "To": [{
                "Email": "[email protected]",
                "Name": "passenger 1"
            }],
            "Subject": "Your email flight plan!",
            "TextPart": "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
            "HTMLPart": "<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"
        }]
    })
request
    .then((result) => {
        console.log(result.body)
    })
    .catch((err) => {
        console.log(err.statusCode)
    })

Client / Call configuration specifics

To instantiate the library you can use the following constructor:

const mailjet = require ('node-mailjet')
    .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
    .METHOD(RESOURCE, {OPTIONS})
  • MJ_APIKEY_PUBLIC : public Mailjet API key
  • MJ_APIKEY_PRIVATE : private Mailjet API key
  • METHOD - the method you want to use for this call (post, put, get, delete)
  • RESOURCE - the API endpoint you want to call
  • OPTIONS : associative array describing the connection options (see Options bellow for full list)

Options

API Versioning

The Mailjet API is spread among three distinct versions:

  • v3 - The Email API
  • v3.1 - Email Send API v3.1, which is the latest version of our Send API
  • v4 - SMS API

Since 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:

const mailjet = require ('node-mailjet')
    .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
    .post("send", {'version': 'v3.1'})

For additional information refer to our API Reference.

Base URL

The 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:

const request = mailjet
    .post("send", {'version': 'v3.1', 'url': 'api.us.mailjet.com'})

If your account has been moved to Mailjet's US architecture, the URL value you need to set is api.us.mailjet.com.

Request timeout

You are able to set a timeout for your request using the timeout parameter. The API request timeout is set in milliseconds:

const request = mailjet
    .post("send", {'version': 'v3.1', 'timeout': 100})

Use proxy

The proxyUrl parameter allows you to set a HTTPS proxy URL to send the API requests through:

const request = mailjet
    .post("send", {'version': 'v3.1', 'proxyUrl': 'YOUR_PROXY_URL'})

The proxy URL is passed directly to superagent-proxy.

Disable API call

By 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:

const request = mailjet
    .post("send", {'version': 'v3.1', 'perform_api_call': false})

Request examples

POST Request

Use the post method of the Mailjet Client:

const request = mailjet
  .post($RESOURCE, {$OPTIONS})
  .id($ID)
  .request({$PARAMS})

.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.

Simple POST request

/**
 *
 * Create a new contact:
 *
 */
const mailjet = require ('node-mailjet')
	.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
	.post("contact")
	.request({
	    "Email":"[email protected]",
	    "IsExcludedFromCampaigns":"true",
	    "Name":"New Contact"
	})
request
	.then((result) => {
		console.log(result.body)
	})
	.catch((err) => {
		console.log(err.statusCode)
	})

Using actions

/**
*
* Manage the subscription status of a contact to multiple lists
*
**/
const mailjet = require ('node-mailjet')
	.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
	.post("contact")
	.id($contact_ID)
	.action("managecontactslists")
	.request({
    "ContactsLists": [{
			"ListID": 987654321,
			"Action": "addnoforce"
		}]
	})
request
	.then((result) => {
		console.log(result.body)
	})
	.catch((err) => {
		console.log(err.statusCode)
	})

GET Request

Use the get method of the Mailjet Client:

const request = mailjet
 .get($RESOURCE, {$OPTIONS})
 .id($ID)
 .request({$PARAMS})

.request will contain any query parameters applied to the request. You need to define .id if you want to retrieve a specific object.

Retrieve all objects

/**
 *
 * Retrieve all contacts
 *
 */
const mailjet = require ('node-mailjet')
	.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
	.get("contact")
	.request()
request
	.then((result) => {
		console.log(result.body)
	})
	.catch((err) => {
		console.log(err.statusCode)
	})

Use filtering

/**
 *
 * Retrieve all contacts that are not in the campaign exclusion list :
 *
 */
const mailjet = require ('node-mailjet')
	.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
	.get("contact")
	.request({'IsExcludedFromCampaigns': false})
request
	.then((result) => {
		console.log(result.body)
	})
	.catch((err) => {
		console.log(err.statusCode)
	})

Retrieve a single object

/**
 *
 * Retrieve a specific contact ID :
 *
 */
const mailjet = require ('node-mailjet')
	.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
	.get("contact")
	.id(Contact_ID)
	.request()
request
	.then((result) => {
		console.log(result.body)
	})
	.catch((err) => {
		console.log(err.statusCode)
	})

PUT Request

Use the put method of the Mailjet Client:

const request = mailjet
 .put($RESOURCE, {$OPTIONS})
 .id($ID)
 .request({$PARAMS})

You need to define .id to specify the object you need to edit. .request will contain the body of the PUT request.

A 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.

Here's an example of a PUT request:

/**
 *
 * Update the contact properties for a contact:
 *
 */
const mailjet = require ('node-mailjet')
    .connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
    .put("contactdata")
    .id($CONTACT_ID)
    .request({
        "Data":[{
            "first_name": "John",
            "last_name": "Smith"
        }]
    })
request
    .then((result) => {
        console.log(result.body)
    })
    .catch((err) => {
        console.log(err.statusCode)
    })

DELETE Request

Use the delete method of the Mailjet Client:

const request = mailjet
 .delete($RESOURCE, {$OPTIONS})
 .id($ID)
 .request()

You need to define .id to specify the object you want to delete. .request should be empty.

Upon a successful DELETE request the response will not include a response body, but only a 204 No Content response code.

Here's an example of a DELETE request:

/**
 *
 * Delete : Delete an email template.
 *
 */
const mailjet = require ('node-mailjet')
	.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE)
const request = mailjet
	.delete("template")
	.id(Template_ID)
	.request()
request
	.then((result) => {
		console.log(result.body)
	})
	.catch((err) => {
		console.log(err.statusCode)
	})

SMS API


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
pksunkara/octonode: github api v3 in nodejs发布时间:2022-04-03
下一篇:
splitbee/notion-api-worker: Notion as CMS with easy API access发布时间:2022-04-03
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap