11.0.2 • Published 9 months ago

@aftership/tracking-sdk v11.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

AfterShip Tracking API library for Node.js

This library allows you to quickly and easily use the AfterShip Tracking API via Node.js.

For updates to this library, see our GitHub release page.

If you need support using AfterShip products, please contact support@aftership.com.

Table of Contents

Before you begin

Before you begin to integrate:

API and SDK Version

Each SDK version is designed to work with a specific API version. Please refer to the table below to identify the supported API versions for each SDK version, ensuring you select the appropriate SDK version for the API version you intend to use.

SDK VersionSupported API VersionBranch
11.x.x2024-10https://github.com/AfterShip/tracking-sdk-nodejs/tree/2024-10
10.x.x2024-07https://github.com/AfterShip/tracking-sdk-nodejs/tree/2024-07
9.x.x2024-04https://github.com/AfterShip/tracking-sdk-nodejs/tree/2024-04
8.x.x2023-10https://github.com/AfterShip/aftership-sdk-nodejs
<=7.x.xLegacy APIhttps://github.com/AfterShip/aftership-sdk-nodejs

Quick Start

Installation

npm install --save @aftership/tracking-sdk

Constructor

Create AfterShip instance with options

NameTypeRequiredDescription
api_keystringYour AfterShip API key
auth_typeenumDefault value: AuthType.API_KEY AES authentication: AuthType.AES RSA authentication: AuthType.RSA
api_secretstringRequired if the authentication type is AuthType.AES or AuthType.RSA
domainstringAfterShip API domain. Default value: https://api.aftership.com
user_agentstringUser-defined user-agent string, please follow RFC9110 format standard.
proxystringHTTP proxy URL to use for requests. Default value: null Example: http://192.168.0.100:8888
max_retrynumberNumber of retries for each request. Default value: 2. Min is 0, Max is 10.
timeoutnumberTimeout for each request in milliseconds.

Example

// Step 1: Require the AfterShip client
import {AfterShip} from '@aftership/tracking-sdk';
// or
// const {AfterShip} = require('@aftership/tracking-sdk');


// Step 2: Initialize the client object
const aftership = new AfterShip({api_key: 'YOUR_API_KEY'});

// Step 3: Create the request object
const createTrackingRequestBody = {
    tracking_number: '<tracking_number>',
    slug: '<slug>'
};

// Step 4: Make the request
aftership.tracking.createTracking(createTrackingRequestBody)
    .then(tracking => console.log(tracking))
    .catch(error => console.log(error));

Rate Limiter

See the Rate Limit to understand the AfterShip rate limit policy.

Error Handling

The SDK will return an error object when there is any error during the request, with the following specification:

NameTypeDescription
messagestringDetail message of the error
codeenumError code enum for API Error.
meta_codenumberAPI response meta code.
status_codenumberHTTP status code.
response_bodystringAPI response body.

Error List

codemeta_codestatus_codemessage
INVALID_REQUEST400400The request was invalid or cannot be otherwise served.
INVALID_JSON4001400Invalid JSON data.
TRACKING_ALREADY_EXIST4003400Tracking already exists.
TRACKING_DOES_NOT_EXIST4004404Tracking does not exist.
TRACKING_NUMBER_INVALID4005400The value of tracking_number is invalid.
TRACKING_REQUIRED4006400tracking object is required.
TRACKING_NUMBER_REQUIRED4007400tracking_number is required.
VALUE_INVALID4008400The value of field_name is invalid.
VALUE_REQUIRED4009400field_name is required.
SLUG_INVALID4010400The value of slug is invalid.
MISSING_OR_INVALID_REQUIRED_FIELD4011400Missing or invalid value of the required fields for this courier. Besides tracking_number, also required: field_name
BAD_COURIER4012400The error message will be one of the following:1. Unable to import shipment as the carrier is not on your approved list for carrier auto-detection. Add the carrier here: https://admin.aftership.com/settings/couriers2. Unable to import shipment as we don’t recognize the carrier from this tracking number.3. Unable to import shipment as the tracking number has an invalid format.4. Unable to import shipment as this carrier is no longer supported.5. Unable to import shipment as the tracking number does not belong to a carrier in that group.
INACTIVE_RETRACK_NOT_ALLOWED4013400Retrack is not allowed. You can only retrack an inactive tracking.
NOTIFICATION_REUQIRED4014400notification object is required.
ID_INVALID4015400The value of id is invalid.
RETRACK_ONCE_ALLOWED4016400Retrack is not allowed. You can only retrack each shipment once.
TRACKING_NUMBER_FORMAT_INVALID4017400The format of tracking_number is invalid.
API_KEY_INVALID401401The API key is invalid.
REQUEST_NOT_ALLOWED403403The request is understood, but it has been refused or access is not allowed.
NOT_FOUND404404The URI requested is invalid or the resource requested does not exist.
TOO_MANY_REQUEST429429You have exceeded the API call rate limit. The default limit is 10 requests per second.
INTERNAL_ERROR500 502 503 504500 502 503 504Something went wrong on AfterShip's end.

