Skip to content

Conversation

panteliselef
Copy link
Member

@panteliselef panteliselef commented Sep 25, 2025

Description

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Summary by CodeRabbit

  • Refactor
    • Standardized billing API endpoint construction.
    • Billing API base path changed from /commerce to /billing; update integrations accordingly. Endpoints for plans, subscriptions, statements, payment sources, and checkouts now reside under /billing.
  • Chores
    • Upgraded authentication-related libraries to the latest patch versions for improved stability.
    • Added a changeset to release the updates.

No functional behavior changes beyond the new billing endpoint paths.

Copy link

changeset-bot bot commented Sep 25, 2025

🦋 Changeset detected

Latest commit: c362b20

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
@clerk/clerk-js Patch
@clerk/backend Patch
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch
@clerk/agent-toolkit Patch
@clerk/astro Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/remix Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Sep 25, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
clerk-js-sandbox Ready Ready Preview Comment Sep 25, 2025 9:46am

Copy link
Contributor

coderabbitai bot commented Sep 25, 2025

Walkthrough

Updates the billing API base path from /commerce to /billing. Adds a centralized Billing.path helper in clerk-js and refactors billing-related client calls to use it. Adjusts multiple clerk-js resources to route through the helper. Includes a changeset patching @clerk/clerk-js and @clerk/backend.

Changes

Cohort / File(s) Summary of Changes
Changeset and versions
\.changeset/young-nails-matter.md
Adds a changeset: bumps @clerk/clerk-js and @clerk/backend; documents public API path change to /billing.
Backend billing base path
packages/backend/src/api/endpoints/BillingApi.ts
Changes basePath from '/commerce' to '/billing' for billing endpoints; no interface changes.
Path helper introduction and adoption (clerk-js billing)
packages/clerk-js/src/core/modules/billing/namespace.ts, packages/clerk-js/src/core/modules/billing/payment-source-methods.ts, packages/clerk-js/src/core/resources/BillingCheckout.ts, packages/clerk-js/src/core/resources/BillingPaymentSource.ts, packages/clerk-js/src/core/resources/BillingSubscription.ts
Adds Billing.path(subPath, { orgId? }) with private #pathRoot; replaces hardcoded URLs across billing modules/resources to construct paths via the helper (plans, subscription, statements, payment attempts, checkouts, payment sources, payer default, subscription item delete). Core fetch/error handling unchanged.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant App as App (Client)
  participant ClerkJS as clerk-js Billing
  participant Fetch as BaseResource._fetch
  participant API as Backend /billing API

  User->>App: Initiates billing action
  App->>ClerkJS: call Billing.* method
  note over ClerkJS: Build URL via Billing.path("/...",{ orgId? })<br/>Path root: "/me/billing"
  ClerkJS->>Fetch: _fetch(constructed URL, options)
  Fetch->>API: HTTP request to /billing/... (server base)
  API-->>Fetch: Response
  Fetch-->>ClerkJS: Parsed result
  ClerkJS-->>App: Return data
  App-->>User: Render result

  note right of ClerkJS: New/changed: centralized URL construction
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I twitch my whiskers, paths align,
From commerce trails to billing’s sign.
A single burrow, neat and bright,
Builds every tunnel just right.
With hops of joy, I patch and cheer—
URLs tidy, carrots near! 🥕✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly and accurately summarizes the primary change of the pull request—replacing /commerce endpoints with /billing endpoints across the clerk-js and backend packages—without extraneous details or vague language.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch elef/bill-1279-js-sdks-to-use-billing-path-instead-of-commerce

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

pkg-pr-new bot commented Sep 25, 2025

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@6854

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@6854

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@6854

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@6854

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@6854

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@6854

@clerk/elements

npm i https://pkg.pr.new/@clerk/elements@6854

@clerk/clerk-expo

npm i https://pkg.pr.new/@clerk/clerk-expo@6854

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@6854

@clerk/express

npm i https://pkg.pr.new/@clerk/express@6854

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@6854

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@6854

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@6854

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@6854

@clerk/clerk-react

npm i https://pkg.pr.new/@clerk/clerk-react@6854

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@6854

@clerk/remix

npm i https://pkg.pr.new/@clerk/remix@6854

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@6854

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@6854

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@6854

@clerk/themes

npm i https://pkg.pr.new/@clerk/themes@6854

@clerk/types

npm i https://pkg.pr.new/@clerk/types@6854

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@6854

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@6854

