Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
node.js SDK for AfterShip API
npm install aftership
const { AfterShip } = require('aftership');
const aftership = new AfterShip('YOUR_API_KEY');
aftership.courier.listAllCouriers()
.then(result => console.log(result))
.catch(err => console.log(err));
/** Console
{ total: 335,
couriers:
[ ... ]
}
*/
npm run test
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'user_agent_prefix
- string, prefix of User-Agent in headers, default 'aftership-sdk-nodejs'Example:
// Construct with options
const aftership = new AfterShip('YOUR_API_KEY', {
endpoint: 'https://api.aftership.com/OLDER_VERSION'
});
The AfterShip instance has the following properties which are exactly the same as the API endpoins:
courier
- Get a list of our supported couriers.tracking
- Create trackings, update trackings, and get tracking results.last_checkpoint
- Get tracking information of the last checkpoint of a tracking.notification
- Get, add or remove contacts (sms or email) to be notified when the status of a tracking has changed.Make request in a specific endpoint
// GET /trackings/:slug/:tracking_number
aftership.tracking
.getTracking({
slug: "ups",
tracking_number: "1234567890",
})
.then(result => console.log(result))
.catch(err => console.log(err));
To understand AfterShip rate limit policy, please see limit
section 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');
const aftership = new AfterShip('YOUR_API_KEY');
console.log(aftership.rate_limit);
// console output
// { reset: null, limit: null, remaining: null }
After making an API call, it will be set.
aftership.courier.listCouriers()
.then(result => {
console.log(result);
console.log(aftership.rate_limit);
})
.catch(err => console.log(err));
// console output
// { limit: 600, remaining: 599, reset: 1453281417 }
There are 3 kinds of 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 ErrorPlease 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 endpoint method
error.type
is one of error_enum
Throw by the SDK instance
const { AfterShip } = require('aftership');
const aftership = new AfterShip('YOUR_API_KEY');
// GET /notifications/:slug/:tracking_number
aftership.notification.getNotification()
.then(result => console.log(result))
.catch(err => console.log(err));
/*
{ Error: HandlerError: You must specify the id or slug and tracking number
type: 'HandlerError',
code: '',
data: undefined,
responseBody: '' }
*/
Error return by the request
module
error.type
could be ETIMEDOUT
, ECONNRESET
, etc.
Catch by promise
const { AfterShip } = require('aftership');
const aftership = new AfterShip('YOUR_API_KEY');
aftership.courier.listCouriers()
.then(result => console.log(result))
.catch(err => console.log(err));
/*
{ Error: getaddrinfo ENOTFOUND api.aftership.com api.aftership.com:443
type: 'ENOTFOUND',
code: 'ENOTFOUND',
... }
*/
Error return by the Aftership API
error.type
shuold be same as https://www.aftership.com/docs/api/4/errors
Catch by promise
const { AfterShip } = require('aftership');
const aftership = new AfterShip('INVALID_API_KEY');
aftership.courier.listCouriers()
.then(result => console.log(result))
.catch(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.courier.listCouriers()
.then(result => console.log(result))
.catch(e => console.log(e));
GET /couriers/all
aftership.courier.listAllCouriers()
.then(result => console.log(result))
.catch(e => console.log(e));
POST /couriers/detect
const payload = {
'tracking': {
'tracking_number': '906587618687',
'tracking_postal_code': 'DA15BU',
'tracking_ship_date': '20131231',
'tracking_account_number': '1234567890',
'slug': ['dhl', 'ups', 'fedex']
}
};
aftership.courier.detectCouriers(payload)
.then(result => console.log(result))
.catch(e => console.log(e));
POST /trackings
const payload = {
'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.tracking
.createTracking(payload)
.then((result) => console.log(result))
.catch((e) => console.log(e));
DELETE /trackings/:slug/:tracking_number
aftership.tracking
.deleteTracking({
slug: "ups",
tracking_number: "1234567890",
})
.then((result) => console.log(result))
.catch((e) => console.log(e));
GET /trackings
const query = {
slug: 'dhl,ups,usps'
};
aftership.tracking
.listTrackings(query)
.then((result) => console.log(result))
.catch((e) => console.log(e));
GET /trackings/:slug/:tracking_number
aftership.tracking
.getTracking({
slug: "ups",
tracking_number: "1234567890",
})
.then((result) => console.log(result))
.catch((e) => console.log(e));
Tip: You can also add optional_parameters
to /:slug/:tracking_number
// GET /trackings/:slug/:tracking_number?tracking_postal_code=:postal_code&tracking_ship_date=:ship_date
aftership.tracking
.getTracking({
slug: "ups",
tracking_number: "1234567890",
optional_parameters: {
tracking_postal_code: "1234",
tracking_ship_date: "20200423",
}
})
.then((result) => console.log(result))
.catch((e) => console.log(e));
Pro Tip: You can always use /:id to replace /:slug/:tracking_number.
// GET /trackings/:id
aftership.tracking
.getTracking({
id: "1234567890",
})
.then((result) => console.log(result))
.catch((e) => console.log(e));
PUT /trackings/:slug/:tracking_number
const payload = {
tracking: {
title: "New Title",
},
};
aftership.tracking
.updateTracking({
slug: "ups",
tracking_number: "1234567890",
}, payload)
.then((result) => console.log(result))
.catch((e) => console.log(e));
POST /trackings/:slug/:tracking_number/retrack
aftership.tracking
.retrack({
slug: "ups",
tracking_number: "1234567890",
})
.then((result) => console.log(result))
.catch((e) => console.log(e));
POST /trackings/:slug/:tracking_number/mark-as-completed
aftership.tracking
.markAsCompleted({
slug: "ups",
tracking_number: "1234567890",
}, {
reason: "DELIVERED"
})
.then((result) => console.log(result))
.catch((e) => console.log(e));
GET /last_checkpoint/:slug/:tracking_number
aftership.last_checkpoint.getLastCheckpoint({
slug: 'ups',
tracking_number: '1234567890',
})
.then(result => console.log(result))
.catch(e => console.log(e));
GET /notifications/:slug/:tracking_number
aftership.notification.getNotification({
slug: 'ups',
tracking_number: '1234567890',
})
.then(result => console.log(result))
.catch(e => console.log(e));
POST /notifications/:slug/:tracking_number/add
const payload = {
'notification': {
'emails': ['user1@gmail.com','user2@gmail.com','invalid EMail @ Gmail. com'],
'smses': ['+85291239123', '+85261236123', 'Invalid Mobile Phone Number']
}
};
aftership.notification.addNotification({
slug: 'ups',
tracking_number: '1234567890',
}, payload)
.then(result => console.log(result))
.catch(e => console.log(e));
POST /notifications/:slug/:tracking_number/remove
const payload = {
'notification': {
'emails': ['user1@gmail.com','user2@gmail.com','invalid EMail @ Gmail. com'],
'smses': ['+85291239123', '+85261236123', 'Invalid Mobile Phone Number']
}
};
aftership.notification.removeNotification({
slug: 'ups',
tracking_number: '1234567890',
}, payload)
.then(result => console.log(result))
.catch(e => console.log(e));
// v5 (old version)
const Aftership = require('aftership')('YOUR_API_KEY');
Aftership.call('GET', '/couriers/all').then(function (result) {
console.log(result);
}).catch(function (err) {
console.log(err);
});
// v6 (new version)
const { AfterShip } = require('aftership');
const aftership = new AfterShip('YOUR_API_KEY');
aftership.courier.listAllCouriers()
.then(result => console.log(result))
.catch(err => console.log(err));
If you get stuck, we're here to help. The following are the best ways to get assistance working through your issue:
For details on contributing to this repository, see the contributing guide.
Copyright (c) 2016-2020 AfterShip
Licensed under the MIT license.
[7.0.2] 2021-10-26
FAQs
node.js SDK for AfterShip API
We found that aftership demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.