Skip to main content

Python wrapper to interact with Canada Post API.

Project description

py-canada-post logo

Py-canada-post

Python written wrapper to interact with Canada Post API.

Table of contents

Why was it created?

There is no public Python library/wrapper to interact with Canada Post API, so an idea was born and brought to life.

How to use?

Essential steps:

Before you start using this wrapper, you should perform these essential steps:

  1. Create a virtual environment:

    • Using python:

      python -m venv .venv
      source .venv/bin/activate
      
    • Using uv:

      uv python pin 3.11
      uv init
      uv sync
      
  2. Create .env and put your customer number, contract id and api key in there as followed:

    CUSTOMER_NUMBER=12345667
    CONTRACT_ID=12344556
    API_KEY=ewfghui34hto34ghui34g:ewghui234h23oih34uigh423
    

    To test this app, you will need some test credentials. You can get them here.

    If you would like to obtain your own, you can register your business and join the Developer Program.

Quick start

To use this wrapper, you will need 3.11+ python version and pip.

Install from PyPi

pip install py-canada-post

or

uv add py-canada-post

Install from source

  1. Clone the repository:

    git clone https://github.com/joludyaster/py-canada-post.git
    
  2. Run:

    pip install .
    

    or

    uv pip install .
    

Getting started

You could define the client object yourself:

import os

from py_canada_post.client import PyCanadaPost
from py_canada_post.services.rating import Destination, DomesticDestination, ParcelCharacteristics
from dotenv import load_dotenv

load_dotenv()

customer_number = os.getenv("CUSTOMER_NUMBER", 0)
api_key = os.getenv("API_KEY", "")
contract_id = os.getenv("CONTRACT_ID", 0)

def main():
    py_canada_post_client = PyCanadaPost(
        customer_number=customer_number,
        api_key=api_key,
        contract_id=contract_id
    )
    rates = py_canada_post_client.rating.rates.get_rates(
        origin_postal_code="E4M8S3",
        destination=Destination(
            domestic=DomesticDestination("T3Z1C8")
        ),
        parcel_characteristics=ParcelCharacteristics(
            weight=13.2
        )
    )
    print(rates)

if __name__ == "__main__":
    main()

Or you could use a shortcut:

from py_canada_post.client import PyCanadaPost
from py_canada_post.services.rating import Destination, DomesticDestination, ParcelCharacteristics

def main():
    py_canada_post_client = PyCanadaPost.from_env()
    rates = py_canada_post_client.rating.rates.get_rates(
        origin_postal_code="E4M8S3",
        destination=Destination(
            domestic=DomesticDestination("T3Z1C8")
        ),
        parcel_characteristics=ParcelCharacteristics(
            weight=13.2
        )
    )
    print(rates)

if __name__ == "__main__":
    main()

See the documentation to learn more.

CLI

Usage:
    py-canada-post [-v] [-h]
    py-canada-post rating get-rates ORIGIN-POSTAL-CODE [ARGS]
    py-canada-post rating discover-services COUNTRY-CODE [ARGS]
    
    more to come...
    
Options:
    rating          Command to contain all rating-related commands (e.g. get_rates, discover_services etc.).                                                                                                                     │
    --help (-h)     Display this message and exit.                                                                                                                                                                               │
    --version (-v)  Display application version.

See the CLI documentation to learn more.

Roadmap

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_canada_post-1.1.1.tar.gz (407.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_canada_post-1.1.1-py3-none-any.whl (35.7 kB view details)

Uploaded Python 3

File details

Details for the file py_canada_post-1.1.1.tar.gz.

File metadata

  • Download URL: py_canada_post-1.1.1.tar.gz
  • Upload date:
  • Size: 407.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_canada_post-1.1.1.tar.gz
Algorithm Hash digest
SHA256 0a6e00d2d9715b39e4664370bd9ea33c8fa699c114f989420ca65bc7e5a90fae
MD5 18cb8d3d9573686f1e4a343d6cf9ccec
BLAKE2b-256 a9f7b71c8da4213cc9705cf3ab0b135edcff30e5a7ba0a37243daa022835b2f5

See more details on using hashes here.

File details

Details for the file py_canada_post-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: py_canada_post-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 35.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for py_canada_post-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a01a0b95b7b62db4023a8ed6e2f3e47d0a54578dddd8fc678e3687bc641dc3e1
MD5 f8462f5e981d2fc3aaafdce537254225
BLAKE2b-256 32d2954549b6edb0538782b0f7c7f8ae8e08aeab4ecfa997a011c774dff62e64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page