Endpoints

The AfterShip instance has the following properties which are exactly the same as the API endpoints:

  • courier - Get a list of our supported couriers.
  • tracking - Create trackings, update trackings, and get tracking results.
  • estimated-delivery-date - Get estimated delivery date for your order.

/trackings

POST /trackings

const payload = {
    slug: '<slug>',
    tracking_number: '<tracking_number>',
    title: 'Title Name',
    smses: [
        '+18555072509',
        '+18555072501'
    ],
    emails: [
        '<your_email>',
        '<your_email>'
    ],
    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/:id

aftership.tracking
    .deleteTrackingById('<tracking_id>')
    .then((result) => console.log(result))
    .catch((e) => console.log(e));

GET /trackings

aftership.tracking
    .getTrackings({ limit: 10, fields: 'slug,tracking_number' })
    .then((result) => console.log(result))
    .catch((e) => console.log(e));

GET /trackings/:id

aftership.tracking
    .getTrackingById('<tracking_id>')
    .then((result) => console.log(result))
    .catch((e) => console.log(e));

PUT /trackings/:id

aftership.tracking
    .updateTrackingById('<tracking_id>',{title: 'New Title123'})
    .then((result) => console.log(result))
    .catch((e) => console.log(e));

POST /trackings/:id/retrack

aftership.tracking
    .retrackTrackingById('<tracking_id>')
    .then((result) => console.log(result))
    .catch((e) => console.log(e));

POST /trackings/:id/mark-as-completed

aftership.tracking
    .markTrackingCompletedById('<tracking_id>', { reason: 'DELIVERED' })
    .then((result) => console.log(result))
    .catch((e) => console.log(e));

/couriers

GET /couriers

aftership.courier
    .getUserCouriers()
    .then(result => console.log(result))
    .catch(e => console.log(e));

GET /couriers/all

aftership.courier
    .getAllCouriers()
    .then(result => console.log(result))
    .catch(e => console.log(e));

POST /couriers/detect

aftership.courier
    .detectCourier({ tracking_number: '<tracking_number>' })
    .then(result => console.log(result))
    .catch(e => console.log(e));

/estimated-delivery-date

POST /estimated-delivery-date/predict-batch

const payload = {
    estimated_delivery_dates: [{
        slug: '<slug>',
        origin_address: { country: '<country>' },
        destination_address: { country: '<country>' },
    }]
};
aftership.estimatedDeliveryDate
    .predictBatch(payload)
    .then(result => console.log(result))
    .catch(e => console.log(e));

Help

If you get stuck, we're here to help:

  • Issue Tracker for questions, feature requests, bug reports and general discussion related to this package. Try searching before you create a new issue.
  • Contact AfterShip official support via support@aftership.com

License

Copyright (c) 2024 AfterShip

Licensed under the MIT license.

11.0.2

9 months ago

10.0.2

9 months ago

9.0.3

9 months ago

10.0.1

12 months ago

9.0.2

1 year ago

9.0.1

1 year ago

9.0.0

1 year ago