Skip to content

: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.

6.0 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.lovable._config import LovableSourceConfig
from products.warehouse_sources.sources.lovable.lovable import (
    LovableResumeConfig,
    check_endpoint_permissions,
    lovable_source,
    validate_credentials as validate_lovable_credentials,
)
from products.warehouse_sources.sources.lovable.settings import (
    INCREMENTAL_FIELDS,
    LOVABLE_API_VERSION_2026_09_11,
    LOVABLE_API_VERSION_V1,
    MERGE_ONLY_ENDPOINTS,
    version_config,
)
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    VersionDeprecation,
    build_endpoint_schemas,
    detect,
)


@SourceRegistry.register
class LovableSource(ResumableSource[LovableSourceConfig, LovableResumeConfig]):
    codebase_detection = detect(env=("^LOVABLE_",), script_hosts=("cdn.gpteng.co",), npm=("lovable-tagger",))

    lists_tables_without_credentials = True  # static endpoint catalog, safe for public docs
    supported_versions = (LOVABLE_API_VERSION_V1, LOVABLE_API_VERSION_2026_09_11)
    default_version = LOVABLE_API_VERSION_2026_09_11
    # Lovable lists 2026-09-11 as its only supported version and serves it to requests without a
    # `Lovable-Version` header, so v1's request path no longer gets the wire it was built for.
    # Lovable publishes no retirement date for v1.
    deprecated_versions = (VersionDeprecation(version=LOVABLE_API_VERSION_V1, sunset_at=None),)
    api_docs_url = "https://api.lovable.dev/v1/docs"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.LOVABLE

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error": "Your Lovable API key is invalid or has been revoked. Create a new key in Lovable and reconnect.",
            "402 Client Error": "This table needs a higher Lovable plan. Turn it off in the sync settings, or upgrade the workspace in Lovable.",
            "403 Client Error": "Your Lovable API key does not have permission to read this data. Use a key from an account with access, or turn this table off in the sync settings.",
            "410 Client Error": "Lovable has retired the API version this source uses. Contact PostHog support to move the source to a supported version.",
        }

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.lovable.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: LovableSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # The table set differs by version, so discovery must follow the source's pin.
        endpoints = tuple(version_config(self.resolve_api_version(api_version)).endpoints)
        return build_endpoint_schemas(endpoints, INCREMENTAL_FIELDS, names, merge_only=MERGE_ONLY_ENDPOINTS)

    def validate_credentials(
        self,
        config: LovableSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_lovable_credentials(config.api_key, self.resolve_api_version(api_version))

    def get_endpoint_permissions(
        self, config: LovableSourceConfig, team_id: int, endpoints: list[str], api_version: str | None = None
    ) -> dict[str, str | None]:
        return check_endpoint_permissions(config.api_key, self.resolve_api_version(api_version), endpoints)

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[LovableResumeConfig]:
        return ResumableSourceManager[LovableResumeConfig](inputs, LovableResumeConfig)

    def source_for_pipeline(
        self,
        config: LovableSourceConfig,
        resumable_source_manager: ResumableSourceManager[LovableResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return lovable_source(
            api_key=config.api_key,
            api_version=self.resolve_api_version(inputs.api_version),
            endpoint=inputs.schema_name,
            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,
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.LOVABLE,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Lovable",
            caption=(
                "Connect your Lovable account with an API key to sync workspaces, projects, "
                "members, groups, credit history, security findings, and website analytics. Member, "
                "collaborator, credit, and PII tables need Lovable's Enterprise plan. Groups, security "
                "scans, and analytics need Business or higher."
            ),
            keywords=["app builder"],
            docsUrl="https://posthog.com/docs/cdp/sources/lovable",
            iconPath="/static/services/lovable.png",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="lov_...",
                        secret=True,
                    ),
                ],
            ),
            releaseStatus=ReleaseStatus.BETA,
        )