Skip to content

chore(backend): Add webhook event types for Commerce related events #6338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

mauricioabreu
Copy link
Contributor

@mauricioabreu mauricioabreu commented Jul 16, 2025

We recently introduced webhook events for Commerce: payment attempts and subscription events.

This commit adds type safety so developer can create type-safe webhook handlers.

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

  • New Features
    • Introduced new commerce-related resource types for payers, payment attempts, subscriptions, and subscription items.
    • Added support for new webhook events related to commerce payment attempts, subscriptions, and subscription items.
    • Expanded available JSON data types for commerce entities, making them accessible for integration and development.

We recently introduced webhook events for Commerce:
payment attempts and subscription events.

This commit adds type safety so developer can create type-safe
webhook handlers.
Copy link

changeset-bot bot commented Jul 16, 2025

🦋 Changeset detected

Latest commit: c5b082b

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

This PR includes changesets to release 11 packages
Name Type
@clerk/backend Minor
@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 Jul 16, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 22, 2025 1:07am

Copy link

pkg-pr-new bot commented Jul 16, 2025

Open in StackBlitz

@clerk/agent-toolkit

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

@clerk/astro

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

@clerk/backend

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

@clerk/chrome-extension

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

@clerk/clerk-js

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

@clerk/dev-cli

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

@clerk/elements

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

@clerk/clerk-expo

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

@clerk/expo-passkeys

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

@clerk/express

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

@clerk/fastify

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

@clerk/localizations

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

@clerk/nextjs

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

@clerk/nuxt

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

@clerk/clerk-react

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

@clerk/react-router

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

@clerk/remix

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

@clerk/shared

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

@clerk/tanstack-react-start

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

@clerk/testing

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

@clerk/themes

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

@clerk/types

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

@clerk/upgrade

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

@clerk/vue

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

commit: c5b082b

Copy link
Contributor

coderabbitai bot commented Jul 16, 2025

📝 Walkthrough

Walkthrough

This change introduces new commerce-related resource types and webhook events to the backend API. It adds several TypeScript interfaces representing commerce entities such as payers, payees, payment attempts, subscriptions, subscription items, plans, payment sources, amounts, totals, credits, and payment failure reasons. These interfaces are incorporated into the resource model and exported for use elsewhere in the codebase. Additionally, new webhook event types are defined for commerce payment attempts, subscriptions, and subscription items, and these are included in the union of recognized webhook events. No existing code or interfaces are modified or removed.

Estimated code review effort

2 (10–30 minutes)


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f0b00a4 and c5b082b.

📒 Files selected for processing (1)
  • packages/backend/src/api/resources/JSON.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/backend/src/api/resources/JSON.ts
⏰ 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). (5)
  • GitHub Check: Build Packages
  • GitHub Check: Formatting | Dedupe | Changeset
  • GitHub Check: semgrep/ci
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: semgrep-cloud-platform/scan

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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: 0

🧹 Nitpick comments (1)
packages/backend/src/api/resources/JSON.ts (1)

778-783: Consider extending ClerkResourceJSON for consistency.

The CommercePayeeJSON interface doesn't extend ClerkResourceJSON unlike other commerce entities. Consider whether this should be consistent with the pattern used by other commerce interfaces.

