Skip to main content

Python wrapper for the Chargebee Subscription Billing API

Project description

Chargebee Python Client Library v3 (Beta)

The Chargebee Python library streamlines integration with the Chargebee API in Python applications. It offers built-in type annotations and enhanced IDE autocompletion for API resources and responses, which improves the developer experience. To get started, please sign up for a Chargebee account here.

Requirements

  • Python 3.11+

Installation

Install the latest beta version of the library with pip:

pip install chargebee --pre

Install from source with:

python setup.py install

Documentation

See our Python API Reference.

Usage

The package needs to be configured with your site's API key, which is available under Configure Chargebee Section. Refer here for more details.

Configuring chargebee

import chargebee
chargebee.configure("api_key", "site")

Configuring Timeouts

import chargebee
chargebee.update_read_timeout_secs(3000)
chargebee.update_connect_timeout_secs(5000)

Making API Request:

# Create a Customer
import chargebee

response = chargebee.Customer.create(
    chargebee.Customer.CreateParams(
        first_name="John",
        last_name="Doe",
        email="john@test.com",
        locale="fr-CA",
        billing_address=chargebee.Customer.BillingAddress(
            first_name="John",
            last_name=" Doe",
            line1="PO Box 9999",
            city="Walnut",
            state="California",
            zip="91789",
            country="US",
        ),
    )
)
customer = response.customer
card = response.card

List API Request With Filter

For pagination, offset is the parameter that is being used. The value used for this parameter must be the value returned in next_offset parameter from the previous API call.

import chargebee
from chargebee import Filters

response = chargebee.Customer.list(
    chargebee.Customer.ListParams(
        first_name=Filters.StringFilter(IS="John")
    )
)
offset = response.next_offset
print(offset)

Using enums

There are two variants of enums in chargebee,

  • Global enums - These are defined globally and can be accessed across resources.
  • Resource specific enums - These are defined within a resource and can be accessed using the resource class name.
# Global Enum
import chargebee

response = chargebee.Customer.create(
    chargebee.Customer.CreateParams(
        first_name="John",
        auto_collection=chargebee.AutoCollection.ON,  # global enum
    )
)
print(response.customer.cf_host_url)
# Resource Specific Enum
import chargebee

response = chargebee.Customer.change_billing_date(
    chargebee.Customer.ChangeBillingDateParams(
        first_name="John",
        billing_day_of_week=chargebee.Customer.BillingDayOfWeek.MONDAY,  # resource specific enum
    )
)
print(response.customer.cf_host_url)

Using custom fields

import chargebee

response = chargebee.Customer.create(
    chargebee.Customer.CreateParams(
        first_name="John",
        cf_host_url="https://john.com",  # `cf_host_url` is a custom field in Customer object
    )
)
print(response.customer.cf_host_url)

Creating an idempotent request:

Idempotency keys are passed along with request headers to allow a safe retry of POST requests.

import chargebee

response = chargebee.Customer.create(
    chargebee.Customer.CreateParams(
        first_name="John",
        last_name="Doe",
        email="john@test.com",
        locale="fr-CA",
        billing_address=chargebee.Customer.BillingAddress(
            first_name="John",
            last_name=" Doe",
            line1="PO Box 9999",
            city="Walnut",
            state="California",
            zip="91789",
            country="US",
        ),
    ),
    None,
    {
        "chargebee-idempotency-key": "<<UUID>>"
    },  # Replace <<UUID>> with a unique string
)
customer = response.customer
card = response.card
responseHeaders = response.response_headers  # Retrieves response headers
print(responseHeaders)
idempotencyReplayedValue = response.response_headers["is_idempotency_replayed"]  # Retrieves Idempotency replayed header value
print(idempotencyReplayedValue)

Waiting for Process Completion

The response from the previous API call must be passed as an argument for wait_for_export_completion() or wait_for_time_travel_completion()

# Wait For Export Completion
import chargebee
from chargebee import Filters
response = chargebee.Export.customers(
    chargebee.Export.CustomersParams(
        customer=chargebee.Export.CustomersCustomerParams(
            first_name=Filters.StringFilter(IS="John")
        )
    )
)
print(chargebee.Export.wait_for_export_completion(response.export))

Feedback

If you find any bugs or have any feedback, open an issue in this repository or email it to dx@chargebee.com

License

See the LICENSE file.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chargebee-3.0.0b1.tar.gz (229.1 kB view details)

Uploaded Source

Built Distribution

chargebee-3.0.0b1-py3-none-any.whl (301.3 kB view details)

Uploaded Python 3

File details

Details for the file chargebee-3.0.0b1.tar.gz.

File metadata

  • Download URL: chargebee-3.0.0b1.tar.gz
  • Upload date:
  • Size: 229.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for chargebee-3.0.0b1.tar.gz
Algorithm Hash digest
SHA256 6a16356b7ba0636406d999c907270f4eae13d8b5c709ab806320ddfc894a10b1
MD5 0e0d26a8805792c9ee01a45da1b3245d
BLAKE2b-256 8c12250e37b3450501818039f6fab91c35c4974abd73b343613789aac891263e

See more details on using hashes here.

File details

Details for the file chargebee-3.0.0b1-py3-none-any.whl.

File metadata

  • Download URL: chargebee-3.0.0b1-py3-none-any.whl
  • Upload date:
  • Size: 301.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for chargebee-3.0.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 75c4bc58b5d72819ef97aea707bd6dce98bd72f798e323b5aa31a5767891edd9
MD5 a43312308b83c33fdff3ce582f0b112d
BLAKE2b-256 8c9575ed57bba046d8cb99ad87af68ef7fdaf3ebadc4453ff47ac621af84c59a

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