Skip to main content

51tracking-sdk-python

The Python SDK of 51Tracking API

Contact: service@51tracking.org

Official document

Document

Supported Python Versions

  • 3.6
  • 3.7
  • 3.8
  • 3.9
  • 3.10
  • pypy3

Index

  1. Installation
  2. Testing
  3. Error Handling
  4. SDK
    1. Couriers
    2. Trackings
    3. Air Waybill

Installation

$ pip install 51tracking

Via source code

下载代码压缩包,无需解压缩,进入源代码根目录,然后运行:

$ pip install 51tracking-sdk-python.zip

Quick Start

import tracking51

tracking51.api_key = 'you api key'

try:
  couriers = tracking51.courier.get_all_couriers()
  print(couriers)
except tracking51.exception.Tracking51Exception as ce:
  print(ce)
except Exception as e:
  print("other error:", e)  

Testing

pytest

Error handling

Throw by the new SDK client

import tracking51

tracking51.api_key = ''

try:
   couriers = tracking51.courier.get_all_couriers()
   print(couriers)
except tracking51.exception.Tracking51Exception as ce:
   print(ce)

# API Key is missing

Throw by the parameter validation in function

import tracking51

tracking51.api_key = 'you api key'

try:
   params = {'tracking_number': '','courier_code':'usps'}
   result = tracking51.tracking.create_tracking(params)
   print(result)
except tracking51.exception.Tracking51Exception as ce:
   print(ce)

# Tracking number cannot be empty

Examples

Couriers

返回所有支持的快递公司列表

https://api.51Tracking.com/v4/couriers/all

try:
  result = tracking51.courier.get_all_couriers()
  print(result)
except tracking51.exception.Tracking51Exception as ce:
  print(ce)
except Exception as e:
  print("other error:", e)  

Trackings

单个物流单号实时添加且查询

https://api.51Tracking.com/v4/trackings/create

try:
  params = {'tracking_number': '92612903029511573030094547','courier_code':'usps'}
  result = tracking51.tracking.create_tracking(params)
  print(result)
except tracking51.exception.Tracking51Exception as ce:
  print(ce)
except Exception as e:
  print("other error:", e)  
获取多个物流单号的查询结果

https://api.51Tracking.com/v4/trackings/get

try:
  # Perform queries based on various conditions
  # params = {'tracking_numbers': '92612903029511573030094547', 'courier_code': 'usps'}
  # params = {'tracking_numbers': '92612903029511573030094547,92612903029511573030094548', 'courier_code': 'usps'}
  params = {'created_date_min': '2023-08-23T14:00:00+00:00', 'created_date_max': '2023-08-23T15:04:00+00:00'}
  result = tracking51.tracking.get_tracking_results(params)
  print(result)
except tracking51.exception.Tracking51Exception as ce:
  print(ce)
except Exception as e:
  print("other error:", e)  
添加多个物流单号(一次调用最多创建 40 个物流单号)

https://api.51Tracking.com/v4/trackings/batch

try:
  params = [{'tracking_number': '92612903029511573030094593', 'courier_code': 'usps'},
              {'tracking_number': '92612903029511573030094594', 'courier_code': 'usps'}]
  result = tracking51.tracking.batch_create_trackings(params)
  print(result)
except tracking51.exception.Tracking51Exception as ce:
  print(ce)
except Exception as e:
  print("other error:", e)  
根据ID更新物流信息

https://api.51Tracking.com/v4/trackings/update/{id}

params = {'customer_name': 'New name', 'note': 'New tests order note'}
id_string = "9a2f732e29b5ed2071d4cf6b5f4a3d19"
try:
  result = tracking51.tracking.update_tracking_by_id(id_string, params)
  print(result)
except tracking51.exception.Tracking51Exception as ce:
  print(ce)
except Exception as e:
  print("other error:", e)  
通过ID删除单号

https://api.51Tracking.com/v4/trackings/delete/{id}

id_string = "9a2f7d1e8b912b729388c5835c188c28"
try:
  result = tracking51.tracking.batch_create_trackings(params)
  print(result)
except tracking51.exception.Tracking51Exception as ce:
  print(ce)
