Skip to main content

Paddle's Python SDK for Paddle Billing

Project description

Build Status PyPI PyPI pyversions License: Apache 2.0

paddle-python-sdk

Paddle Billing is a complete digital product sales and subscription management platform, designed for modern software businesses. It helps you increase your revenue, retain customers, and scale your operations.

This is a Python SDK that you can use to integrate Paddle Billing with applications written in Python.

For working with Paddle in your frontend, use Paddle.js. You can open checkouts, securely collect payment information, build pricing pages, and integrate with Paddle Retain.

Important: This package works with Paddle Billing. It does not support Paddle Classic. To work with Paddle Classic, see: Paddle Classic API reference

Table of contents

Requirements

Python>=3.11 (for native type hinting, StrEnum, trailing commas, f-strings)

Project dependencies (automatically installed by pip):

  • requests>=2.31
  • urllib3>=2.1.0

Install

Because paddle-python-sdk is available on PyPi, installation is as simple as running the following pip command:

pip install paddle-python-sdk

Usage

To authenticate, you'll need an API key. You can create and manage API keys in Paddle > Developer tools > Authentication.

Pass your API key while initializing a new Paddle client:

from paddle_billing import Client

paddle = Client('PADDLE_API_SECRET_KEY')

You can pass your Paddle API secret key into the SDK from an environment variable:

from os             import getenv
from paddle_billing import Client

paddle = Client(getenv('PADDLE_API_SECRET_KEY'))

You can also pass an environment to work with Paddle's sandbox:

from paddle_billing import Client, Environment, Options

paddle = Client('PADDLE_API_SECRET_KEY', options=Options(Environment.SANDBOX))

Keep in mind that API keys are separate for your sandbox and live accounts, so you'll need to generate keys for each environment.

Examples

There are examples included in the examples folder. To prevent leaking errors we recommend encapsulating Paddle operations inside Try/Except blocks. For brevity, most of the examples below do not do this.

List entities

You can list supported entities with the list() method in the resource. It returns an iterator to help when working with multiple pages.

from paddle_billing import Client

paddle = Client('PADDLE_API_SECRET_KEY')

products = paddle.products.list()

# list() returns an iterable, so pagination is automatically handled
for product in products:
    print(f"Product's id: {product.id}")

Get an entity

You can get an entity with the get() method in the resource. It accepts the id of the entity to get. The entity is returned.

from paddle_billing import Client

paddle = Client('PADDLE_API_SECRET_KEY')

product = paddle.products.get('PRODUCT_ID')

Create an entity

You can create a supported entity with the create() method in the resource. It accepts the resource's corresponding CreateOperation e.g. CreateProduct. The created entity is returned.

from paddle_billing                               import Client
from paddle_billing.Entities.Shared.TaxCategory   import TaxCategory
from paddle_billing.Resources.Products.Operations import CreateProduct

paddle = Client('PADDLE_API_SECRET_KEY')

created_product = paddle.products.create(CreateProduct(
    name         = 'My Product',
    tax_category = TaxCategory.Standard,
))

Update an entity

You can update a supported entity with the update() method in the resource. It accepts the id of the entity to update and the corresponding UpdateOperation e.g. UpdateProduct. The updated entity is returned.

from paddle_billing                        import Client
from paddle_billing.Resources.Products.Operations import UpdateProduct

paddle = Client('PADDLE_API_SECRET_KEY')

# Update the name of the product
updated_product = paddle.products.update('PRODUCT_ID', UpdateProduct(
    name = 'My Improved Product'
))

Where operations require more than one id, the update() method accepts multiple arguments. For example, to update an address for a customer, pass the customerId and the addressId:

updated_address = paddle.addresses.update(
    'CUSTOMER_ID',
    'ADDRESS_ID',
    operation_goes_here,
)

Delete an entity

You can delete an entity with the delete() method in the resource. It accepts the id of the entity to delete. The deleted entity is returned.

from paddle_billing import Client

paddle = Client('PADDLE_API_SECRET_KEY')

deleted_product = paddle.products.delete('PRODUCT_ID')

Error Handling

If a request fails, Paddle raises an ApiError that contains the same information as errors returned by the API. You can use the code attribute to search an error in the error reference and to handle the error in your app. Validation errors also return an array of errors that tell you which fields failed validation. The retry_after property will be set for too_many_requests errors.

This example shows how to handle an error with the code conflict:

from paddle_billing.Exceptions.ApiError import ApiError

try:
    # Call functions from the SDK
except ApiError as error:
    # error.error_code will always follow the error code defined in our documentation
    if error.error_code == 'conflict':
        # Handle Conflict error

Resources

Webhook signature verification

The SDK includes a helper class to verify webhook signatures sent by Notifications from Paddle.

from paddle_billing.Notifications import Secret, Verifier

integrity_check = Verifier().verify(request, Secret('WEBHOOK_SECRET_KEY'))

The verify() method currently accepts any request object matching the paddle_billing.Notifications.Requests.Request protocol, and supports popular frameworks such as Flask and Django.

Time drift max variance can be adjusted by passing seconds to Verifier() (default: 5 seconds):

integrity_check = Verifier(seconds).verify(request, Secret('WEBHOOK_SECRET_KEY'))

Learn more

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

paddle_python_sdk-1.12.0.tar.gz (167.3 kB view details)

Uploaded Source

Built Distribution

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

paddle_python_sdk-1.12.0-py3-none-any.whl (448.0 kB view details)

Uploaded Python 3

File details

Details for the file paddle_python_sdk-1.12.0.tar.gz.

File metadata

  • Download URL: paddle_python_sdk-1.12.0.tar.gz
  • Upload date:
  • Size: 167.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for paddle_python_sdk-1.12.0.tar.gz
Algorithm Hash digest
SHA256 7bc57b2e4c1350df45be9b888957109ff1f1d4591ebc8c20948b7d72bb1e233d
MD5 c29ceb05565ec1ce737185abcfc99627
BLAKE2b-256 0f2687f35554977de3c0efe3cc6ff20c9b4893c71d2d134a2a5a574d64aeac6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddle_python_sdk-1.12.0.tar.gz:

Publisher: publish_to_pypi.yml on PaddleHQ/paddle-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file paddle_python_sdk-1.12.0-py3-none-any.whl.

File metadata

File hashes

Hashes for paddle_python_sdk-1.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 203fd15e2b8c7e1cfe9b4cef745a7606e1532c261c96837c411525a2ad8a9ee0
MD5 17b8a5248628fe1abfeb50886f4dc019
BLAKE2b-256 9f8a216e3d12b6da7ab95fe617aa9bac40087a3e07d26b5f410c10702b5a58ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for paddle_python_sdk-1.12.0-py3-none-any.whl:

Publisher: publish_to_pypi.yml on PaddleHQ/paddle-python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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