在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:mailjet/mailjet-apiv3-nodejs开源软件地址:https://github.com/mailjet/mailjet-apiv3-nodejs开源编程语言:JavaScript 100.0%开源软件介绍:Mailjet NodeJs WrapperOverviewWelcome to the Mailjet official NodeJs API wrapper! Check out all the resources and NodeJs code examples in the official Mailjet Documentation. Table of contents
CompatibilityThis library officially supports the following Node.js versions:
InstallationFirst, create a project folder:
Then use the following code to install the wrapper:
If you want to do a global installation, add the AuthenticationThe 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'
Initialize your Mailjet Client: const mailjet = require ('node-mailjet')
.connect(process.env.MJ_APIKEY_PUBLIC, process.env.MJ_APIKEY_PRIVATE) Make your first callHere'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 specificsTo 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})
OptionsAPI VersioningThe Mailjet API is spread among three distinct versions:
Since most Email API endpoints are located under 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 URLThe default base domain name for the Mailjet API is api.mailjet.com. You can modify this base URL by setting a value for 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 Request timeoutYou are able to set a timeout for your request using the const request = mailjet
.post("send", {'version': 'v3.1', 'timeout': 100}) Use proxyThe const request = mailjet
.post("send", {'version': 'v3.1', 'proxyUrl': 'YOUR_PROXY_URL'}) The proxy URL is passed directly to superagent-proxy. Disable API callBy 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 const request = mailjet
.post("send", {'version': 'v3.1', 'perform_api_call': false}) Request examplesPOST RequestUse the const request = mailjet
.post($RESOURCE, {$OPTIONS})
.id($ID)
.request({$PARAMS})
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 RequestUse the const request = mailjet
.get($RESOURCE, {$OPTIONS})
.id($ID)
.request({$PARAMS})
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 RequestUse the const request = mailjet
.put($RESOURCE, {$OPTIONS})
.id($ID)
.request({$PARAMS}) You need to define A 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 RequestUse the const request = mailjet
.delete($RESOURCE, {$OPTIONS})
.id($ID)
.request() You need to define Upon a successful Here's an example of a /**
*
* 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 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论