Research on how to integrate Nod
2018-08-24 本文已影响12人
一个废人
Today we did the research on how to integrate Node.js with Mailchimp RESTful API.
We've created a account and got a API_KEY:07387b34ddfakea3e4511b60f3e0e-us19
Here is the sample code to get the members in a specified list:
//index.js
const https = require('https');
const fs = require('fs');
const listId = '2c51d1ee79'
const options = {
hostname: 'us19.api.mailchimp.com',
path: `/3.0/lists/${listId}/members`,
method: 'GET',
headers: {
'Authorization': 'Basic 07387b34ddfakea3e4511b60f3e0e-us19',
'Content-Type': 'application/json',
},
};
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
fs.writeFile('message.js', d, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
Then executed the nodejs file,
$ node index.js
and got:
{
"members": [{
"id": "68396cc53fdf846b547bbc195782e63f",
"email_address": "jiadong@pandu.ie",
"unique_email_id": "bcbafb7e4f",
"email_type": "html",
"status": "subscribed",
"merge_fields": {
"FNAME": "jiadong",
"LNAME": "yu",
"ADDRESS": {
"addr1": "",
"addr2": "",
"city": "",
"state": "",
"zip": "",
"country": "US"
},
"PHONE": "",
"BIRTHDAY": ""
},
"stats": {
"avg_open_rate": 0,
"avg_click_rate": 0
},
"ip_signup": "",
"timestamp_signup": "",
"ip_opt": "95.169.19.13",
"timestamp_opt": "2018-08-24T06:35:06+00:00",
"member_rating": 2,
"last_changed": "2018-08-24T06:35:06+00:00",
"language": "zh_CN",
"vip": false,
"email_client": "",
"location": {
"latitude": 0,
"longitude": 0,
"gmtoff": 0,
"dstoff": 0,
"country_code": "",
"timezone": ""
},
"tags_count": 0,
"tags": [],
"list_id": "2c51d1ee79",
"_links": [{
"rel": "self",
"href": "https://us19.api.mailchimp.com/3.0/lists/2c51d1ee79/members/68396cc53fdf846b547bbc195782e63f",
"method": "GET",
"targetSchema": "https://us19.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json"
}, {
"rel": "parent",
"href": "https://us19.api.mailchimp.com/3.0/lists/2c51d1ee79/members",
"method": "GET",
"targetSchema": "https://us19.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/CollectionResponse.json",
"schema": "https://us19.api.mailchimp.com/schema/3.0/CollectionLinks/Lists/Members.json"
}, {
"rel": "update",
"href": "https://us19.api.mailchimp.com/3.0/lists/2c51d1ee79/members/68396cc53fdf846b547bbc195782e63f",
"method": "PATCH",
"targetSchema": "https://us19.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json",
"schema": "https://us19.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/PATCH.json"
}, {
"rel": "upsert",
"href": "https://us19.api.mailchimp.com/3.0/lists/2c51d1ee79/members/68396cc53fdf846b547bbc195782e63f",
"method": "PUT",
"targetSchema": "https://us19.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Response.json",
"schema": "https://us19.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/PUT.json"
}, {
"rel": "delete",
"href": "https://us19.api.mailchimp.com/3.0/lists/2c51d1ee79/members/68396cc53fdf846b547bbc195782e63f",
"method": "DELETE"
}, {
"rel": "activity",
"href": "https://us19.api.mailchimp.com/3.0/lists/2c51d1ee79/members/68396cc53fdf846b547bbc195782e63f/activity",
"method": "GET",
"targetSchema": "https://us19.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Activity/Response.json"
}, {
"rel": "goals",
"href": "https://us19.api.mailchimp.com/3.0/lists/2c51d1ee79/members/68396cc53fdf846b547bbc195782e63f/goals",
"method": "GET",
"targetSchema": "https://us19.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Goals/Response.json"
}, {
"rel": "notes",
"href": "https://us19.api.mailchimp.com/3.0/lists/2c51d1ee79/members/68396cc53fdf846b547bbc195782e63f/notes",
"method": "GET",
"targetSchema": "https://us19.api.mailchimp.com/schema/3.0/Definitions/Lists/Members/Notes/CollectionResponse.json"
}, {
"rel": "delete_permanent",
"href": "https://us19.api.mailchimp.com/3.0/lists/2c51d1ee79/members/68396cc53fdf846b547bbc195782e63f/actions/delete-permanent",
"method": "POST"
}]
}]
}
// Omitted the followings
This is all about the GET method, and we can
Add a new list member by POST /lists/{list_id}/members ,
Update a list member by PATCH /lists/{list_id}/members/{subscriber_hash}
the API documentation is here: https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#%20
We'll do a further research on it.