commit: c362b20

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
packages/clerk-js/src/core/resources/BillingPaymentSource.ts (1)

47-53: Fix Billing.path so org routes don’t collapse to /me

The new helper currently does:

let prefix = '';
if (orgId) {
  prefix = `/organizations/${orgId}`;
}
prefix = '/me';

That final assignment overwrites the organization prefix, so every call you switched over—including this DELETE—now goes to /me/billing/... even when orgId is provided. Organization payment-source operations will 404/regress.

Please fix the helper in packages/clerk-js/src/core/modules/billing/namespace.ts so it only falls back to /me when orgId is absent:

-    let prefix = '';
-    if (orgId) {
-      prefix = `/organizations/${orgId}`;
-    }
-    prefix = '/me';
+    const prefix = orgId ? `/organizations/${orgId}` : '/me';
packages/clerk-js/src/core/modules/billing/payment-source-methods.ts (1)

18-34: Organization billing endpoints currently resolve to the wrong base

Every call you switched to Billing.path (Line 18, 31, 43) now inherits the helper bug where /me is always selected, so organization payment-source operations will target /me/billing/... instead of /organizations/${orgId}/billing/... and 404/403. Once the helper is corrected as noted in the other comment, these requests will route properly.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 2d4d8cb and c362b20.

📒 Files selected for processing (7)
  • .changeset/young-nails-matter.md (1 hunks)
  • packages/backend/src/api/endpoints/BillingApi.ts (1 hunks)
  • packages/clerk-js/src/core/modules/billing/namespace.ts (7 hunks)
  • packages/clerk-js/src/core/modules/billing/payment-source-methods.ts (3 hunks)
  • packages/clerk-js/src/core/resources/BillingCheckout.ts (2 hunks)
  • packages/clerk-js/src/core/resources/BillingPaymentSource.ts (3 hunks)
  • packages/clerk-js/src/core/resources/BillingSubscription.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

**/*.{js,jsx,ts,tsx}: All code must pass ESLint checks with the project's configuration
Follow established naming conventions (PascalCase for components, camelCase for variables)
Maintain comprehensive JSDoc comments for public APIs
Use dynamic imports for optional features
All public APIs must be documented with JSDoc
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Profile and optimize critical paths
Validate all inputs and sanitize outputs
Implement proper logging with different levels

Files:

  • packages/clerk-js/src/core/resources/BillingSubscription.ts
  • packages/clerk-js/src/core/resources/BillingPaymentSource.ts
  • packages/clerk-js/src/core/resources/BillingCheckout.ts
  • packages/backend/src/api/endpoints/BillingApi.ts
  • packages/clerk-js/src/core/modules/billing/namespace.ts
  • packages/clerk-js/src/core/modules/billing/payment-source-methods.ts
**/*.{js,jsx,ts,tsx,json,css,scss,md,yaml,yml}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use Prettier for consistent code formatting

Files:

  • packages/clerk-js/src/core/resources/BillingSubscription.ts
  • packages/clerk-js/src/core/resources/BillingPaymentSource.ts
  • packages/clerk-js/src/core/resources/BillingCheckout.ts
  • packages/backend/src/api/endpoints/BillingApi.ts
  • packages/clerk-js/src/core/modules/billing/namespace.ts
  • packages/clerk-js/src/core/modules/billing/payment-source-methods.ts
packages/**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

TypeScript is required for all packages

Files:

  • packages/clerk-js/src/core/resources/BillingSubscription.ts
  • packages/clerk-js/src/core/resources/BillingPaymentSource.ts
  • packages/clerk-js/src/core/resources/BillingCheckout.ts
  • packages/backend/src/api/endpoints/BillingApi.ts
  • packages/clerk-js/src/core/modules/billing/namespace.ts
  • packages/clerk-js/src/core/modules/billing/payment-source-methods.ts
packages/**/*.{ts,tsx,d.ts}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Packages should export TypeScript types alongside runtime code

Files:

  • packages/clerk-js/src/core/resources/BillingSubscription.ts
  • packages/clerk-js/src/core/resources/BillingPaymentSource.ts
  • packages/clerk-js/src/core/resources/BillingCheckout.ts
  • packages/backend/src/api/endpoints/BillingApi.ts
  • packages/clerk-js/src/core/modules/billing/namespace.ts
  • packages/clerk-js/src/core/modules/billing/payment-source-methods.ts
