:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import re
from typing import Optional, cast
from products.warehouse_sources.sources.chargebee._config import ChargebeeSourceConfig
from products.warehouse_sources.sources.chargebee.chargebee import (
ChargebeeResumeConfig,
chargebee_source,
validate_credentials as validate_chargebee_credentials,
)
from products.warehouse_sources.sources.chargebee.settings import ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.sdk import (
CanonicalDescriptions,
DataWarehouseSourceCategory,
ExternalDataSourceType,
FieldType,
ResumableSource,
ResumableSourceManager,
SourceConfig,
SourceFieldInputConfig,
SourceFieldInputConfigType,
SourceInputs,
SourceRegistry,
SourceResponse,
SourceSchema,
build_endpoint_schemas,
detect,
)
# The site name is interpolated into https://{site_name}.chargebee.com, so it must be a bare DNS
# subdomain: alphanumerics and hyphens, starting alphanumeric. This keeps outbound traffic pinned
# to *.chargebee.com (a value with a scheme, dot, or path can't build another host). Digits are
# allowed; a letters-only check rejected valid site names like "acme2024".
_SITE_NAME_RE = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9-]*$")
@SourceRegistry.register
class ChargebeeSource(ResumableSource[ChargebeeSourceConfig, ChargebeeResumeConfig]):
codebase_detection = detect(
env=("^CHARGEBEE_",),
script_hosts=("js.chargebee.com", "app.retention.chargebee.com", "app.brightback.com"),
npm=(
"chargebee",
"@chargebee/chargebee-js-react-wrapper",
"@chargebee/chargebee-js-vue-wrapper",
"@chargebee/nextjs",
"@chargebee/chargebee-js-angular-wrapper",
"@chargebee/chargebee-js-types",
"@chargebee/react-native-chargebee",
"@chargebee/express",
"@chargebee/better-auth",
"@chargebee/entitlements",
"@chargebee/atomicpricing",
"@chargebee/cli",
"@chargebee/chargebee-apps",
"chargebee-init",
"chargebee-typescript",
"@openaudr/audr-sink-chargebee",
),
pypi=("chargebee",),
cocoapods=("Chargebee",),
composer=("chargebee/chargebee-php", "chargebee/cashier"),
go=("github.com/chargebee/chargebee-go",),
maven=("com.chargebee:chargebee-java", "com.chargebee:chargebee-android", "com.chargebee:cb-partner-spi"),
pub=("chargebee_flutter",),
rubygems=("chargebee",),
swiftpm=("github.com/chargebee/chargebee-ios",),
)
lists_tables_without_credentials = True # static endpoint catalog — safe for public docs
supported_versions = ("v2",)
default_version = "v2"
api_docs_url = "https://apidocs.chargebee.com/docs/api"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.CHARGEBEE
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
# Credential validation already read customers with this key, so a 403 here is one resource
# the key can't read, not a wrong key or site name.
"403 Client Error: Forbidden for url": "Your Chargebee API key can't read one of the tables you sync. Give the key read access to that data in Chargebee, then reconnect the source.",
"Unauthorized for url": "Chargebee rejected your API key. Update the key in your Chargebee settings, then reconnect the source.",
}
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.chargebee.canonical_descriptions import CANONICAL_DESCRIPTIONS
return CANONICAL_DESCRIPTIONS
def get_schemas(
self,
config: ChargebeeSourceConfig,
team_id: int,
with_counts: bool = False,
names: list[str] | None = None,
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names)
def validate_credentials(
self,
config: ChargebeeSourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
if not _SITE_NAME_RE.match(config.site_name):
return (
False,
"That doesn't look like a Chargebee site name. Enter just the site name "
"(the 'acme' in 'acme.chargebee.com'), not the full URL.",
)
if validate_chargebee_credentials(config.api_key, config.site_name):
return True, None
return False, "Invalid credentials"
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[ChargebeeResumeConfig]:
return ResumableSourceManager[ChargebeeResumeConfig](inputs, ChargebeeResumeConfig)
def source_for_pipeline(
self,
config: ChargebeeSourceConfig,
resumable_source_manager: ResumableSourceManager[ChargebeeResumeConfig],
inputs: SourceInputs,
) -> SourceResponse:
resource = chargebee_source(
api_key=config.api_key,
site_name=config.site_name,
endpoint=inputs.schema_name,
team_id=inputs.team_id,
job_id=inputs.job_id,
resumable_source_manager=resumable_source_manager,
should_use_incremental_field=inputs.should_use_incremental_field,
db_incremental_field_last_value=inputs.db_incremental_field_last_value
if inputs.should_use_incremental_field
else None,
)
return SourceResponse(
name=resource.name,
items=lambda: resource,
primary_keys=["id"],
column_hints=resource.column_hints,
)
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.CHARGEBEE,
category=DataWarehouseSourceCategory.PAYMENTS___BILLING,
docsUrl="https://posthog.com/docs/cdp/sources/chargebee",
iconPath="/static/services/chargebee.png",
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="api_key",
label="API key",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="",
secret=True,
),
SourceFieldInputConfig(
name="site_name",
label="Site name (subdomain)",
type=SourceFieldInputConfigType.TEXT,
required=True,
placeholder="",
secret=False,
),
],
),
)