except Exception as e:
  print("other error:", e)  
通过ID重新查询过期的单号

https://api.51Tracking.com/v4/trackings/retrack/{id}

id_string = "9a2f7d1e8b912b729388c5835c188c28"
try:
  result = tracking51.tracking.retrack_tracking_by_id(id_string)
  print(result)
except tracking51.exception.Tracking51Exception as ce:
  print(ce)
except Exception as e:
  print("other error:", e)  

Air Waybill

查询航空运单的结果

https://api.51Tracking.com/v4/awb

params = {'awb_number': '235-69030430'}
try:
  result = tracking51.air_waybill.create_an_air_waybill(params)
  print(result)
except tracking51.exception.Tracking51Exception as ce:
  print(ce)
except Exception as e:
  print("other error:", e)  

响应状态码

51Tracking 使用传统的HTTP状态码来表明 API 请求的状态。通常,2xx形式的状态码表示请求成功,4XX形式的状态码表请求发生错误(比如:必要参数缺失),5xx格式的状态码表示 51tracking 的服务器可能发生了问题。

Http CODE META CODE TYPE MESSAGE
200 200 成功 请求响应成功。
400 400 错误请求 请求类型错误。请查看 API 文档以了解此 API 的请求类型。
400 4101 错误请求 物流单号已存在。
400 4102 错误请求 物流单号不存在。请先使用「Create接口」将单号添加至系统。
400 4103 错误请求 您已超出 API 调用的创建数量。每次创建的最大数量为 40 个快递单号。
400 4110 错误请求 物流单号(tracking_number) 不符合规则。
400 4111 错误请求 物流单号(tracking_number)为必填字段。
400 4112 错误请求 查询ID无效。
400 4113 错误请求 不允许重新查询。您只能重新查询过期的物流单号。
400 4120 错误请求 物流商简码(courier_code)的值无效。
400 4121 错误请求 无法识别物流商。
400 4122 错误请求 特殊物流商字段缺失或填写不符合规范。
400 4130 错误请求 请求参数的格式无效。
400 4160 错误请求 空运单号(awb_number)是必需的或有效的格式。
400 4161 错误请求 此空运航空不支持查询。
400 4165 错误请求 查询失败:未创建空运单号。
400 4166 错误请求 删除未创建的空运单号失败。
400 4167 错误请求 空运单号已存在,无需再次创建。
400 4190 错误请求 当前查询额度不足。
401 401 未经授权 身份验证失败或没有权限。请检查并确保您的 API 密钥正确无误。
403 403 禁止 禁止访问。请求被拒绝或不允许访问。
404 404 未找到 页面不存在。请检查并确保您的链接正确无误。
429 429 太多请求 超出 API 请求限制,请稍后重试。请查看 API 文档以了解此 API 的限制。
500 511 服务器错误 服务器错误。请联系我们: service@51Tracking.org。
500 512 服务器错误 服务器错误。请联系我们:service@51Tracking.org。
500 513 服务器错误 服务器错误。请联系我们: service@51Tracking.org。

Metadata

Release files for 51tracking 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for 51tracking 0.1.1
File Size Uploaded Provenance
51tracking-0.1.1.tar.gz 8.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for 51tracking 0.1.1
File Interpreter ABI Platform
51tracking-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / 51tracking-0.1.1.tar.gz

Download URL 51tracking-0.1.1.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c85b3c0557da00865c1a5e6bcc527b65f7a1ddc119756834a9f31e3c0bad3a2f
BLAKE2b-256 checksum
How to use checksums
344f1c11d3d0844189776da48e455ad2c15ac3bf2cef53249fe1f0dad613d572
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.0 Windows/10

Release files / 51tracking-0.1.1-py3-none-any.whl

Download URL 51tracking-0.1.1-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7fe9aa9ddb0de1cf85207c47b43b6a564fcef74ed2473a71f2f36927621d6fbc
BLAKE2b-256 checksum
How to use checksums
1ffc21a01312a87fe9e90455752455b58434c7c91284520c941925b35522fc26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.7.0 Windows/10

Security for 51tracking 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page