aftership-sdk-node
node.js SDK for AfterShip API
Installation
npm install aftership
Quick Start
const Aftership = require('aftership')('YOUR_API_KEY');
Aftership.call('GET', '/couriers/all', function (err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
Aftership.call('GET', '/couriers/all').then(function (result) {
console.log(result);
}).catch(function (err) {
console.log(err);
});
Test
mocha --recursive
Table of contents
Constructor(api_key, options)
Create AfterShip instance with options
api_key
- Require - your Aftership API keyoptions
- Optional - object of request options
endpoint
- string, AfterShip endpoint, default 'https://api.aftership.com/v4'proxy
- string, proxy, default is null
retry
- boolean, retry if fail? default is true
, see Retry policyrate
- boolean, retry if response with 429 Too Many request error
see Rate Limiter
Example:
const Aftership = require('aftership')('YOUR_API_KEY', {
endpoint: 'https://api.aftership.com/OLDER_VERSION',
proxy: 'http://username:password@my.proxy.com',
retry: false
});
Aftership.call('GET', '/couriers/all', function (err, result) {
});
call(method, path, options, callback)
Make request with option
method
- Require - Either get
, post
, put
or delete
, case insensitivepath
- Require string, start with /
, see available path hereoptions
- Optional - object of request options
api_key
- string, AfterShip API key, override default api_key
if setbody
- object, POST
bodyquery
- object, query
objectretry
- boolean, retry if fail? override default retry
if set, see Retry policyraw
- boolean, if true
, return result as string
, else return as object
, default is false
callback
- the callback to handle error and result, the result is the response body of the request
Proxy Method (GET, POST, PUT, DELETE)
There are also interface GET
, POST
, PUT
, DELETE
which are proxy to Aftership.call(...)
Aftership.call('GET', '/path', options, callback);
Aftership.GET('/path', options, callback);
Rate Limiter
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);
After making an API call, it will be set.
Aftership.call('GET', '/couriers', function (err, result) {
console.log(Aftership.rate_limit);
});
When the API response with 429 Too Many request error
- if
rate
is true
, it wont throw, will delay the job, retry when the rate limit is reset. - if
rate
is false
, it will return 429 Too Many request error
to the callback
Retry policy
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
/ECONNREFUSED
from node.js
You can set the retry
flag
- in constructor as default
retry
flag - specify in
options
of call()
method
When an retriable error comes,
- if
retry
is true
,
- if
retry_count < 5
, use the same payload to retry after 1 second - if
retry_count == 5
, return the error to the callback, with retry_count
- if
retry
is false
, return the error to the callback
Error Handling
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
SDK Error
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);
}
Request Error
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);
}
});
API Error
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);
}
});
Examples
/couriers
GET /couriers
Aftership.call('GET', '/couriers', function (err, result) {
});
GET /couriers/all
Aftership.call('GET', '/couriers/all', function (err, result) {
});
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', {
body: body
}, function (err, result) {
});
/trackings
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) {
});
DELETE /trackings/:slug/:tracking_number
Aftership.call('DELETE', '/trackings/ups/1234567890', function (err, result) {
});
GET /trackings
let query = {
slug: 'dhl,ups,usps'
};
Aftership.call('GET', '/trackings', {
query: query
}, function (err, result) {
});
GET /trackings/exports
Aftership.call('GET', '/trackings/exports', function (err, result) {
});
GET /trackings/:slug/:tracking_number
Aftership.call('GET', '/trackings/ups/1234567890', function (err, result) {
});
GET /trackings/:slug/:tracking_number
let body = {
'tracking': {
'title': 'New Title'
}
};
Aftership.call('PUT', '/trackings/ups/1234567890', {
body: body
}, function (err, result) {
});
POST /trackings/:slug/:tracking_number/retrack
Aftership.call('POST', '/trackings/ups/1234567890/retrack', function (err, result) {
});
/last_checkpoint
GET /last_checkpoint/:slug/:tracking_number
Aftership.call('GET', '/last_checkpoint/ups/1234567890', function (err, result) {
});
/notifications
GET /notifications/:slug/:tracking_number
Aftership.call('GET', '/notifications/ups/1234567890', function (err, result) {
});
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) {
});
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) {
});
License
Copyright (c) 2016 AfterShip
Licensed under the MIT license.