@activeprospect/sailthru-client 中文文档教程

发布于 3年前 浏览 17 项目主页 更新于 3年前

sailthru-node-client

有关安装说明、文档和示例,请访问: http://getstarted.sailthru.com/new-for- developers-overview/api-client-library/node-js-npm

一个简单的客户端库,用于根据 http://getstarted.sailthru.com/new-for-developers-overview/api/api-overview/

默认,它将以 JSON 格式发出请求。 不支持 XML 格式。

Installation

npm install sailthru-client --save

Examples

Initialization

var apiKey = '******',
    apiSecret = '*****',
    sailthru = require('sailthru-client').createSailthruClient(apiKey, apiSecret);

Getting version

var version = require('sailthru-client').VERSION;

Enable / Disable Logging

sailthru.enableLogging();
sailthru.disableLogging();

Making POST Request

var data = {
    email: 'foo@example.com',
    lists: {
        'list-a': 1
    }
};
sailthru.apiPost('email', data, function(err, response) {
    if (!err) {
        console.log(response);
    } else {
        console.log('Error!');
        console.log(err);
    }
});

Making POST Request with multipart (Eg: Job API call with import type)

// Making import /job API POST call
// MUltipart call
var data = {
    job: 'import',
    list: 'test-list',
    file: './emails.txt'
};
var multipart_params = ['file']; // this is required to mark file as a multipart upload item'
sailthru.apiPost('job', data, multipart_params, function(err, response) {
   console.log(response);
});

Making GET Request

// Making /send API GET call
var send_id = 'TE8EZ3-LmosnAgAA';
sailthru.apiGet('send', {send_id: send_id}, function(err, response) {
    console.log(response);
});

Making DELETE Request

// /send API DELETE call
var send_id = 'TE8EZ3-LmosnAgAA';
sailthru.apiDelete('send', {send_id: send_id}, function(err, response) {
    console.log(response);
});

send

//send
var template = 'my-template',
    email = 'foo@example.com',
    options = {
        'vars': {
            'name': 'Foo Bar',
            'address': 'Queens, NY'
        },
        'options': {
            'test': 1,
            'replyto': 'bar@example.com'
        }
    };
sailthru.send(template, email, options, function(err, response) {
    if (err) {
        console.log("Status Code: " + err.statusCode);
        console.log("Error Code: " + err.error);
        console.log("Error Message: " + err.errormsg);
    } else {
        //process output
    }
});

//multi-send
var emails = ['blah@example.com', 'foo@example.com', 'bar@example.com'],
    template = 'multi-template',
    options = {
        'options': {
            'test': 1
        }
    };
sailthru.multiSend(template, emails, options, function(err, response) {
    if (err) {
        //Process error
    } else {
        //process JSON output
    }
});

Rate Limit Information

该库允许检查 Sailthru API 返回的“X-Rate-Limit-*”标头。 getLastRateLimitInfo(action, method) 函数允许您检索给定操作/方法组合的最后已知速率限制信息。 它必须遵循 API 调用。 例如,如果您执行 /send POST,您可以调用 getLastRateLimitInfo('send', 'POST'),如下所示:

// make API call as normal
sailthru.apiPost('send', {'template': 'my template', 'email': 'foo@example.com'}, function(err, response) {
    if (!err) {
        console.log(response);
    } else {
        console.log('Error!');
        console.log(err);
    }
});

// check rate limit information
var rateLimitInfo = sailthru.getLastRateLimitInfo('send', 'POST');

返回类型如果给定的操作/方法组合没有速率限制信息(例如,如果您尚未向该端点发出请求),则将为 undefined。 否则,它将是以下格式的对象:

{
    limit: 1234, // <Number representing the limit of requests/minute for this action / method combination>
    remaining: 1230, // <Number representing how many requests remain in the current minute>
    reset: 1459381680 // <Number representing the UNIX epoch timestamp of when the next minute starts, and when the rate limit resets>
}

Development

