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.

fix(recharge): stop sending sort_by to the 2021-11 /products endpoint (#62137)

Tom Owersac839f578458
diff --git a/posthog/temporal/data_imports/sources/recharge/api_inventory.md b/posthog/temporal/data_imports/sources/recharge/api_inventory.md
index 52ed9f41dbb1..5206dbd0a531 100644
--- a/posthog/temporal/data_imports/sources/recharge/api_inventory.md
+++ b/posthog/temporal/data_imports/sources/recharge/api_inventory.md
@@ -21,18 +21,18 @@ Source: Recharge Payments — subscription management for e-commerce.
## Endpoints
-| Schema | Path | Primary key | Partition key | Incremental | Notes |
-| ----------------- | ------------------ | ----------- | ------------- | ----------- | --------------------------------------------------------------------------------- |
-| `customers` | `/customers` | `id` | `created_at` | ✅ | |
-| `subscriptions` | `/subscriptions` | `id` | `created_at` | ✅ | |
-| `orders` | `/orders` | `id` | `created_at` | ✅ | |
-| `charges` | `/charges` | `id` | `created_at` | ✅ | 90-day retention for `processed_at` filtering (we use `updated_at`/`created_at`). |
-| `addresses` | `/addresses` | `id` | `created_at` | ✅ | |
-| `discounts` | `/discounts` | `id` | `created_at` | ✅ | |
-| `onetimes` | `/onetimes` | `id` | `created_at` | ✅ | |
-| `products` | `/products` | `id` | `created_at` | ✅ | |
-| `payment_methods` | `/payment_methods` | `id` | `created_at` | ✅ | Requires Recharge Pro / Custom plan. |
-| `collections` | `/collections` | `id` | `created_at` | ➖ full | Server-side timestamp filter not reliably documented — full refresh. |
+| Schema | Path | Primary key | Partition key | Incremental | Notes |
+| ----------------- | ------------------ | ----------- | ------------- | ----------- | ---------------------------------------------------------------------------------- |
+| `customers` | `/customers` | `id` | `created_at` | ✅ | |
+| `subscriptions` | `/subscriptions` | `id` | `created_at` | ✅ | |
+| `orders` | `/orders` | `id` | `created_at` | ✅ | |
+| `charges` | `/charges` | `id` | `created_at` | ✅ | 90-day retention for `processed_at` filtering (we use `updated_at`/`created_at`). |
+| `addresses` | `/addresses` | `id` | `created_at` | ✅ | |
+| `discounts` | `/discounts` | `id` | `created_at` | ✅ | |
+| `onetimes` | `/onetimes` | `id` | `created_at` | ✅ | |
+| `products` | `/products` | `id` | `created_at` | ➖ full | 2021-11 list endpoint takes only cursor + `limit`/`ids` — no `sort_by` or `*_min`. |
+| `payment_methods` | `/payment_methods` | `id` | `created_at` | ✅ | Requires Recharge Pro / Custom plan. |
+| `collections` | `/collections` | `id` | `created_at` | ➖ full | Server-side timestamp filter not reliably documented — full refresh. |
## Possible future work
diff --git a/posthog/temporal/data_imports/sources/recharge/recharge.py b/posthog/temporal/data_imports/sources/recharge/recharge.py
index 836ae34991d0..9c0673d74679 100644
--- a/posthog/temporal/data_imports/sources/recharge/recharge.py
+++ b/posthog/temporal/data_imports/sources/recharge/recharge.py
@@ -100,9 +100,14 @@ def _build_initial_params(
# created_at). Filter server-side on `<field>_min` and sort ascending on
# the same field so the pipeline watermark advances monotonically.
params[f"{incremental_field}_min"] = _format_incremental_value(db_incremental_field_last_value)
- params["sort_by"] = f"{incremental_field}-asc"
+ sort_field = incremental_field
else:
- params["sort_by"] = f"{config.default_sort_field}-asc"
+ sort_field = config.default_sort_field
+
+ # Some endpoints (e.g. `/products` on the 2021-11 API) reject `sort_by`
+ # outright with a 422 — they rely on cursor pagination for stable ordering.
+ if config.supports_sort:
+ params["sort_by"] = f"{sort_field}-asc"
return params
diff --git a/posthog/temporal/data_imports/sources/recharge/settings.py b/posthog/temporal/data_imports/sources/recharge/settings.py
index caeaf877c954..8ef7ac7cd617 100644
--- a/posthog/temporal/data_imports/sources/recharge/settings.py
+++ b/posthog/temporal/data_imports/sources/recharge/settings.py
@@ -24,6 +24,11 @@ class RechargeEndpointConfig:
incremental_fields: list[IncrementalField] = field(default_factory=list)
partition_key: Optional[str] = "created_at"
supports_incremental: bool = True
+ # Whether the list endpoint accepts a `sort_by` query param. The 2021-11 API
+ # dropped `sort_by` for `/products` (it isn't in the sorting reference for
+ # this version), so sending it returns a 422. Endpoints without sort support
+ # rely on cursor pagination for ordering instead.
+ supports_sort: bool = True
# Default sort field used when not syncing incrementally. Recharge accepts
# `<field>-asc` / `<field>-desc`; `id-asc` is a stable monotonic order that
# avoids page-boundary skips/dupes when rows are inserted mid-sync.
@@ -81,10 +86,15 @@ RECHARGE_ENDPOINTS: dict[str, RechargeEndpointConfig] = {
path="/onetimes",
incremental_fields=_UPDATED_AND_CREATED,
),
+ # `/products` on the 2021-11 API only accepts cursor pagination + `limit`/
+ # `ids` — unlike 2021-01 it exposes neither `sort_by` nor a `*_min` timestamp
+ # filter, so sending either returns a 422. Full-refresh, cursor-ordered only.
"products": RechargeEndpointConfig(
name="products",
path="/products",
- incremental_fields=_UPDATED_AND_CREATED,
+ incremental_fields=[],
+ supports_incremental=False,
+ supports_sort=False,
),
"payment_methods": RechargeEndpointConfig(
name="payment_methods",
diff --git a/posthog/temporal/data_imports/sources/recharge/tests/test_recharge.py b/posthog/temporal/data_imports/sources/recharge/tests/test_recharge.py
index a436e73f5523..413ed47ed699 100644
--- a/posthog/temporal/data_imports/sources/recharge/tests/test_recharge.py
+++ b/posthog/temporal/data_imports/sources/recharge/tests/test_recharge.py
@@ -77,16 +77,24 @@ class TestBuildInitialParams:
assert params["sort_by"] == "id-asc"
assert not any(k.endswith("_min") for k in params)
- def test_non_incremental_endpoint_never_filters(self) -> None:
- # `collections` ships full-refresh only; even with incremental inputs set
- # it must not emit a server-side timestamp filter.
+ @parameterized.expand(
+ [
+ # `collections` is full-refresh but still sortable -> keeps `id-asc`.
+ ("collections", {"limit": 250, "sort_by": "id-asc"}),
+ # `/products` on the 2021-11 API rejects `sort_by` outright -> limit only.
+ ("products", {"limit": 250}),
+ ]
+ )
+ def test_full_refresh_endpoint_ignores_incremental_inputs(self, endpoint: str, expected: dict) -> None:
+ # Full-refresh endpoints must never emit a `*_min` filter even when the
+ # user supplies incremental inputs; products additionally omits `sort_by`.
params = _build_initial_params(
- RECHARGE_ENDPOINTS["collections"],
+ RECHARGE_ENDPOINTS[endpoint],
should_use_incremental_field=True,
db_incremental_field_last_value=datetime(2026, 1, 1, tzinfo=UTC),
incremental_field="updated_at",
)
- assert params["sort_by"] == "id-asc"
+ assert params == expected
assert not any(k.endswith("_min") for k in params)
@@ -180,6 +188,24 @@ class TestGetRows:
assert mock_session.call_args.kwargs["redact_values"] == ("secret-token",)
+ @patch("posthog.temporal.data_imports.sources.recharge.recharge.make_tracked_session")
+ def test_products_request_omits_sort_by(self, mock_session: MagicMock) -> None:
+ # Regression: the 2021-11 `/products` endpoint 422s on `sort_by`, so the
+ # initial request must send only `limit` (no sort, no timestamp filter).
+ mock_session.return_value.get.return_value = _mock_response(
+ json_body={"products": [{"id": 1}], "next_cursor": None}
+ )
+
+ manager = MagicMock()
+ manager.can_resume.return_value = False
+
+ list(get_rows(api_key="t", endpoint="products", logger=MagicMock(), resumable_source_manager=manager))
+
+ requested_url = mock_session.return_value.get.call_args.args[0]
+ assert "products?limit=250" in requested_url
+ assert "sort_by" not in requested_url
+ assert "_min" not in requested_url
+
@patch("posthog.temporal.data_imports.sources.recharge.recharge.make_tracked_session")
def test_resumes_from_saved_cursor_when_endpoint_matches(self, mock_session: MagicMock) -> None:
mock_session.return_value.get.return_value = _mock_response(
diff --git a/posthog/temporal/data_imports/sources/recharge/tests/test_recharge_source.py b/posthog/temporal/data_imports/sources/recharge/tests/test_recharge_source.py
index 836d286ac541..a5f4dc7fbc83 100644
--- a/posthog/temporal/data_imports/sources/recharge/tests/test_recharge_source.py
+++ b/posthog/temporal/data_imports/sources/recharge/tests/test_recharge_source.py
@@ -52,6 +52,14 @@ class TestRechargeSourceGetSchemas:
assert collections.supports_incremental is False
assert collections.incremental_fields == []
+ def test_products_is_full_refresh_only(self) -> None:
+ # The 2021-11 `/products` endpoint has no `sort_by` or `*_min` filter, so
+ # it can't sync incrementally — it must be advertised as full-refresh.
+ schemas = RechargeSource().get_schemas(_config(), team_id=1)
+ products = next(s for s in schemas if s.name == "products")
+ assert products.supports_incremental is False
+ assert products.incremental_fields == []
+
def test_filters_by_names(self) -> None:
schemas = RechargeSource().get_schemas(_config(), team_id=1, names=["customers", "orders"])
assert {s.name for s in schemas} == {"customers", "orders"}