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.

4.6 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.airtable._config import AirtableSourceConfig
from products.warehouse_sources.sources.airtable.airtable import (
    airtable_source,
    validate_credentials as validate_airtable_credentials,
)
from products.warehouse_sources.sources.airtable.settings import ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.sdk import (
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    SimpleSource,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)


@SourceRegistry.register
class AirtableSource(SimpleSource[AirtableSourceConfig]):
    codebase_detection = detect(
        env=("^AIRTABLE_",),
        script_hosts=("static.airtable.com",),
        npm=("airtable", "@airtable/blocks", "@airtable/blocks-cli", "@airtable/mcp-cli"),
        pypi=("pyairtable", "airtable-python-wrapper", "airtable"),
        composer=("sleiman/airtable-php", "tapp/laravel-airtable"),
        go=("github.com/mehanizm/airtable",),
        rubygems=("airtable", "airrecord"),
    )

    supported_versions = ("v0",)
    default_version = "v0"
    api_docs_url = "https://airtable.com/developers/web/api/changelog"

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

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

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error: Unauthorized for url: https://api.airtable.com": "Airtable authentication failed. Please check your personal access token.",
            "403 Client Error: Forbidden for url: https://api.airtable.com": "Airtable denied access. Please check that your personal access token has the required scopes and base access.",
        }

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.AIRTABLE,
            category=DataWarehouseSourceCategory.PRODUCTIVITY,
            label="Airtable",
            caption="""Enter your Airtable personal access token to pull your Airtable bases into the PostHog Data warehouse.

Create a personal access token at [airtable.com/create/tokens](https://airtable.com/create/tokens) with the `data.records:read` and `schema.bases:read` scopes, and grant it access to the bases you want to sync. Records are synced from every table of every base the token can access.""",
            iconPath="/static/services/airtable.png",
            docsUrl="https://posthog.com/docs/cdp/sources/airtable",
            releaseStatus=ReleaseStatus.GA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="personal_access_token",
                        label="Personal access token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="pat...",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_schemas(
        self,
        config: AirtableSourceConfig,
        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: AirtableSourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if validate_airtable_credentials(config.personal_access_token):
            return True, None

        return (
            False,
            "Invalid Airtable personal access token. Check that the token is correct and has access to the bases you want to sync, then try again.",
        )

    def source_for_pipeline(self, config: AirtableSourceConfig, inputs: SourceInputs) -> SourceResponse:
        return airtable_source(
            personal_access_token=config.personal_access_token,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            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,
        )