**/*.{ts,tsx}

📄 CodeRabbit inference engine (.cursor/rules/development.mdc)

Use proper TypeScript error types

**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoid any type - prefer unknown when type is uncertain, then narrow with type guards
Use interface for object shapes that might be extended
Use type for unions, primitives, and computed types
Prefer readonly properties for immutable data structures
Use private for internal implementation details
Use protected for inheritance hierarchies
Use public explicitly for clarity in public APIs
Prefer readonly for properties that shouldn't change after construction
Prefer composition and interfaces over deep inheritance chains
Use mixins for shared behavior across unrelated classes
Implement dependency injection for loose coupling
Let TypeScript infer when types are obvious
Use const assertions for literal types: as const
Use satisfies operator for type checking without widening
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Use type-only imports: import type { ... } from ...
No any types without justification
Proper error handling with typed errors
Consistent use of readonly for immutable data
Proper generic constraints
No unused type parameters
Proper use of utility types instead of manual type construction
Type-only imports where possible
Proper tree-shaking friendly exports
No circular dependencies
Efficient type computations (avoid deep recursion)

Files:

  • packages/clerk-js/src/core/resources/BillingSubscription.ts
  • packages/clerk-js/src/core/resources/BillingPaymentSource.ts
  • packages/clerk-js/src/core/resources/BillingCheckout.ts
  • packages/backend/src/api/endpoints/BillingApi.ts
  • packages/clerk-js/src/core/modules/billing/namespace.ts
  • packages/clerk-js/src/core/modules/billing/payment-source-methods.ts
**/*.{js,ts,tsx,jsx}

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Support multiple Clerk environment variables (CLERK_, NEXT_PUBLIC_CLERK_, etc.) for configuration.

Files:

  • packages/clerk-js/src/core/resources/BillingSubscription.ts
  • packages/clerk-js/src/core/resources/BillingPaymentSource.ts
  • packages/clerk-js/src/core/resources/BillingCheckout.ts
  • packages/backend/src/api/endpoints/BillingApi.ts
  • packages/clerk-js/src/core/modules/billing/namespace.ts
  • packages/clerk-js/src/core/modules/billing/payment-source-methods.ts
