@aftership/tracking-sdk

中文文档
17.0.0发布于 2026/07/13ISCTypeScriptCommonJS近 30 天下载 6万

Tracking NodeJS SDK

原始使用文档
文档目录17 节
  1. Table of Contents
  2. Before you begin
  3. API and SDK Version
  4. Quick Start
  5. Installation
  6. Constructor
  7. Example
  8. Rate Limiter
  9. Error Handling
  10. Error List
  11. Endpoints
  12. /trackings
  13. /couriers
  14. /courier-connections
  15. /estimated-delivery-date
  16. Help
  17. License

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 [email protected].

Table of Contents

Before you begin

Before you begin to integrate:

API and SDK Version

  • SDK Version: 17.0.0
  • API Version: 2026-07

Quick Start

Installation

npm install --save @aftership/tracking-sdk

Constructor

Create AfterShip instance with options

NameTypeRequiredDescription
api_keystring✔Your 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: Import 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 query object
const query = {};

// Step 4: Make the request
aftership.tracking
  .getTrackingById(
    "valid_value",

    query,
  )
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

Rate Limiter

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

The API returns its current rate limit status in the headers of every response, and the SDK exposes these headers on both successful responses and rate-limited errors, so you can monitor your consumption proactively instead of waiting for 429 errors.

HeaderDescription
X-RateLimit-LimitThe rate limit ceiling for the current endpoint per second
X-RateLimit-RemainingThe number of requests left for the 1-second window
X-RateLimit-ResetThe Unix timestamp when the rate limit will be reset

Every successful response exposes a response_headers object alongside data (header names are lower-cased). Taking the Quick Start example above:

const remaining = Number(result.response_headers["x-ratelimit-remaining"]);
const resetAt = Number(result.response_headers["x-ratelimit-reset"]);

if (remaining <= 1) {
  // Throttle or defer lower-priority requests until `resetAt`
}

When the rate limit is exceeded, the request fails with a 429 error that carries the same headers — see Error Handling.

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.
response_headersobjectAPI response header.

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_REQUIRED4009400[field_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_REQUIRED4014400notification 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_ERROR500500Something went wrong on AfterShip's end.
INTERNAL_ERROR502502Something went wrong on AfterShip's end.
INTERNAL_ERROR503503Something went wrong on AfterShip's end.
INTERNAL_ERROR504504Something went wrong on AfterShip's end.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |

Endpoints

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

  • tracking
    • Get trackings
    • Create a tracking
    • Get a tracking by ID
    • Update a tracking by ID
    • Delete a tracking by ID
    • Retrack an expired tracking by ID
    • Mark tracking as completed by ID
  • courier
    • Get couriers
    • Detect courier
  • courierConnection
    • Get courier connections
    • Create courier connections
    • Get courier connection by id
    • Update courier connection by id
    • Delete courier connection by id
  • estimatedDeliveryDate
    • Prediction for the Estimated Delivery Date
    • Batch prediction for the Estimated Delivery Date

/trackings

GET /trackings

const query = {};

aftership.tracking
  .getTrackings(query)
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

POST /trackings

const createTrackingRequestBody = {
  tracking_number: "valid_value",
};

aftership.tracking
  .createTracking(createTrackingRequestBody)
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

GET /trackings/{id}

const query = {};

aftership.tracking
  .getTrackingById(
    "valid_value",

    query,
  )
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

PUT /trackings/{id}

const updateTrackingByIdRequestBody = {};

aftership.tracking
  .updateTrackingById("valid_value", updateTrackingByIdRequestBody)
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

DELETE /trackings/{id}

aftership.tracking
  .deleteTrackingById("valid_value")
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

POST /trackings/{id}/retrack

aftership.tracking
  .retrackTrackingById("valid_value")
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

POST /trackings/{id}/mark-as-completed

const markTrackingCompletedByIdRequestBody = {};

aftership.tracking
  .markTrackingCompletedById(
    "valid_value",
    markTrackingCompletedByIdRequestBody,
  )
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

/couriers

GET /couriers

const query = {};

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

POST /couriers/detect

const detectCourierRequestBody = {
  tracking_number: "valid_value",
};

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

/courier-connections

GET /courier-connections

const query = {};

aftership.courierConnection
  .getCourierConnections(query)
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

POST /courier-connections

const postCourierConnectionsRequestBody = {
  courier_slug: "valid_value",
  credentials: {},
};

aftership.courierConnection
  .postCourierConnections(postCourierConnectionsRequestBody)
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

GET /courier-connections/{id}

aftership.courierConnection
  .getCourierConnectionsById("valid_value")
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

PATCH /courier-connections/{id}

const putCourierConnectionsByIdRequestBody = {
  credentials: {},
};

aftership.courierConnection
  .putCourierConnectionsById(
    "valid_value",
    putCourierConnectionsByIdRequestBody,
  )
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

DELETE /courier-connections/{id}

aftership.courierConnection
  .deleteCourierConnectionsById("valid_value")
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

/estimated-delivery-date

POST /estimated-delivery-date/predict

const predictRequestBody = {
  slug: "valid_value",
  origin_address: {}, // EstimatedDeliveryDateRequestOriginAddress
  destination_address: {}, // EstimatedDeliveryDateRequestDestinationAddress
};

aftership.estimatedDeliveryDate
  .predict(predictRequestBody)
  .then((result) => console.log(result))
  .catch((e) => console.log(e));

POST /estimated-delivery-date/predict-batch

const predictBatchRequestBody = {};

aftership.estimatedDeliveryDate
  .predictBatch(predictBatchRequestBody)
  .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 [email protected]

License

Copyright (c) 2025 AfterShip

Licensed under the MIT license.

版本与发布记录

@aftership/tracking-sdk 共发布 16 个版本,首次发布于 2024/06/20;过去 12 个月发布了 3 个版本。

版本发布日期距今
17.0.0latest2026/07/133 个月前
16.0.02026/06/094 个月前
15.0.12025/11/1211 个月前
15.0.02025/10/211 年前
14.0.02025/07/221.3 年前
13.0.02025/04/231.5 年前

关于 @aftership/tracking-sdk 的常见问题

@aftership/tracking-sdk 是什么?

Tracking NodeJS SDK

如何安装 @aftership/tracking-sdk?

在项目目录运行 npm install @aftership/tracking-sdk,或使用 pnpm add / yarn add / bun add。

@aftership/tracking-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aftership/tracking-sdk 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aftership/tracking-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.20.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aftership/tracking-sdk 可以商用吗?

@aftership/tracking-sdk 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aftership/tracking-sdk 的下载量有多少?

根据 npm 官方下载统计,@aftership/tracking-sdk 在 2026/10/05 – 2026/11/03 这 30 天内被下载 60,375 次,日均约 2,013 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aftership/tracking-sdk 还在维护吗?

最新版本 17.0.0 发布于 2026/07/13(3 个月前);至今共发布 16 个版本,其中过去 12 个月发布了 3 个;首次发布于 2024/06/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。