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.

7.5 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)
from products.warehouse_sources.sources.vapi._config import VapiSourceConfig
from products.warehouse_sources.sources.vapi.settings import (
    DEFAULT_VERSION,
    ENDPOINTS,
    INCREMENTAL_FIELDS,
    SUPPORTED_VERSIONS,
    VAPI_ENDPOINTS,
)
from products.warehouse_sources.sources.vapi.vapi import (
    VapiResumeConfig,
    validate_credentials as validate_vapi_credentials,
    vapi_source,
)


@SourceRegistry.register
class VapiSource(ResumableSource[VapiSourceConfig, VapiResumeConfig]):
    codebase_detection = detect(
        env=("^VAPI_",),
        npm=("@vapi-ai/server-sdk", "@vapi-ai/web"),
        pypi=("vapi-server-sdk", "vapi-python"),
        go=("github.com/VapiAI/server-sdk-go",),
    )

    supported_versions = SUPPORTED_VERSIONS
    default_version = DEFAULT_VERSION
    api_docs_url = "https://docs.vapi.ai/api-reference"

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

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

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.VAPI,
            category=DataWarehouseSourceCategory.COMMUNICATION,
            label="Vapi",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your Vapi private API key to automatically pull your Vapi voice agent data into the PostHog Data warehouse.

You can find your private API key in the [Vapi dashboard](https://dashboard.vapi.ai) under **Organization settings** → **API keys**.
""",
            iconPath="/static/services/vapi.png",
            docsUrl="https://posthog.com/docs/cdp/sources/vapi",
            keywords=["voice", "ai agent", "calls", "telephony"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        # An invalid or revoked Vapi API key surfaces as a requests HTTPError from
        # `raise_for_status()`. Match the stable status text and base host, not the
        # per-request path/query.
        return {
            "401 Client Error: Unauthorized for url: https://api.vapi.ai": "Your Vapi API key is invalid or has been revoked. Create a new private API key in the Vapi dashboard, then reconnect.",
            "403 Client Error: Forbidden for url: https://api.vapi.ai": "Your Vapi API key does not have permission to read this data. Check the key's permissions in the Vapi dashboard, then reconnect.",
            # Vapi's v2 phone-number list endpoint rejects our request with a 400 for some
            # accounts even though the query (limit/page/sortOrder/sortBy=createdAt) matches
            # Vapi's own published spec byte for byte — every retry replays the identical,
            # spec-compliant request and gets the same 400 back. Match the path, not the
            # per-request query string, so this stays scoped to this one known-bad endpoint.
            "400 Client Error: Bad Request for url: https://api.vapi.ai/v2/phone-number": "Vapi rejected the request to list phone numbers on API v2 for this account. This looks like an issue on Vapi's side rather than your credentials — contact Vapi support, or disable the phone_numbers table if you don't need it.",
            # Same pattern as the phone-number v2 case above, on the /chat list endpoint: the
            # request (limit/page/sortOrder/sortBy/createdAtGt) matches Vapi's published spec,
            # and every retry replays the identical request and gets the same 400 back.
            "400 Client Error: Bad Request for url: https://api.vapi.ai/chat": "Vapi rejected the request to list chats for this account. This looks like an issue on Vapi's side rather than your credentials — contact Vapi support, or disable the chats table if you don't need it.",
        }

    def get_schemas(
        self,
        config: VapiSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Version-blind: the table set, incremental fields, and primary keys are identical across
        # versions; only phone_numbers' request path/pagination differ, which discovery doesn't expose.
        def _build_schema(endpoint: str) -> SourceSchema:
            endpoint_config = VAPI_ENDPOINTS[endpoint]
            has_incremental = len(endpoint_config.incremental_fields) > 0
            return SourceSchema(
                name=endpoint,
                supports_incremental=has_incremental,
                supports_append=has_incremental,
                incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
                should_sync_default=endpoint_config.should_sync_default,
            )

        schemas = [_build_schema(endpoint) 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: VapiSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
    ) -> tuple[bool, str | None]:
        # Version-blind: the probe hits /assistant, which has no v2 variant, so it validates both pins.
        if validate_vapi_credentials(config.api_key):
            return True, None

        return False, "Invalid Vapi API key"

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

    def source_for_pipeline(
        self,
        config: VapiSourceConfig,
        resumable_source_manager: ResumableSourceManager[VapiResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return vapi_source(
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            api_version=self.resolve_api_version(inputs.api_version),
            logger=inputs.logger,
            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,
            db_incremental_field_earliest_value=inputs.db_incremental_field_earliest_value
            if inputs.should_use_incremental_field
            else None,
            incremental_field=inputs.incremental_field,
        )