Skip to main content

python-dhl-api

PyPI version

A Django module to integrate with DHL EXPRESS API (MyDHL API).

DHL APIs documentation: https://developer.dhl.com/api-reference/dhl-express-mydhl-api#reference-docs-section/

Requirements

Python 3.12+

Installation

Python DHL API is available through pip. To easily install or upgrade it, do

pip install --upgrade python-dhl-api

Usage

Set the following DHL fields in a private file.

DHL_API_KEY = ''
DHL_API_SECRET = ''
DHL_ACCOUNT = ''
DHL_ACCOUNT_IMPORT = '' 
DHL_ACCOUNT_EXPORT = ''

Services available

Please check test_validate.py to see some practical uses.

  1. Address validation
  2. Get rates and services available
  3. Create a shipment
  4. Create a pickup
  5. Upload document
  6. Check shipment
  7. Shipment status

Notes

Use https://dct.dhl.com/ and enter the pickup origin and delivery destination to see all available services. If no service is listed, it is likely unavailable or temporarily suspended.

Create a shipment

service = DHLService(api_key=Setting.DHL_API_KEY, api_secret=Setting.DHL_API_SECRET,
                     account_number=Setting.DHL_ACCOUNT_EXPORT,
                     test_mode=True)

Set one or more accounts:

accounts = [
    shipment.DHLAccountType(type_code=AccountType.SHIPPER, number=Setting.DHL_ACCOUNT_EXPORT),
]

If you want the shipper to pay also the duties then:

accounts = [
    shipment.DHLAccountType(type_code=AccountType.SHIPPER, number=Setting.DHL_ACCOUNT_EXPORT),
    shipment.DHLAccountType(type_code=AccountType.DUTIES_TAXES, number=Setting.DHL_ACCOUNT_EXPORT),
]

Create the sender:

sender_contact = address.DHLContactInformation(
    company_name='Test Co.',
    full_name='Name and surname',
    phone='+39000000000',
    email='name.surname@example.com',
    contact_type=ShipperType.BUSINESS.value
)
sender_address = address.DHLPostalAddress(
    street_line1='123 Example St',
    postal_code='12345',
    province_code='EX',
    country_code='IT',
    city_name='Exampletown',
)
registration_numbers = [address.DHLRegistrationNumber(
    type_code=TypeCode.VAT.name,
    number='42342423423',
    issuer_country_code='IT'
)]

Create the receiver:

receiver_contact = address.DHLContactInformation(
    full_name='Customer',
    phone='+39000000000',
    email='customer@example.com',
    contact_type=ShipperType.PRIVATE.value
)
receiver_address = address.DHLPostalAddress(
    street_line1='456 Sample Ave',
    postal_code='54321',
    country_code='FR',
    city_name='Sampleville',
)

Add the packages to ship:

packages = [shipment.DHLProduct(
    weight=1,
    length=35,
    width=28,
    height=8
)]

Set content and label output. For a list of services you are entitled to, use service get_rates.

shipment_date = next_business_day()
shipment_date = shipment_date.replace(hour=14, minute=0, second=0, microsecond=0)
shipment_date = shipment_date.replace(tzinfo=ZoneInfo('Europe/Rome'))

added_service = [shipment.DHLAddedService(
    service_code='W'
)]

content = shipment.DHLShipmentContent(
    packages=packages,
    is_custom_declarable=False,
    description='Shipment test',
    incoterm_code=IncotermCode.DAP.name,
    unit_of_measurement=MeasurementUnit.METRIC.value,
    product_code=ProductCode.EUROPE.value
)

output = shipment.DHLShipmentOutput(
    dpi=300,
    encoding_format='pdf',
    logo_file_format='png',
    logo_file_base64=LOGO_BASE64
)

customer_references = ['id1', 'id2']

Let's ship:

s = shipment.DHLShipment(
    accounts=accounts,
    sender_contact=sender_contact,
    sender_address=sender_address,
    sender_registration_numbers=registration_numbers,
    receiver_contact=receiver_contact,
    receiver_address=receiver_address,
    ship_datetime=shipment_date,
    added_services=added_service,
    product_code=ProductCode.EUROPE.value,
    content=content,
    output_format=output,
    customer_references=customer_references,
    request_pickup=False
)

ship = service.ship(dhl_shipment=s)

Metadata

Release files for python-dhl-api 3.0.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 python-dhl-api 3.0.1
File Size Uploaded
python_dhl_api-3.0.1.tar.gz 33.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-dhl-api 3.0.1
File Interpreter ABI Platform
python_dhl_api-3.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 50.3 kB

Release files / python_dhl_api-3.0.1.tar.gz

Download URL python_dhl_api-3.0.1.tar.gz
Size 33.6 kB
Tags Source
SHA-256 checksum
How to use checksums
fbdf76f03c85767ee2b7adc153e7f6a0b1c8c06969178743b2bc9ca28d0ed305
BLAKE2b-256 checksum
How to use checksums
9fe7cf0c7d9679854770e41b62aacca92d8e5efe46ef6c9ef91612e5a6167033
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.21

Release files / python_dhl_api-3.0.1-py3-none-any.whl

Download URL python_dhl_api-3.0.1-py3-none-any.whl
Size 16.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f1555bd5afdffcfad3d868be9962bb028e553c77f7fb64f00f9576b4451c2e13
BLAKE2b-256 checksum
How to use checksums
a9fa9c4393ea87a2dd2b16ab351b5ad5623a66bc346d1e3a150680b33ea28a8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.21

Release history Release notifications | RSS feed

This release

3.0.1 This release

2 release files

3.0.0

2 release files

2.4.3

2 release files

2.4.2

2 release files

2.4.1

2 release files

2.4

2 release files

2.3

2 release files

2.2

2 release files

2.1

2 release files

2.0

2 release files

1.1

2 release files

1.0

2 release files

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