npm install -g grunt-cli
npm install # to install dependencies locally
grunt # for running tests

sailthru-node-client

For installation instructions, documentation, and examples please visit: http://getstarted.sailthru.com/new-for-developers-overview/api-client-library/node-js-npm

A simple client library to remotely access the Sailthru REST API as per http://getstarted.sailthru.com/new-for-developers-overview/api/api-overview/

By default, it will make request in JSON format. XML format is not supported.

Installation

npm install sailthru-client --save

Examples

Initialization

var apiKey = '******',
    apiSecret = '*****',
    sailthru = require('sailthru-client').createSailthruClient(apiKey, apiSecret);

Getting version

var version = require('sailthru-client').VERSION;

Enable / Disable Logging

sailthru.enableLogging();
sailthru.disableLogging();

Making POST Request

var data = {
    email: 'foo@example.com',
    lists: {
        'list-a': 1
    }
};
sailthru.apiPost('email', data, function(err, response) {
    if (!err) {
        console.log(response);
    } else {
        console.log('Error!');
        console.log(err);
    }
});

Making POST Request with multipart (Eg: Job API call with import type)

// Making import /job API POST call
// MUltipart call
var data = {
    job: 'import',
    list: 'test-list',
    file: './emails.txt'
};
var multipart_params = ['file']; // this is required to mark file as a multipart upload item'
sailthru.apiPost('job', data, multipart_params, function(err, response) {
   console.log(response);
});

Making GET Request

// Making /send API GET call
var send_id = 'TE8EZ3-LmosnAgAA';
sailthru.apiGet('send', {send_id: send_id}, function(err, response) {
    console.log(response);
});

Making DELETE Request

// /send API DELETE call
var send_id = 'TE8EZ3-LmosnAgAA';
sailthru.apiDelete('send', {send_id: send_id}, function(err, response) {
    console.log(response);
});

send

//send
var template = 'my-template',
    email = 'foo@example.com',
    options = {
        'vars': {
            'name': 'Foo Bar',
            'address': 'Queens, NY'
        },
        'options': {
            'test': 1,
            'replyto': 'bar@example.com'
        }
    };
sailthru.send(template, email, options, function(err, response) {
    if (err) {
        console.log("Status Code: " + err.statusCode);
        console.log("Error Code: " + err.error);
        console.log("Error Message: " + err.errormsg);
    } else {
        //process output
    }
});

//multi-send
var emails = ['blah@example.com', 'foo@example.com', 'bar@example.com'],
    template = 'multi-template',
    options = {
        'options': {
            'test': 1
        }
    };
sailthru.multiSend(template, emails, options, function(err, response) {
    if (err) {
        //Process error
    } else {
        //process JSON output
    }
});

Rate Limit Information

The library allows inspection of the 'X-Rate-Limit-*' headers returned by the Sailthru API. The getLastRateLimitInfo(action, method) function allows you to retrieve the last known rate limit information for the given action / method combination. It must follow an API call. For example, if you do a /send POST, you can follow up with a call to getLastRateLimitInfo('send', 'POST') as shown below:

// make API call as normal
sailthru.apiPost('send', {'template': 'my template', 'email': 'foo@example.com'}, function(err, response) {
    if (!err) {
        console.log(response);
    } else {
        console.log('Error!');
        console.log(err);
    }
});

// check rate limit information
var rateLimitInfo = sailthru.getLastRateLimitInfo('send', 'POST');

The return type will be undefined if there is no rate limit information for the given action / method combination (e.g. if you have not yet made a request to that endpoint). Otherwise, it will be an object in the following format:

{
    limit: 1234, // <Number representing the limit of requests/minute for this action / method combination>
    remaining: 1230, // <Number representing how many requests remain in the current minute>
    reset: 1459381680 // <Number representing the UNIX epoch timestamp of when the next minute starts, and when the rate limit resets>
}

Development

npm install -g grunt-cli
npm install # to install dependencies locally
grunt # for running tests
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文