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.5 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.n8n._config import N8nSourceConfig
from products.warehouse_sources.sources.n8n.n8n import (
    N8nResumeConfig,
    hostname_of,
    n8n_source,
    validate_credentials as validate_n8n_credentials,
)
from products.warehouse_sources.sources.n8n.settings import ENDPOINTS, N8N_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    ValidateDatabaseHostMixin,
    detect,
)


@SourceRegistry.register
class N8nSource(ResumableSource[N8nSourceConfig, N8nResumeConfig], ValidateDatabaseHostMixin):
    codebase_detection = detect(env=("^N8N_",), npm=("n8n", "n8n-workflow", "@n8n/chat"))

    supported_versions = ("v1",)
    default_version = "v1"
    api_docs_url = "https://docs.n8n.io/api/api-reference/"

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs

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

    @property
    def connection_host_fields(self) -> list[str]:
        # `host` determines where the stored API key is sent, so retargeting it
        # must re-require the key.
        return ["host"]

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.N8N,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="n8n",
            caption="""Connect your n8n instance to pull your workflow automation data into the PostHog Data warehouse.

Works with n8n Cloud and self-hosted instances. Enter your instance URL (e.g. `https://myorg.app.n8n.cloud`) and an API key created under **Settings > n8n API**.

Some tables (users, projects, project users, folders, variables) require an owner/admin key or an Enterprise plan; deselect them if your key can't access them.""",
            iconPath="/static/services/n8n.png",
            docsUrl="https://posthog.com/docs/cdp/sources/n8n",
            releaseStatus=ReleaseStatus.GA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="host",
                        label="Instance URL",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="https://myorg.app.n8n.cloud",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="n8n_api_...",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url": "Your n8n API key is invalid or has been revoked. Create a new key under Settings > n8n API, then reconnect.",
            "403 Client Error: Forbidden for url": "Your n8n API key does not have access to this resource. Some tables require an owner/admin key or an Enterprise plan — deselect them or use a key with the required access.",
        }

    def get_schemas(
        self,
        config: N8nSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Only the insights summary takes a server-side date filter; every list endpoint
        # is full refresh only.
        schemas = [
            SourceSchema(
                name=endpoint,
                supports_incremental=bool(N8N_ENDPOINTS[endpoint].incremental_fields),
                supports_append=False,
                incremental_fields=list(N8N_ENDPOINTS[endpoint].incremental_fields),
                detected_primary_keys=list(N8N_ENDPOINTS[endpoint].primary_keys),
            )
            for endpoint in ENDPOINTS
        ]

        if names is not None:
            names_set = set(names)
            schemas = [s for s in schemas if s.name in names_set]

        return schemas

    def validate_credentials(
        self, config: N8nSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
    ) -> tuple[bool, str | None]:
        try:
            host_valid, host_error = self.is_database_host_valid(hostname_of(config.host), team_id)
        except ValueError:
            return False, "Invalid n8n instance URL"
        if not host_valid:
            return False, host_error

        if validate_n8n_credentials(config.host, config.api_key):
            return True, None

        return False, "Invalid n8n credentials. Check the instance URL and API key."

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

    def source_for_pipeline(
        self,
        config: N8nSourceConfig,
        resumable_source_manager: ResumableSourceManager[N8nResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        try:
            host_valid, host_error = self.is_database_host_valid(hostname_of(config.host), inputs.team_id)
        except ValueError:
            raise ValueError("Invalid n8n instance URL")
        if not host_valid:
            raise ValueError(host_error or "Invalid n8n host")

        return n8n_source(
            host=config.host,
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
        )