Chargebee Python Client Library

3.20.0 · active · verified Sun Apr 12

The Chargebee Python Client Library is the official Python wrapper for integrating with the Chargebee Subscription Billing API. It provides a comprehensive, idiomatic Python interface to manage customers, subscriptions, invoices, payments, and other billing-related resources. The current version is 3.20.0, and the library is actively maintained with a regular release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Chargebee client using environment variables for security and then perform a basic operation, such as creating a new customer. It highlights the use of `Chargebee.Customer.CreateParams` for structured request bodies. Ensure you replace placeholder values or set environment variables before running.

import os
from chargebee import Chargebee

# Configure the Chargebee client with your API key and site name
# It's recommended to use environment variables for sensitive data
API_KEY = os.environ.get('CHARGEBEE_API_KEY', 'your_test_api_key')
SITE_NAME = os.environ.get('CHARGEBEE_SITE_NAME', 'your_test_site')

if API_KEY == 'your_test_api_key' or SITE_NAME == 'your_test_site':
    print("WARNING: Please set CHARGEBEE_API_KEY and CHARGEBEE_SITE_NAME environment variables.")
    print("Using placeholder values, API calls will likely fail.")

cb_client = Chargebee(api_key=API_KEY, site=SITE_NAME)

try:
    # Example: Create a new customer
    # Note: CreateParams are nested under the resource class via the client
    response = cb_client.Customer.create(
        cb_client.Customer.CreateParams(
            first_name="John",
            last_name="Doe",
            email="john.doe@example.com",
            locale="en-US"
        )
    )

    customer = response.customer
    print(f"Successfully created customer: {customer.id} - {customer.email}")

    # Example: Retrieve a customer
    # response = cb_client.Customer.retrieve(customer.id)
    # retrieved_customer = response.customer
    # print(f"Retrieved customer: {retrieved_customer.id} - {retrieved_customer.first_name}")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure your API key and site name are correct and have appropriate permissions.")

view raw JSON →