Skip to main content

A Python Library for the PostGrid Print & Mail API

Project description

PostGrid Python Library

The PostGrid Python library enables you to access the PostGrid Print & Mail API conveniently from applications written in Python. It contains classes representing every resource in the PostGrid Print & Mail API and methods which create and operate on these resources.

Installation

In order to install this package, run

pip install --upgrade postgrid-python

Requirements

  • Python 3.6+

Usage

You must have a PostGrid Print & Mail Account (you can sign up at https://dashboard.postgrid.com/signup) and access to your API key which you can retrieve from the settings page.

import postgrid

# Swap this out for your live API key to create live orders
postgrid.pm_key = 'test_sk_...'

# Send a letter
letter = postgrid.Letter.create(
    to={
        'first_name': 'Apaar',
        'last_name': 'Madan',
        'address_line1': '145 Mulberry St, Apt PH D, New York NY 10013',
        'country_code': 'US'
    },
    from_={
        'first_name': 'Kevin',
        'last_name': 'Villena',
        'address_line1': '90 Canal St',
        'address_line2': '2nd Floor',
        'city': 'Boston',
        'province_or_state': 'MA',
        'postal_or_zip': '02114',
        'country_code': 'US'
    },
    html='''
        <html>
            <body>
                <p>Hi, {{to.firstName}}</p>
                <p>Welcome to PostGrid.</p>
                <p>Yours Truly,</p>
                <p>{{from.firstName}} {{from.lastName}}</p>
            </body>
        </html>
    '''
)

# Prints 'ready'
print(letter.status)

# Cancel the letter
letter = postgrid.Letter.delete(letter.id)

# Prints 'cancelled'
print(letter.status)

Note that API parameters map one-to-one with the underlying REST API. However, instead of camelCase, this library uses snake_case. For example, a letterHTML parameter in the original API would be letter_html in this library.

Handling Exceptions

Errors produced by the API will raise a PMError exception. The PMError object has a type field that can be used to determine what the error was. It also has a message field which provides a human readable message describing the error in detail.

import postgrid

postgrid.pm_key = 'test_sk_...'

try:
    template = postgrid.Template.create(
        description='SDK Template',
        html='''
            <html>
                <body>
                    <p>Hi, {{to.firstName}}</p>
                    <p>Welcome to PostGrid.</p>
                    <p>Yours Truly,</p>
                    <p>{{from.firstName}} {{from.lastName}}</p>
                </body>
            </html>
        ''',
    )
except postgrid.PMError as e:
    print(e.status_code)
    print(e.type)
    print(e.message)

    raise e

Support

Email support@postgrid.com if you face any errors with the API or create issues in the postgrid-python GitHub if you face issues with the SDK itself.

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

postgrid-python-1.0.14.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

postgrid_python-1.0.14-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file postgrid-python-1.0.14.tar.gz.

File metadata

  • Download URL: postgrid-python-1.0.14.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for postgrid-python-1.0.14.tar.gz
Algorithm Hash digest
SHA256 f4c21534e60f57c12e336f8ba9a60fe81e4b826caf1eb9a63fbc452429053328
MD5 853be73d74f99e368bbd95df0aa1374f
BLAKE2b-256 ad5ad8cda1450c39fa8b6f58a82d615db83b17c25804be26595a288d265e8520

See more details on using hashes here.

File details

Details for the file postgrid_python-1.0.14-py3-none-any.whl.

File metadata

  • Download URL: postgrid_python-1.0.14-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for postgrid_python-1.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 59acb4532f8ebcbf3ba5f08bf20377d89037fbcc8b4a14c2f29acb762086f698
MD5 7b9282033bb9575cae207f93614cf490
BLAKE2b-256 43e3bf3492263c5c907d887381e9b1701b2f5d211535f3dd2e99b9cd27285ef5

See more details on using hashes here.

Supported by

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