-export interface CommercePayeeJSON {
+export interface CommercePayeeJSON extends ClerkResourceJSON {
+  object: typeof ObjectType.CommercePayee;
   id: string;
   gateway_type: string;
   gateway_external_id: string;
   gateway_status: string;
 }

Note: This would require adding CommercePayee: 'commerce_payee' to the ObjectType constant.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6041c39 and 503fcca.

📒 Files selected for processing (3)
  • packages/backend/src/api/resources/JSON.ts (2 hunks)
  • packages/backend/src/api/resources/Webhooks.ts (3 hunks)
  • packages/backend/src/index.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (7)
**/*.{js,jsx,ts,tsx}

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .cursor/rules/development.mdc
packages/**/*.ts

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .cursor/rules/development.mdc
packages/**/*.{ts,tsx,d.ts}

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .cursor/rules/development.mdc
packages/**/index.{js,ts}

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .cursor/rules/development.mdc
**/*.{ts,tsx}

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .cursor/rules/development.mdc
  • .cursor/rules/typescript.mdc
**/index.ts

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • .cursor/rules/react.mdc
  • .cursor/rules/typescript.mdc
**/*

Instructions used from:

Sources:
⚙️ CodeRabbit Configuration File

🧠 Learnings (4)
📓 Common learnings
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/nextjs.mdc:0-0
Timestamp: 2025-06-30T10:31:43.578Z
Learning: Applies to app/api/**/*.ts : Use TypeScript interfaces for API response types
Learnt from: panteliselef
PR: clerk/javascript#6327
File: .changeset/eight-socks-lead.md:2-4
Timestamp: 2025-07-16T10:43:17.696Z
Learning: In the Clerk JavaScript repository, APIs marked with @experimental JSDoc annotations can have breaking changes released with minor version bumps rather than major version bumps, as consumers are warned about the instability of experimental features. The billing feature specifically has an @experimental annotation stating "This is an experimental API for the Billing feature that is available under a public beta, and the API is subject to change."
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/nextjs.mdc:0-0
Timestamp: 2025-06-30T10:31:43.578Z
Learning: Applies to app/api/**/*.ts : Document API endpoints with TypeScript interfaces
packages/backend/src/index.ts (5)
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/development.mdc:0-0
Timestamp: 2025-06-30T10:29:42.997Z
Learning: Applies to packages/**/*.{ts,tsx,d.ts} : Packages should export TypeScript types alongside runtime code
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/development.mdc:0-0
Timestamp: 2025-06-30T10:29:42.997Z
Learning: Applies to packages/**/index.{js,ts} : Use tree-shaking friendly exports
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/typescript.mdc:0-0
Timestamp: 2025-06-30T10:33:45.961Z
Learning: Applies to **/*.{ts,tsx} : Proper tree-shaking friendly exports
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/typescript.mdc:0-0
Timestamp: 2025-06-30T10:33:45.961Z
Learning: Applies to **/*.{ts,tsx} : Use ES6 imports/exports consistently
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/typescript.mdc:0-0
Timestamp: 2025-06-30T10:33:45.961Z
Learning: Applies to **/index.ts : Avoid barrel files (index.ts re-exports) as they can cause circular dependencies
packages/backend/src/api/resources/Webhooks.ts (2)
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/react.mdc:0-0
Timestamp: 2025-06-30T10:32:37.848Z
Learning: Applies to **/*.tsx : Use proper event types for handlers
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/typescript.mdc:0-0
Timestamp: 2025-06-30T10:33:45.961Z
Learning: Applies to **/*.{ts,tsx} : Use `type` for unions, primitives, and computed types
packages/backend/src/api/resources/JSON.ts (8)
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/typescript.mdc:0-0
Timestamp: 2025-06-30T10:33:45.961Z
Learning: Applies to **/*.{ts,tsx} : Use `interface` for object shapes that might be extended
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/nextjs.mdc:0-0
Timestamp: 2025-06-30T10:31:43.578Z
Learning: Applies to app/api/**/*.ts : Use TypeScript interfaces for API response types
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/nextjs.mdc:0-0
Timestamp: 2025-06-30T10:31:43.578Z
Learning: Applies to app/api/**/*.ts : Document API endpoints with TypeScript interfaces
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/typescript.mdc:0-0
Timestamp: 2025-06-30T10:33:45.961Z
Learning: Applies to **/__tests__/**/*.{ts,tsx} : Implement proper mock types that match interfaces in tests
Learnt from: wobsoriano
PR: clerk/javascript#6229
File: packages/backend/src/api/endpoints/MachineTokensApi.ts:47-89
Timestamp: 2025-07-01T15:20:41.834Z
Learning: In the Clerk JavaScript repository, for the MachineTokensApi class (packages/backend/src/api/endpoints/MachineTokensApi.ts), the maintainers prefer to rely on TypeScript types and readable property names for API documentation rather than JSDoc comments.
Learnt from: CR
PR: clerk/javascript#0
File: .cursor/rules/typescript.mdc:0-0
Timestamp: 2025-06-30T10:33:45.961Z
Learning: Applies to **/*.{ts,tsx} : Use mapped types for transforming object types
Learnt from: panteliselef
PR: clerk/javascript#6327
File: .changeset/eight-socks-lead.md:2-4
Timestamp: 2025-07-16T10:43:17.696Z
Learning: In the Clerk JavaScript repository, APIs marked with @experimental JSDoc annotations can have breaking changes released with minor version bumps rather than major version bumps, as consumers are warned about the instability of experimental features. The billing feature specifically has an @experimental annotation stating "This is an experimental API for the Billing feature that is available under a public beta, and the API is subject to change."
Learnt from: panteliselef
PR: clerk/javascript#6327
File: .changeset/eight-socks-lead.md:2-4
Timestamp: 2025-07-16T10:43:17.696Z
Learning: In the Clerk JavaScript repository, APIs marked with @experimental JSDoc annotations can have breaking changes released with minor version bumps rather than major version bumps, as consumers are warned about the instability of experimental features.
🧬 Code Graph Analysis (1)
packages/backend/src/api/resources/JSON.ts (2)
packages/backend/src/index.ts (12)
  • CommercePayerJSON (103-103)
  • ClerkResourceJSON (59-59)
  • CommercePayeeJSON (104-104)
  • CommerceAmountJSON (105-105)
  • CommerceTotalsJSON (106-106)
  • CommercePaymentSourceJSON (107-107)
  • CommercePaymentFailedReasonJSON (108-108)
  • CommerceSubscriptionCreditJSON (109-109)
  • CommercePlanJSON (110-110)
  • CommerceSubscriptionItemJSON (111-111)
  • CommercePaymentAttemptJSON (112-112)
  • CommerceSubscriptionJSON (113-113)
packages/types/src/json.ts (4)
  • ClerkResourceJSON (35-39)
  • CommercePaymentSourceJSON (693-703)
  • CommercePlanJSON (629-666)
  • CommerceSubscriptionJSON (781-797)
⏰ 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). (5)
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: Build Packages
  • GitHub Check: Formatting | Dedupe | Changeset
  • GitHub Check: semgrep/ci
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (15)
packages/backend/src/index.ts (1)

103-113: LGTM! Commerce JSON type exports follow established patterns.

The new commerce-related JSON type exports are correctly added to the existing JSON types section and follow the established naming conventions and export patterns.

packages/backend/src/api/resources/Webhooks.ts (3)

2-4: LGTM! Import additions for commerce JSON types.

The new imports for commerce-related JSON types are correctly added and follow the established import pattern.


68-89: LGTM! Comprehensive commerce webhook event types added.

The new webhook event types are well-structured and follow the established pattern:

  • CommercePaymentAttemptWebhookEvent covers payment attempt lifecycle events
  • CommerceSubscriptionWebhookEvent handles subscription state changes
  • CommerceSubscriptionItemWebhookEvent provides comprehensive coverage of subscription item states

The event type names are descriptive and follow consistent naming conventions.


103-105: LGTM! Webhook event union type properly updated.

The new commerce webhook event types are correctly added to the WebhookEvent union type, maintaining type safety for webhook event handling.

packages/backend/src/api/resources/JSON.ts (11)

66-69: LGTM! ObjectType entries for commerce entities follow established pattern.

The new ObjectType entries for commerce entities are correctly added and follow the established naming convention with snake_case values.


764-776: LGTM! CommercePayerJSON interface is well-structured.

The interface properly extends ClerkResourceJSON and includes comprehensive payer information for both user and organization contexts with appropriate optional fields.


785-790: LGTM! CommerceAmountJSON interface provides comprehensive amount representation.

The interface includes both raw amount and formatted display values with proper currency information.


792-796: LGTM! CommerceTotalsJSON interface provides clear billing breakdown.

The interface properly composes CommerceAmountJSON for different total types, providing a clear billing structure.


798-807: LGTM! CommercePaymentSourceJSON interface covers payment method details.

The interface includes comprehensive payment source information with appropriate optional fields for card-specific details.


809-812: LGTM! CommercePaymentFailedReasonJSON interface provides clear error information.

The interface includes both general error codes and specific decline codes for comprehensive error handling.


814-819: LGTM! CommerceSubscriptionCreditJSON interface provides comprehensive credit tracking.

The interface includes both amount and cycle information with percentage calculations for credit management.


821-838: LGTM! CommercePlanJSON interface provides comprehensive plan details.

The interface includes all necessary plan information including pricing, billing periods, and configuration flags.


840-856: LGTM! CommerceSubscriptionItemJSON interface is well-structured.

The interface properly extends ClerkResourceJSON and includes comprehensive subscription item details with proper composition of related interfaces.


858-877: LGTM! CommercePaymentAttemptJSON interface provides comprehensive payment attempt tracking.

The interface properly extends ClerkResourceJSON and includes all necessary payment attempt details with proper composition of related interfaces for payer, payee, totals, and payment source information.


879-893: LGTM! CommerceSubscriptionJSON interface provides comprehensive subscription management.

The interface properly extends ClerkResourceJSON and includes all necessary subscription details with proper composition of related interfaces for payer and subscription items.

currency_symbol: string;
}

export interface CommerceTotalsJSON {
Copy link
Member

Choose a reason for hiding this comment

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

For FAPI we also have total_due_now and credit. They may not be needed here, but wanted to mention it just in case.

Use union types for every status field.
No longer being used.
@mauricioabreu mauricioabreu merged commit 2bbeaf3 into main Jul 24, 2025
36 checks passed
@mauricioabreu mauricioabreu deleted the mauricio-antunes/com-1039-export-billing-webhook-types-from-clerkbackend branch July 24, 2025 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants