API Reference
Complete API reference for the Cloudflare SaaS Platform library.
Core Classes
CloudflareSaaSPlatform
- class cloudflare_saas.CloudflareSaaSPlatform(config: Config, storage: StorageAdapter | None = None)[source]
Bases:
LoggerMixinMain platform orchestrator with pluggable storage.
This class coordinates R2 storage, Cloudflare custom hostnames, DNS verification, and tenant management with configurable persistence.
- __init__(config: Config, storage: StorageAdapter | None = None)[source]
- async create_tenant(name: str, slug: str, owner_id: str | None = None, metadata: Dict | None = None) Tenant[source]
Create a new tenant.
- async list_tenants(limit: int = 100, offset: int = 0) List[Tenant][source]
List tenants with pagination.
- async deploy_tenant_site(tenant_id: str, local_path: str, base_prefix: str = '') DeploymentResult[source]
Deploy static site for tenant.
- async add_custom_domain(tenant_id: str, domain: str, verification_method: VerificationMethod = VerificationMethod.HTTP) HostnameVerificationInstructions[source]
Start custom domain onboarding process.
- async get_domain_status(domain: str) CustomDomain[source]
Get status of custom domain provisioning.
- async list_tenant_domains(tenant_id: str) List[CustomDomain][source]
List all domains for a tenant.
Config
- class cloudflare_saas.Config(*, cloudflare_api_token: str, cloudflare_account_id: str, cloudflare_zone_id: str, r2_access_key_id: str, r2_secret_access_key: str, r2_bucket_name: str, r2_endpoint: str | None = None, r2_jurisdiction: str | None = None, d1_database_id: str | None = None, d1_jurisdiction: str | None = None, platform_domain: str, worker_script_name: str = 'site-router', worker_script_template_name: str | None = None, internal_api_key: str | None = None, log_level: str = 'INFO', log_format: str = 'detailed', log_file: str | None = None, enable_console_logging: bool = True, enable_custom_hostnames: bool = True, default_cache_ttl: int = 604800)[source]
Bases:
BaseModelConfiguration for Cloudflare SaaS platform.
- model_config = {'validate_assignment': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Clients
CloudflareClient
- class cloudflare_saas.CloudflareClient(config: Config)[source]
Bases:
LoggerMixinAsync Cloudflare API client.
- async create_custom_hostname(hostname: str, verification_method: VerificationMethod = VerificationMethod.HTTP) Dict[str, Any][source]
Create custom hostname in Cloudflare for SaaS.
R2Client
- class cloudflare_saas.R2Client(config: Config)[source]
Bases:
LoggerMixinAsync R2 client for object operations.
- async upload_file(tenant_id: str, local_path: Path, object_key: str, content_type: str | None = None) str[source]
Upload a file to R2 under tenant namespace.
- async upload_directory(tenant_id: str, local_dir: Path, base_prefix: str = '') List[str][source]
Upload entire directory to R2.
DNSVerifier
- class cloudflare_saas.DNSVerifier[source]
Bases:
objectAsync DNS verification.
- async verify_cname(domain: str, expected_target: str) bool[source]
Verify CNAME record points to expected target.
TerraformDeployer
- class cloudflare_saas.TerraformDeployer(config: Config, working_dir: str = './terraform')[source]
Bases:
objectAutomate Terraform deployment for Workers and R2.
- generate_terraform_config(worker_script_path: Path) None[source]
Generate Terraform configuration files.
Storage Adapters
StorageAdapter
- class cloudflare_saas.StorageAdapter[source]
Bases:
ABCAbstract base class for storage implementations.
- abstractmethod async list_tenants(limit: int = 100, offset: int = 0) List[Tenant][source]
List all tenants with pagination.
- abstractmethod async save_domain(domain: CustomDomain) None[source]
Save custom domain to storage.
- abstractmethod async get_domain(domain: str) CustomDomain | None[source]
Retrieve custom domain by hostname.
- abstractmethod async list_tenant_domains(tenant_id: str) List[CustomDomain][source]
List all domains for a tenant.
InMemoryStorageAdapter
- class cloudflare_saas.InMemoryStorageAdapter[source]
Bases:
StorageAdapterIn-memory storage implementation (for testing/development).
- async list_tenants(limit: int = 100, offset: int = 0) List[Tenant][source]
List all tenants with pagination.
- async save_domain(domain: CustomDomain) None[source]
Save custom domain to storage.
- async get_domain(domain: str) CustomDomain | None[source]
Retrieve custom domain by hostname.
- async list_tenant_domains(tenant_id: str) List[CustomDomain][source]
List all domains for a tenant.
PostgresStorageAdapter
- class cloudflare_saas.PostgresStorageAdapter(connection_string: str)[source]
Bases:
StorageAdapterPostgreSQL storage implementation.
- async list_tenants(limit: int = 100, offset: int = 0) List[Tenant][source]
List all tenants with pagination.
- async save_domain(domain: CustomDomain) None[source]
Save custom domain to storage.
- async get_domain(domain: str) CustomDomain | None[source]
Retrieve custom domain by hostname.
- async list_tenant_domains(tenant_id: str) List[CustomDomain][source]
List all domains for a tenant.
Data Models
Tenant
- class cloudflare_saas.Tenant(*, tenant_id: str, name: str, slug: str, subdomain: str, owner_id: str | None = None, created_at: datetime = <factory>, metadata: Dict[str, ~typing.Any]=<factory>)[source]
Bases:
BaseModelTenant model.
- model_config = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
CustomDomain
- class cloudflare_saas.CustomDomain(*, domain: str, tenant_id: str, status: DomainStatus = DomainStatus.PENDING, verification_method: VerificationMethod = VerificationMethod.HTTP, verification_token: str | None = None, cname_target: str | None = None, cloudflare_hostname_id: str | None = None, ssl_status: str | None = None, created_at: datetime = <factory>, verified_at: datetime | None = None, error_message: str | None = None)[source]
Bases:
BaseModelCustom domain model.
- status: DomainStatus
- verification_method: VerificationMethod
- model_config = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
DomainStatus
DeploymentResult
- class cloudflare_saas.DeploymentResult(*, tenant_id: str, files_uploaded: int, total_size_bytes: int, deployment_time_seconds: float, success: bool, error_message: str | None = None, uploaded_paths: List[str] = <factory>)[source]
Bases:
BaseModelResult of site deployment.
- model_config = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
VerificationMethod
HostnameVerificationInstructions
- class cloudflare_saas.HostnameVerificationInstructions(*, domain: str, cname_target: str, verification_method: VerificationMethod, http_verification_url: str | None = None, http_verification_token: str | None = None, txt_record_name: str | None = None, txt_record_value: str | None = None, instructions: str)[source]
Bases:
BaseModelInstructions for domain verification.
- verification_method: VerificationMethod
- model_config = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Exceptions
CloudflareSaaSException
TenantNotFoundError
- class cloudflare_saas.TenantNotFoundError[source]
Bases:
CloudflareSaaSExceptionRaised when tenant is not found.
DomainVerificationError
- class cloudflare_saas.DomainVerificationError[source]
Bases:
CloudflareSaaSExceptionRaised when domain verification fails.
DeploymentError
- class cloudflare_saas.DeploymentError[source]
Bases:
CloudflareSaaSExceptionRaised when site deployment fails.
R2OperationError
- class cloudflare_saas.R2OperationError[source]
Bases:
CloudflareSaaSExceptionRaised when R2 operations fail.
CustomHostnameError
- class cloudflare_saas.CustomHostnameError[source]
Bases:
CloudflareSaaSExceptionRaised when custom hostname operations fail.
DNSError
- class cloudflare_saas.DNSError[source]
Bases:
CloudflareSaaSExceptionRaised when DNS operations fail.
Logging
configure_logging
- cloudflare_saas.configure_logging(level: LogLevel = LogLevel.INFO, log_format: LogFormat = LogFormat.DETAILED, log_file: str | None = None, enable_console: bool = True) None[source]
Configure logging for the application.
- Parameters:
level – The logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
log_format – The format of log messages (simple, detailed, json)
log_file – Optional file path to write logs to
enable_console – Whether to enable console output
Examples
>>> from cloudflare_saas.logging_config import configure_logging, LogLevel, LogFormat >>> configure_logging(level=LogLevel.DEBUG, log_format=LogFormat.JSON) >>> configure_logging(level=LogLevel.INFO, log_file="app.log")
get_logger
- cloudflare_saas.get_logger(name: str) Logger[source]
Get a logger instance.
- Parameters:
name – The name of the logger (typically __name__)
- Returns:
A configured logger instance
Examples
>>> from cloudflare_saas.logging_config import get_logger >>> logger = get_logger(__name__) >>> logger.info("Starting operation")