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

Uploaded Source

Built Distribution

postgrid_python-1.0.6-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: postgrid-python-1.0.6.tar.gz
  • Upload date:
  • Size: 6.3 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.6.tar.gz
Algorithm Hash digest
SHA256 844333f8de6668e8955b57eb29c011c26531663c7fab94d3a27318d4fd3f8f7b
MD5 ea6dfa2b141a18d3784c98269ee0e73d
BLAKE2b-256 3f924371c384f835cb4a8b8229af6112f47d50ea30bab8e9e5e79dd388cfb5d0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: postgrid_python-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.1 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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e32822c8bd30b52c6404124c928d7d1bad28ac3b223d586735fef503377a13d4
MD5 43a417b721c6e4ceb7b520537a920962
BLAKE2b-256 80163007d7fa49c0076fdb60c3f9c3d07aa823b464575664ee281b500322a6b2

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