DEV Community

KeyDelivery Support for KeyDelivery

Posted on • Edited on

Free eCommerce Shipping & Tracking API Python Demo

KeyDelivery APIs have all the features and integrations you need for shipping, label printing, package tracking, address validation, and order management.

This demo is a complete guide to integrate KeyDelivery Shipping & Tracking API.

# coding = utf-8
# create_tracking.py

import hashlib
import json

import requests


class KuaiDi100:
    def __init__(self):
        self.ApiKey = ''  # You can find your ApiKey on https://app.kd100.com/api-managment
        self.Secret = ''  # You can find your Secret on https://app.kd100.com/api-managment 
        self.url = 'https://www.kd100.com/api/v1/tracking/create'  

    def track(self, com, num, phone, ship_from, ship_to):
        """
        Request Parameters
        :param carrier_id: carrier ID
        :param tracking_number: tracking number
        :param phone: sender & recipient phone number (fill in with a mobile phone number or landline number)
        :param ship_from: sender city
        :param ship_to: recipient city. The tracking frequency will be increased when the shipment arrives at the recipient city.
        :param area_show: Adding this field means using the administrative area determination feature.0: close (default) 1: return data about area_name, location, order_status_description
        :param webhook_url: the URL to which we'll send webhook notifications.
        :return: requests.Response.text
        """
        param = {
            'carrier_id': com,
            'tracking_number': num,
            'phone': phone,
            'ship_from': ship_from,
            'ship_to': ship_to,
            'area_show': 1,
            'webhook_url': 'http://www.kd100.com/console/debug/callback/sandbox'
        }

        param_str = json.dumps(param)
        temp_sign = param_str + self.ApiKey + self.Secret
        md = hashlib.md5()
        md.update(temp_sign.encode())
        sign = md.hexdigest().upper()

        headers = {
            'API-Key': self.ApiKey,
            'signature': sign,
            'Content-Type': 'application/json'
        }

        response = requests.request("POST", self.url, headers=headers, data=param_str)

        return response.text  


result = KuaiDi100().track('fedexcn', '992693344132', '95279527', 'Toronto, Canada', 'Los Angeles, CA, United States')
print(result)
Enter fullscreen mode Exit fullscreen mode
# coding = utf-8
# carrier_auto_detection.py
import hashlib
import json

import requests


class KuaiDi100:
    def __init__(self):
        self.ApiKey = ''  # You can find your ApiKey on https://app.kd100.com/api-managment
        self.Secret = ''  # You can find your Secret on https://app.kd100.com/api-managment 
        self.url = 'https://www.kd100.com/api/v1/carriers/detect'  

    def track(self, num):
        """
        Request Parameters
        :param tracking_number: tracking number
        :return: requests.Response.text
        """
        param = {
            'tracking_number': num
        }

        param_str = json.dumps(param)
        temp_sign = param_str + self.ApiKey + self.Secret
        md = hashlib.md5()
        md.update(temp_sign.encode())
        sign = md.hexdigest().upper()

        headers = {
            'API-Key': self.ApiKey,
            'signature': sign,
            'Content-Type': 'application/json'
        }

        response = requests.request("POST", self.url, headers=headers, data=param_str)

        return response.text  


result = KuaiDi100().track('9926933413')
print(result)
Enter fullscreen mode Exit fullscreen mode
# coding = utf-8
# real_time_tracking.py

import hashlib
import json

import requests


class KuaiDi100:
    def __init__(self):
        self.ApiKey = ''  # You can find your ApiKey on https://app.kd100.com/api-managment
        self.Secret = ''  # You can find your Secret on https://app.kd100.com/api-managment 
        self.url = 'https://www.kd100.com/api/v1/tracking/realtime'  

    def track(self, com, num, phone, ship_from, ship_to):
        """
        Request Parameters
        :param carrier_id: carrier ID
        :param tracking_number: tracking number
        :param phone: sender & recipient phone number (fill in with a mobile phone number or landline number)
        :param ship_from: sender city
        :param ship_to: recipient city. The tracking frequency will be increased when the shipment arrives at the recipient city.
        :param area_show: Adding this field means using the administrative area determination feature.0: close (default) 1: return data about area_name, location, order_status_description
        :param order: false Returned data sort: desc(default), asc.
        :return: requests.Response.text
        """
        param = {
            'carrier_id': com,
            'tracking_number': num,
            'phone': phone,
            'ship_from': ship_from,
            'ship_to': ship_to,
            'area_show': 1,
            'order': 'desc'
        }

        param_str = json.dumps(param)
        temp_sign = param_str + self.ApiKey + self.Secret
        md = hashlib.md5()
        md.update(temp_sign.encode())
        sign = md.hexdigest().upper()

        headers = {
            'API-Key': self.ApiKey,
            'signature': sign,
            'Content-Type': 'application/json'
        }

        response = requests.request("POST", self.url, headers=headers, data=param_str)

        return response.text  


result = KuaiDi100().track('dhlen', '9926933413', '95279527', 'Toronto, Canada', 'Los Angeles, CA, United States')
print(result)
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
karfagen profile image
Sofiia Shevchuk

Thanks for sharing! For transport companies, API is a common component of logistics software, such as TMS and CRM, which allows you to increase the speed of operations, improve the interaction between business chain programs, optimize routes, simplify work with documentation, and much more: cleveroad.com/blog/trucking-api/