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
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(
        "E4M8S3",
        Destination(
            domestic=DomesticDestination("T3Z1C8")
        )
    )
    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

def main():
    py_canada_post_client = PyCanadaPost.from_env()
    rates = py_canada_post_client.rating.rates.get_rates(
        "E4M8S3",
        Destination(
            domestic=DomesticDestination("T3Z1C8")
        )
    )
    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.0.0.tar.gz (403.8 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.0.0-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_canada_post-1.0.0.tar.gz
  • Upload date:
  • Size: 403.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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.0.0.tar.gz
Algorithm Hash digest
SHA256 40a15cb62c5c3eed9ec2227570fb65d5a44bfdc6c9242c8a77a27f1167554990
MD5 9e3d81675bd16bacf6a665b92d1fe694
BLAKE2b-256 19032ee4f30a96fcb0be713507bbedf31096f15d9922d634017d658c1906d8d6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_canada_post-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 27.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 297062faf94a5aef9a45b7ddb046f3b0e18d25c7686c1452fb72a5e083b8554f
MD5 4396461eb3a1af433c5c26a007cc7e5f
BLAKE2b-256 3227ae508ed8ae9b638272a35eeb009cdfe6957235dbd26b930ab3080ccb3a8d

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