.changeset/**

📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)

Automated releases must use Changesets.

Files:

  • .changeset/young-nails-matter.md
🧬 Code graph analysis (5)
packages/clerk-js/src/core/resources/BillingSubscription.ts (1)
packages/clerk-js/src/core/modules/billing/namespace.ts (1)
  • Billing (30-142)
packages/clerk-js/src/core/resources/BillingPaymentSource.ts (1)
packages/clerk-js/src/core/modules/billing/namespace.ts (1)
  • Billing (30-142)
packages/clerk-js/src/core/resources/BillingCheckout.ts (1)
packages/clerk-js/src/core/modules/billing/namespace.ts (1)
  • Billing (30-142)
packages/clerk-js/src/core/modules/billing/namespace.ts (4)
packages/types/src/billing.ts (3)
  • GetPlansParams (110-115)
  • BillingPlanResource (122-189)
  • GetSubscriptionParams (472-474)
packages/types/src/pagination.ts (1)
  • ClerkPaginatedResponse (22-31)
packages/types/src/json.ts (1)
  • BillingPlanJSON (643-669)
packages/clerk-js/src/core/resources/BillingPlan.ts (1)
  • BillingPlan (7-54)
packages/clerk-js/src/core/modules/billing/payment-source-methods.ts (2)
packages/types/src/billing.ts (1)
  • InitializePaymentSourceParams (240-245)
packages/clerk-js/src/core/modules/billing/namespace.ts (1)
  • Billing (30-142)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Build Packages
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: semgrep-cloud-platform/scan
🔇 Additional comments (2)
packages/backend/src/api/endpoints/BillingApi.ts (1)

10-10: Base path update aligns with new billing routes.

The switch to /billing keeps the backend in step with the frontend refactor.

.changeset/young-nails-matter.md (1)

1-6: Changeset looks good.

Package bumps and summary read clean—no issues from my side.

Comment on lines +31 to +40
static readonly #pathRoot = '/billing';
static path(subPath: string, param?: { orgId?: string }): string {
const { orgId } = param || {};
let prefix = '';
if (orgId) {
prefix = `/organizations/${orgId}`;
}
prefix = '/me';
return `${prefix}${Billing.#pathRoot}${subPath}`;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Org-specific billing routes are now broken.

Billing.path always resets the prefix to /me, so every caller—including those that pass orgId—now hits /me/billing/.... This regresses all organization-scoped billing operations (statements, payment sources, subscription items, checkout confirmation, etc.), which will 404 or operate on the wrong tenant. Please restore the conditional prefix so that orgId-backed requests keep using /organizations/{orgId}/billing/....

-    let prefix = '';
-    if (orgId) {
-      prefix = `/organizations/${orgId}`;
-    }
-    prefix = '/me';
-    return `${prefix}${Billing.#pathRoot}${subPath}`;
+    const prefix = orgId ? `/organizations/${orgId}` : '/me';
+    return `${prefix}${Billing.#pathRoot}${subPath}`;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
static readonly #pathRoot = '/billing';
static path(subPath: string, param?: { orgId?: string }): string {
const { orgId } = param || {};
let prefix = '';
if (orgId) {
prefix = `/organizations/${orgId}`;
}
prefix = '/me';
return `${prefix}${Billing.#pathRoot}${subPath}`;
}
static readonly #pathRoot = '/billing';
static path(subPath: string, param?: { orgId?: string }): string {
const { orgId } = param || {};
const prefix = orgId ? `/organizations/${orgId}` : '/me';
return `${prefix}${Billing.#pathRoot}${subPath}`;
}
🤖 Prompt for AI Agents
In packages/clerk-js/src/core/modules/billing/namespace.ts around lines 31 to
40, the prefix is unconditionally reset to '/me' which ignores orgId and breaks
organization-scoped billing routes; restore the conditional so when param.orgId
is provided the prefix remains `/organizations/${orgId}` and only use '/me' when
no orgId is present, then return `${prefix}${Billing.#pathRoot}${subPath}` as
before; ensure no other code overrides prefix after the orgId check.

Comment on lines +64 to 66
path: Billing.path(`/checkouts/${this.id}/confirm`, { orgId: this.payer.organizationId }),
body: params as any,
}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Confirm checkout fails for organization payers

Line 64 relies on Billing.path, which currently ignores orgId and always returns the /me/billing/... path. As a result, confirming an organization checkout will call the wrong endpoint. Please fix Billing.path to respect the organization prefix before merging.

🤖 Prompt for AI Agents
In packages/clerk-js/src/core/resources/BillingCheckout.ts around lines 64 to
66, the call to Billing.path(..., { orgId: this.payer.organizationId }) fails
because Billing.path currently ignores orgId and always returns a
/me/billing/... path; update Billing.path to prepend the organization prefix
when an orgId is provided (i.e., return /organizations/{orgId}/billing/...
instead of /me/billing/...), keep the existing behavior when orgId is undefined,
and ensure the path builder is used consistently by other callers (adjust or add
tests to cover both org and me paths).

Comment on lines +114 to 116
path: Billing.path(`/subscription_items/${this.id}`, { orgId }),
method: 'DELETE',
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Restore organization billing routes before merging

Line 114: Billing.path currently disregards orgId because the helper always overwrites the prefix with /me after the conditional assignment. As soon as this method is called for an organization subscription item, we’ll hit /me/billing/subscription_items/... instead of /organizations/${orgId}/billing/..., causing the cancel call to fail. Please fix the helper so it preserves the organization prefix.

 static path(subPath: string, param?: { orgId?: string }): string {
   const { orgId } = param || {};
-  let prefix = '';
-  if (orgId) {
-    prefix = `/organizations/${orgId}`;
-  }
-  prefix = '/me';
+  const prefix = orgId ? `/organizations/${orgId}` : '/me';
   return `${prefix}${Billing.#pathRoot}${subPath}`;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
path: Billing.path(`/subscription_items/${this.id}`, { orgId }),
method: 'DELETE',
})
static path(subPath: string, param?: { orgId?: string }): string {
const { orgId } = param || {};
const prefix = orgId ? `/organizations/${orgId}` : '/me';
return `${prefix}${Billing.#pathRoot}${subPath}`;
}
🤖 Prompt for AI Agents
In packages/clerk-js/src/core/resources/BillingSubscription.ts around lines
114-116, the call uses Billing.path(..., { orgId }) but the Billing.path helper
currently overwrites the computed prefix with "/me" unconditionally; change the
helper so it only uses "/me" when orgId is absent and when orgId is present it
sets and preserves the prefix to "/organizations/{orgId}" (do not overwrite it
later), update any conditional assignment/order so orgId wins, and add/adjust
unit tests to verify paths produce "/organizations/{orgId}/billing/..." when
orgId is provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants