Skip to main content

Python wrapper for the Chargebee Subscription Billing API

Project description

Chargebee Python Client Library v3

[!NOTE] Join Discord

We are trialing a Discord server for developers building with Chargebee. Limited spots are open on a first-come basis. Join here if interested.

This is the official Python library for integrating with Chargebee.

  • 📘 For a complete reference of available APIs, check out our API Documentation.
  • 🧪 To explore and test API capabilities interactively, head over to our API Explorer.

If you're upgrading from an older version please refer to the Migration Guide

Requirements

  • Python 3.11+

Installation

Install the latest version of the library with pip:

pip install chargebee

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 client

from chargebee import Chargebee
cb_client = Chargebee(api_key="", site="")

Configuring Timeouts

from chargebee import Chargebee
cb_client = Chargebee(api_key="api_key", site="site")
cb_client.update_read_timeout_secs(3000)
cb_client.update_connect_timeout_secs(5000)

Configuring Retry Delays

from chargebee import Chargebee
cb_client = Chargebee(api_key="api_key", site="site")
cb_client.update_export_retry_delay_ms(3000)
cb_client.update_time_travel_retry_delay_ms(5000)

Making API Request:

# Create a Customer

response = cb_client.Customer.create(
    cb_client.Customer.CreateParams(
        first_name="John",
        last_name="Doe",
        email="john@test.com",
        locale="fr-CA",
        billing_address=cb_client.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

Async HTTP client

Starting with version 3.9.0, the Chargebee Python SDK can optionally be configured to use an asynchronous HTTP client which uses asyncio to perform non-blocking requests. This can be enabled by passing the use_async_client=True argument to the constructor:

cb_client = Chargebee(api_key="api_key", site="site", use_async_client=True)

When configured to use the async client, all model methods return a coroutine, which will have to be awaited to get the response:

async def get_customers():
    response = await cb_client.Customer.list(
        cb_client.Customer.ListParams(
            first_name=Filters.StringFilter(IS="John")
        )
    )
    return response

Note: The async methods will have to be wrapped in an event loop during invocation. For example, the asyncio.run method can be used to run the above example:

import asyncio
response = asyncio.run(get_customers())

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.

from chargebee import Filters

response = cb_client.Customer.list(
    cb_client.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 = cb_client.Customer.create(
    cb_client.Customer.CreateParams(
        first_name="John",
        auto_collection=chargebee.AutoCollection.ON,  # global enum
    )
)
print(response.customer)
# Resource Specific Enum

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

Using custom fields

response = cb_client.Customer.create(
    cb_client.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.

response = cb_client.Customer.create(
    cb_client.Customer.CreateParams(
        first_name="John",
        last_name="Doe",
        email="john@test.com",
        locale="fr-CA",
        billing_address=cb_client.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.headers  # Retrieves response headers
print(responseHeaders)
idempotencyReplayedValue = response.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

from chargebee import Filters

response = cb_client.Export.customers(
    cb_client.Export.CustomersParams(
        customer=cb_client.Export.CustomersCustomerParams(
            first_name=Filters.StringFilter(IS="John")
        )
    )
)
print(cb_client.Export.wait_for_export_completion(response.export))

Retry Handling

Chargebee's SDK includes built-in retry logic to handle temporary network issues and server-side errors. This feature is disabled by default but can be enabled when needed.

Key features include:

  • Automatic retries for specific HTTP status codes: Retries are automatically triggered for status codes 500, 502, 503, and 504.
  • Exponential backoff: Retry delays increase exponentially to prevent overwhelming the server.
  • Rate limit management: If a 429 Too Many Requests response is received with a Retry-After header, the SDK waits for the specified duration before retrying.

    Note: Exponential backoff and max retries do not apply in this case.

  • Customizable retry behavior: Retry logic can be configured using the retryConfig parameter in the environment configuration.

Example: Customizing Retry Logic

You can enable and configure the retry logic by passing a retryConfig object when initializing the Chargebee environment:

from chargebee import Chargebee
from chargebee.retry_config import RetryConfig

retry_config = RetryConfig(
    enabled=True,
    max_retries=5,
    delay_ms=1000,
    retry_on=[500]
)
cb_client = Chargebee(api_key="api_key", site="site")
cb_client.update_retry_config(retry_config)

# ... your Chargebee API operations ...

Example: Rate Limit retry logic

You can enable and configure the retry logic for rate-limit by passing a retryConfig object when initializing the Chargebee environment:

from chargebee import Chargebee
from chargebee.retry_config import RetryConfig

retry_config = RetryConfig(
    enabled=True,
    max_retries=5,
    delay_ms=1000,
    retry_on=[429]
)
cb_client = Chargebee(api_key="api_key", site="site")
cb_client.update_retry_config(retry_config)

# ... your Chargebee API operations ...

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

Uploaded Source

Built Distribution

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

chargebee-3.24.0-py3-none-any.whl (375.9 kB view details)

Uploaded Python 3

File details

Details for the file chargebee-3.24.0.tar.gz.

File metadata

  • Download URL: chargebee-3.24.0.tar.gz
  • Upload date:
  • Size: 270.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for chargebee-3.24.0.tar.gz
Algorithm Hash digest
SHA256 606fd3e5264cd552f06a191f957cc17b0c09ad508062dcb75a775732d2597aaf
MD5 69c1387081626a6d8745fe17ecb02a4a
BLAKE2b-256 0ae106b2f4a08b4dfeb53fa3b376402cdeb8daab75f49e7c8d3130d69f176bca

See more details on using hashes here.

File details

Details for the file chargebee-3.24.0-py3-none-any.whl.

File metadata

  • Download URL: chargebee-3.24.0-py3-none-any.whl
  • Upload date:
  • Size: 375.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for chargebee-3.24.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f306b9326dae8f81f17292a714d74cddad22b8737ae86cad68c5b9d226135d5
MD5 813b0e8996bd4c9f0a811e60b233e779
BLAKE2b-256 1d0b42313ff89c9e3471756e83777b75095d2773eb35db6a9ff3411dc7f46288

See more details on using hashes here.

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