node.js SDK for AfterShip API
npm install aftership
const Aftership = require('aftership')('YOUR_API_KEY');
Aftership.call('GET', '/couriers/all', function (err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
// Or do it with promise
Aftership.call('GET', '/couriers/all').then(function (result) {
console.log(result);
}).catch(function (err) {
console.log(err);
});
/** Console
{ meta: { code: 200 },
data:
{ total: 335,
couriers:
[ ... ]
}
}
*/mocha --recursive
- Constructor(api_key, options)
- call(method, path, options, callback)
- Proxy Method
- Rate Limiter
- Retry policy
- Error Handling
- Examples
Create AfterShip instance with options
api_key- Require - your Aftership API keyoptions- Optional - object of request optionsendpoint- string, AfterShip endpoint, default 'https://api.aftership.com/v4'proxy- string, proxy, default isnullretry- boolean, retry if fail? default istrue, see Retry policyrate- boolean, retry if response with429 Too Many request errorsee Rate Limiteruser_agent_prefix- string, prefix of User-Agent in headers, default 'aftership-sdk-nodejs'
Example:
// Construct with options
const Aftership = require('aftership')('YOUR_API_KEY', {
endpoint: 'https://api.aftership.com/OLDER_VERSION',
proxy: 'http://username:password@my.proxy.com',
retry: false
});
// Make your call
Aftership.call('GET', '/couriers/all', function (err, result) {
// ...
});Make request with option
method- Require - Eitherget,post,putordelete, case insensitivepath- Require string, start with/, see available path hereoptions- Optional - object of request optionsapi_key- string, AfterShip API key, overridedefault api_keyif setbody- object,POSTbodyquery- object,queryobjectretry- boolean, retry if fail? overridedefault retryif set, see Retry policyraw- boolean, iftrue, return result asstring, else return asobject, default isfalse
callback- the callback to handle error and result, the result is the response body of the request
There are also interface GET, POST, PUT, DELETE which are proxy to Aftership.call(...)
Aftership.call('GET', '/path', options, callback);
// is equivalent to
Aftership.GET('/path', options, callback);
// So as `POST`, `PUT` and `DELETE`To understand AfterShip rate limit policy, please see limit session in https://www.aftership.com/docs/api/4
You can get the recent rate limit by aftership.rate_limit. Initially all value are null.
const Aftership = require('aftership')('YOUR_API_KEY');
console.log(Aftership.rate_limit);
// console output
// { 'YOUR_API-KEY' : { reset: null, limit: null, remaining: null } }After making an API call, it will be set.
Aftership.call('GET', '/couriers', function (err, result) {
console.log(Aftership.rate_limit);
});
// console output
// { 'YOUR_API-KEY' : { limit: 600, remaining: 599, reset: 1453281417 } }When the API response with 429 Too Many request error
- if
rateistrue, it wont throw, will delay the job, retry when the rate limit is reset. - if
rateisfalse, it will return429 Too Many request errorto the callback
To understand error of AfterShip, please see https://www.aftership.com/docs/api/4/errors
For this SDK, errors below are retriable.
HTTP status code == [500, 502, 503, 504]from APIETIMEDOUT/ECONNRESET/ECONNREFUSEDfrom node.js
You can set the retry flag
- in constructor as default
retryflag - specify in
optionsofcall()method
When an retriable error comes,
- if
retryistrue,- if
retry_count < 5, use the same payload to retry after 1 second - if
retry_count == 5, return the error to the callback, withretry_count
- if
- if
retryisfalse, return the error to the callback
There are 3 kinds of error
- SDK Error
- Request Error
- API Error
Error object of this SDK contain fields:
type- Require - type of the error, please handle each error by this fieldmessage- Optional - detail message of the errordata- Optional - data lead to the errorcode- Optional - error code for API Errorresponse_body- Optional - response body of API Errorretry_count- Optional - number of retry, if the error is retriable
Please handle each error by its
type, since it is a require field
Error return by the SDK instance, mostly invalid param type when calling constructor or call function
error.type is one of error_enum
Throw by the SDK instance
const Aftership = require('aftership')('YOUR_API_KEY');
try {
Aftership.call('INVALID_METHOD', '/couriers/all', function (err, result) {
// ...
});
} catch (e) {
console.log(e);
}
/*
{ [Error: HandlerError: Invalid Method value]
type: 'HandlerError',
message: 'HandlerError: Invalid Method value',
data: 'INVALID_METHOD'
... }
*/Error return by the request module
error.type could be ETIMEDOUT, ECONNRESET, etc.
Catch by promise or Pass through the first param of callback
const Aftership = require('aftership')('YOUR_API_KEY');
Aftership.call('GET', '/couriers/all', function (err, result) {
if (err) {
console.log(err);
}
});
/*
{ [Error]
code: 'ECONNRESET',
type: 'ECONNRESET'
... }
*/Error return by the Aftership API
error.type shuold be same as https://www.aftership.com/docs/api/4/errors
Catch by promise or Pass through the first param of callback
const Aftership = require('aftership')('INVALID_API_KEY');
Aftership.call('GET', '/couriers/all', function (err, result) {
if (err) {
console.log(err);
}
});
/*
{ [Error: Invalid API key.]
type: 'Unauthorized',
message: 'Invalid API key.',
code: 401,
data: {},
response_body: '{"meta":{"code":401,"message":"Invalid API key.","type":"Unauthorized"},"data":{}}' }
*/GET /couriers
Aftership.call('GET', '/couriers', function (err, result) {
// Your code here
});GET /couriers/all
Aftership.call('GET', '/couriers/all', function (err, result) {
// Your code here
});POST /couriers/detect
let body = {
'tracking': {
'tracking_number': '906587618687',
'tracking_postal_code': 'DA15BU',
'tracking_ship_date': '20131231',
'tracking_account_number': '1234567890',
'slug': ['dhl', 'ups', 'fedex']
}
};
Aftership.call('POST', '/couriers/detect', {
body: body
}, function (err, result) {
// Your code here
});POST /trackings
let body = {
'tracking': {
'slug': 'dhl',
'tracking_number': '123456789',
'title': 'Title Name',
'smses': [
'+18555072509',
'+18555072501'
],
'emails': [
'email@yourdomain.com',
'another_email@yourdomain.com'
],
'order_id': 'ID 1234',
'order_id_path': 'http://www.aftership.com/order_id=1234',
'custom_fields': {
'product_name': 'iPhone Case',
'product_price': 'USD19.99'
}
}
};
Aftership.call('POST', '/trackings', {
body: body
}, function (err, result) {
// Your code here
});DELETE /trackings/:slug/:tracking_number
Aftership.call('DELETE', '/trackings/ups/1234567890', function (err, result) {
// Your code here
});GET /trackings
let query = {
slug: 'dhl,ups,usps'
};
Aftership.call('GET', '/trackings', {
query: query
}, function (err, result) {
// Your code here
});GET /trackings/exports
Aftership.call('GET', '/trackings/exports', function (err, result) {
// Your code here
});GET /trackings/:slug/:tracking_number
Aftership.call('GET', '/trackings/ups/1234567890', function (err, result) {
// Your code here
});GET /trackings/:slug/:tracking_number
let body = {
'tracking': {
'title': 'New Title'
}
};
Aftership.call('PUT', '/trackings/ups/1234567890', {
body: body
}, function (err, result) {
// Your code here
});POST /trackings/:slug/:tracking_number/retrack
Aftership.call('POST', '/trackings/ups/1234567890/retrack', function (err, result) {
// Your code here
});GET /last_checkpoint/:slug/:tracking_number
Aftership.call('GET', '/last_checkpoint/ups/1234567890', function (err, result) {
// Your code here
});GET /notifications/:slug/:tracking_number
Aftership.call('GET', '/notifications/ups/1234567890', function (err, result) {
// Your code here
});POST /notifications/:slug/:tracking_number/add
let body = {
'notification': {
'emails': ['user1@gmail.com','user2@gmail.com','invalid EMail @ Gmail. com'],
'smses': ['+85291239123', '+85261236123', 'Invalid Mobile Phone Number']
}
};
Aftership.call('POST', '/notifications/ups/1234567890/add', {
body: body
}, function (err, result) {
// Your code here
});POST /notifications/:slug/:tracking_number/remove
let body = {
'notification': {
'emails': ['user1@gmail.com','user2@gmail.com','invalid EMail @ Gmail. com'],
'smses': ['+85291239123', '+85261236123', 'Invalid Mobile Phone Number']
}
};
Aftership.call('POST', '/notifications/ups/1234567890/remove', {
body: body
}, function (err, result) {
// Your code here
});Copyright (c) 2016 AfterShip
Licensed under the MIT license.