Node.js (npm)

View on Github | Download

Initialization

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

Get Version

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

Enable / Disable Logging

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

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);
    }
});

POST Request with Multipart

(e.g.: 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);
});

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);
});

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 Limits

 var lastRateLimitInfo = sailthru.getLastRateLimitInfo('send', 'POST');

// getLastRateLimitInfo returns undefined if given endpoint/method wasn't triggered previously
if (lastRateLimitInfo !== 'undefined') {
    var limit = lastRateLimitInfo['limit'];
    var remaining = lastRateLimitInfo['remaining'];
    var reset_timestamp = lastRateLimitInfo['reset'];

    // throttle api calls based on last rate limit info
    if (remaining <= 0) {
        var seconds_till_reset = reset_timestamp - Math.floor(Data.now() / 1000)
        // sleep or perform other business logic before next api call
        sleep(seconds_till_reset); 
    }
}
Top