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.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

postgrid_python-1.0.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: postgrid-python-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 59d0991f37958264199d8b706a2608b4f3dc8a8470deef9e7462a7568b0869fb
MD5 070da2bf93991d8deac97ac4a84fa129
BLAKE2b-256 0c6940238b3b506af9cfca46dea15613203af0b9dfc52924f29a8bf2ef25e99b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: postgrid_python-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9dc54e3e521845147a4d4bfe040e9a2c1efe017bb1fd8a8d563dff962de86439
MD5 0675092ac2e5e6037bdeec1fbf5d1e54
BLAKE2b-256 b48b921f3ad81a56dc99de5d9c88fcfcdcc1ea38b442036c662252f74fc4a467

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