@commet/node 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,13 +13,26 @@
13
13
 
14
14
  <br/>
15
15
 
16
+ [Commet](https://commet.co) is an all-in-one billing and payments platform for SaaS and AI products. `@commet/node` is the core SDK — use it to manage customers, track usage, handle subscriptions, and more from your server.
17
+
16
18
  ## Installation
17
19
 
18
20
  ```bash
19
21
  npm install @commet/node
20
22
  ```
21
23
 
22
- ## Quick Start
24
+ ## Getting Started
25
+
26
+ ### 1. Create a Commet account
27
+
28
+ Sign up at [commet.co](https://commet.co) and create an organization. Go to **Settings → API Keys** to get your key.
29
+
30
+ ```bash
31
+ # .env
32
+ COMMET_API_KEY=ck_...
33
+ ```
34
+
35
+ ### 2. Initialize the SDK
23
36
 
24
37
  ```typescript
25
38
  import { Commet } from '@commet/node';
@@ -30,9 +43,22 @@ const commet = new Commet({
30
43
  });
31
44
  ```
32
45
 
33
- ## Usage
46
+ ### 3. Start using it
34
47
 
35
48
  ```typescript
49
+ // Create a customer
50
+ const customer = await commet.customers.create({
51
+ fullName: 'Acme Corp',
52
+ billingEmail: 'billing@acme.com'
53
+ });
54
+
55
+ // Subscribe them to a plan
56
+ await commet.subscriptions.create({
57
+ externalId: 'user_123',
58
+ planCode: 'pro', // autocomplete works after `commet pull`
59
+ billingInterval: 'yearly',
60
+ });
61
+
36
62
  // Track usage events
37
63
  await commet.usage.create({
38
64
  feature: 'api_call',
@@ -47,22 +73,15 @@ await commet.seats.add({
47
73
  count: 5
48
74
  });
49
75
 
50
- // Create subscriptions
51
- await commet.subscriptions.create({
76
+ // Check feature access
77
+ const feature = await commet.features.check({
52
78
  externalId: 'user_123',
53
- planCode: 'pro', // autocomplete works after `commet pull`
54
- billingInterval: 'yearly',
55
- });
56
-
57
- // Manage customers
58
- await commet.customers.create({
59
- fullName: 'Acme Corp',
60
- billingEmail: 'billing@acme.com'
79
+ code: 'api_calls'
61
80
  });
62
81
 
63
- // Generate customer portal access
64
- await commet.portal.getUrl({
65
- externalId: 'my-customer-123'
82
+ // Generate customer portal link
83
+ const portal = await commet.portal.getUrl({
84
+ externalId: 'user_123'
66
85
  });
67
86
  ```
68
87
 
package/dist/index.d.mts CHANGED
@@ -41,7 +41,7 @@ declare class CommetValidationError extends CommetError {
41
41
  validationErrors: Record<string, string[]>;
42
42
  constructor(message: string, validationErrors: Record<string, string[]>);
43
43
  }
44
- type CustomerID = `cus_${string}`;
44
+ type CustomerID = string;
45
45
  type EventID = `evt_${string}`;
46
46
  type Currency = "USD" | "EUR" | "GBP" | "CAD" | "AUD" | "JPY" | "ARS" | "BRL" | "MXN" | "CLP";
47
47
  interface ListParams extends Record<string, unknown> {
@@ -116,7 +116,7 @@ declare class CommetHTTPClient {
116
116
  }
117
117
 
118
118
  interface FeatureParams {
119
- externalId: string;
119
+ customerId: CustomerID;
120
120
  code: GeneratedFeatureCode;
121
121
  }
122
122
  type GetFeatureParams = FeatureParams;
@@ -144,73 +144,13 @@ interface CanUseResult {
144
144
  interface CheckResult {
145
145
  allowed: boolean;
146
146
  }
147
- /**
148
- * Features resource for checking feature access and usage
149
- *
150
- * Provides a clean API to check if a customer can use a feature
151
- * without having to manually parse subscription data.
152
- */
153
147
  declare class FeaturesResource {
154
148
  private httpClient;
155
149
  constructor(httpClient: CommetHTTPClient);
156
- /**
157
- * Get detailed feature access/usage for a customer
158
- *
159
- * @example
160
- * ```typescript
161
- * const seats = await commet.features.get({ code: "team_members", externalId: "user_123" });
162
- * console.log(seats.current, seats.included, seats.remaining);
163
- * ```
164
- */
165
150
  get(params: GetFeatureParams, options?: RequestOptions): Promise<ApiResponse<FeatureAccess>>;
166
- /**
167
- * Check if a boolean feature is enabled for a customer
168
- *
169
- * @example
170
- * ```typescript
171
- * const { allowed } = await commet.features.check({
172
- * code: "custom_branding",
173
- * externalId: "user_123"
174
- * });
175
- * if (!allowed) redirect("/upgrade");
176
- * ```
177
- */
178
151
  check(params: CheckFeatureParams, options?: RequestOptions): Promise<ApiResponse<CheckResult>>;
179
- /**
180
- * Check if customer can use one more unit of a feature
181
- *
182
- * Returns whether the customer can add one more (allowed)
183
- * and whether they'll be charged extra (willBeCharged).
184
- *
185
- * @example
186
- * ```typescript
187
- * const { allowed, willBeCharged } = await commet.features.canUse({
188
- * code: "team_members",
189
- * externalId: "user_123"
190
- * });
191
- *
192
- * if (!allowed) {
193
- * return { error: "Upgrade to add more members" };
194
- * }
195
- *
196
- * if (willBeCharged) {
197
- * // Show confirmation: "This will cost $10/month extra"
198
- * }
199
- * ```
200
- */
201
152
  canUse(params: CanUseFeatureParams, options?: RequestOptions): Promise<ApiResponse<CanUseResult>>;
202
- /**
203
- * List all features for a customer's active subscription
204
- *
205
- * @example
206
- * ```typescript
207
- * const features = await commet.features.list({ externalId: "user_123" });
208
- * for (const feature of features) {
209
- * console.log(feature.code, feature.allowed);
210
- * }
211
- * ```
212
- */
213
- list(externalId: string, options?: RequestOptions): Promise<ApiResponse<FeatureAccess[]>>;
153
+ list(customerId: CustomerID, options?: RequestOptions): Promise<ApiResponse<FeatureAccess[]>>;
214
154
  }
215
155
 
216
156
  interface PortalAccess {
@@ -221,33 +161,15 @@ interface PortalAccess {
221
161
  interface GetUrlByCustomerId {
222
162
  customerId: CustomerID;
223
163
  email?: never;
224
- externalId?: never;
225
- }
226
- interface GetUrlByExternalId {
227
- externalId: string;
228
- email?: never;
229
- customerId?: never;
230
164
  }
231
165
  interface GetUrlByEmail {
232
166
  email: string;
233
167
  customerId?: never;
234
- externalId?: never;
235
168
  }
236
- type GetUrlParams = GetUrlByCustomerId | GetUrlByExternalId | GetUrlByEmail;
237
- /**
238
- * Portal resource - Generate customer portal access
239
- */
169
+ type GetUrlParams = GetUrlByCustomerId | GetUrlByEmail;
240
170
  declare class PortalResource {
241
171
  private httpClient;
242
172
  constructor(httpClient: CommetHTTPClient);
243
- /**
244
- * Get a portal URL
245
- *
246
- * @example
247
- * ```typescript
248
- * const portal = await commet.portal.getUrl({ externalId: 'user_123' });
249
- * ```
250
- */
251
173
  getUrl(params: GetUrlParams, options?: RequestOptions): Promise<ApiResponse<PortalAccess>>;
252
174
  }
253
175
 
@@ -268,116 +190,39 @@ interface SeatBalance {
268
190
  asOf: string;
269
191
  }
270
192
  interface AddParams {
271
- customerId?: CustomerID;
272
- externalId?: string;
193
+ customerId: CustomerID;
273
194
  seatType: GeneratedSeatType;
274
195
  count: number;
275
196
  }
276
197
  interface RemoveParams {
277
- customerId?: CustomerID;
278
- externalId?: string;
198
+ customerId: CustomerID;
279
199
  seatType: GeneratedSeatType;
280
200
  count: number;
281
201
  }
282
202
  interface SetParams {
283
- customerId?: CustomerID;
284
- externalId?: string;
203
+ customerId: CustomerID;
285
204
  seatType: GeneratedSeatType;
286
205
  count: number;
287
206
  }
288
207
  interface SetAllParams {
289
- customerId?: CustomerID;
290
- externalId?: string;
208
+ customerId: CustomerID;
291
209
  seats: Record<string, number>;
292
210
  }
293
211
  interface GetBalanceParams {
294
- customerId?: CustomerID;
295
- externalId?: string;
212
+ customerId: CustomerID;
296
213
  seatType: GeneratedSeatType;
297
214
  }
298
215
  interface GetAllBalancesParams {
299
- customerId?: CustomerID;
300
- externalId?: string;
216
+ customerId: CustomerID;
301
217
  }
302
- /**
303
- * Seats resource - Manage seat-based licenses
304
- */
305
218
  declare class SeatsResource {
306
219
  private httpClient;
307
220
  constructor(httpClient: CommetHTTPClient);
308
- /**
309
- * Add seats
310
- *
311
- * @example
312
- * ```typescript
313
- * await commet.seats.add({
314
- * externalId: 'user_123',
315
- * seatType: 'editor',
316
- * count: 5
317
- * });
318
- * ```
319
- */
320
221
  add(params: AddParams, options?: RequestOptions): Promise<ApiResponse<SeatEvent>>;
321
- /**
322
- * Remove seats
323
- *
324
- * @example
325
- * ```typescript
326
- * await commet.seats.remove({
327
- * externalId: 'user_123',
328
- * seatType: 'editor',
329
- * count: 2
330
- * });
331
- * ```
332
- */
333
222
  remove(params: RemoveParams, options?: RequestOptions): Promise<ApiResponse<SeatEvent>>;
334
- /**
335
- * Set seats to a specific count
336
- *
337
- * @example
338
- * ```typescript
339
- * await commet.seats.set({
340
- * externalId: 'user_123',
341
- * seatType: 'editor',
342
- * count: 10
343
- * });
344
- * ```
345
- */
346
223
  set(params: SetParams, options?: RequestOptions): Promise<ApiResponse<SeatEvent>>;
347
- /**
348
- * Set all seat types
349
- *
350
- * @example
351
- * ```typescript
352
- * await commet.seats.setAll({
353
- * externalId: 'user_123',
354
- * seats: { editor: 10, viewer: 50 }
355
- * });
356
- * ```
357
- */
358
224
  setAll(params: SetAllParams, options?: RequestOptions): Promise<ApiResponse<SeatEvent[]>>;
359
- /**
360
- * Get balance for a seat type
361
- *
362
- * @example
363
- * ```typescript
364
- * const balance = await commet.seats.getBalance({
365
- * externalId: 'user_123',
366
- * seatType: 'editor'
367
- * });
368
- * ```
369
- */
370
225
  getBalance(params: GetBalanceParams): Promise<ApiResponse<SeatBalance>>;
371
- /**
372
- * Get all seat balances
373
- *
374
- * @example
375
- * ```typescript
376
- * const balances = await commet.seats.getAllBalances({
377
- * externalId: 'user_123'
378
- * });
379
- * ```
380
- */
381
226
  getAllBalances(params: GetAllBalancesParams): Promise<ApiResponse<Record<string, SeatBalance>>>;
382
227
  }
383
228
 
@@ -524,7 +369,6 @@ interface ActiveSubscription {
524
369
  interface CreatedSubscription {
525
370
  id: string;
526
371
  customerId: string;
527
- externalId: string;
528
372
  planId: string;
529
373
  planName: string;
530
374
  name: string;
@@ -562,13 +406,6 @@ interface Subscription {
562
406
  createdAt: string;
563
407
  updatedAt: string;
564
408
  }
565
- type CustomerIdentifier = {
566
- customerId: string;
567
- externalId?: never;
568
- } | {
569
- customerId?: never;
570
- externalId: string;
571
- };
572
409
  type PlanIdentifier = {
573
410
  planCode: GeneratedPlanCode;
574
411
  planId?: never;
@@ -576,7 +413,9 @@ type PlanIdentifier = {
576
413
  planCode?: never;
577
414
  planId: string;
578
415
  };
579
- type CreateSubscriptionParams = CustomerIdentifier & PlanIdentifier & {
416
+ type CreateSubscriptionParams = PlanIdentifier & {
417
+ customerId: string;
418
+ } & {
580
419
  billingInterval?: BillingInterval;
581
420
  initialSeats?: Record<string, number>;
582
421
  skipTrial?: boolean;
@@ -619,7 +458,7 @@ declare class SubscriptionsResource {
619
458
  * const sub = await commet.subscriptions.get('user_123');
620
459
  * ```
621
460
  */
622
- get(externalId: string): Promise<ApiResponse<ActiveSubscription | null>>;
461
+ get(customerId: string): Promise<ApiResponse<ActiveSubscription | null>>;
623
462
  /**
624
463
  * Cancel a subscription
625
464
  *
@@ -651,18 +490,9 @@ interface UsageEventProperty {
651
490
  value: string;
652
491
  createdAt: string;
653
492
  }
654
- interface BatchResult$1<T> {
655
- successful: T[];
656
- failed: Array<{
657
- index: number;
658
- error: string;
659
- data: TrackParams;
660
- }>;
661
- }
662
493
  interface TrackBaseParams {
663
494
  feature: GeneratedFeatureCode;
664
- customerId?: CustomerID;
665
- externalId?: string;
495
+ customerId: CustomerID;
666
496
  idempotencyKey?: string;
667
497
  timestamp?: string;
668
498
  properties?: Record<string, string>;
@@ -684,15 +514,12 @@ declare class UsageResource {
684
514
  private httpClient;
685
515
  constructor(httpClient: CommetHTTPClient);
686
516
  track(params: TrackParams, options?: RequestOptions): Promise<ApiResponse<UsageEvent>>;
687
- trackBatch(params: {
688
- events: TrackParams[];
689
- }, options?: RequestOptions): Promise<ApiResponse<BatchResult$1<UsageEvent>>>;
690
517
  }
691
518
 
692
519
  /**
693
520
  * Customer-scoped API context
694
521
  *
695
- * Provides a cleaner API where you don't have to pass externalId
522
+ * Provides a cleaner API where you don't have to pass customerId
696
523
  * on every call. All operations are scoped to a specific customer.
697
524
  *
698
525
  * @example
@@ -706,52 +533,37 @@ declare class UsageResource {
706
533
  * ```
707
534
  */
708
535
  declare class CustomerContext {
709
- private readonly externalId;
536
+ private readonly customerId;
710
537
  private readonly featuresResource;
711
538
  private readonly seatsResource;
712
539
  private readonly usageResource;
713
540
  private readonly subscriptionsResource;
714
541
  private readonly portalResource;
715
- constructor(externalId: string, resources: {
542
+ constructor(customerId: string, resources: {
716
543
  features: FeaturesResource;
717
544
  seats: SeatsResource;
718
545
  usage: UsageResource;
719
546
  subscriptions: SubscriptionsResource;
720
547
  portal: PortalResource;
721
548
  });
722
- /**
723
- * Feature access methods - delegates to FeaturesResource
724
- */
725
549
  features: {
726
550
  get: (code: string, options?: RequestOptions) => Promise<ApiResponse<FeatureAccess>>;
727
551
  check: (code: string, options?: RequestOptions) => Promise<ApiResponse<CheckResult>>;
728
552
  canUse: (code: string, options?: RequestOptions) => Promise<ApiResponse<CanUseResult>>;
729
553
  list: (options?: RequestOptions) => Promise<ApiResponse<FeatureAccess[]>>;
730
554
  };
731
- /**
732
- * Seat management methods - delegates to SeatsResource
733
- */
734
555
  seats: {
735
556
  add: (seatType: string, count?: number, options?: RequestOptions) => Promise<ApiResponse<SeatEvent>>;
736
557
  remove: (seatType: string, count?: number, options?: RequestOptions) => Promise<ApiResponse<SeatEvent>>;
737
558
  set: (seatType: string, count: number, options?: RequestOptions) => Promise<ApiResponse<SeatEvent>>;
738
559
  getBalance: (seatType: string) => Promise<ApiResponse<SeatBalance>>;
739
560
  };
740
- /**
741
- * Usage tracking methods - delegates to UsageResource
742
- */
743
561
  usage: {
744
562
  track: (feature: string, value?: number, properties?: Record<string, string>, options?: RequestOptions) => Promise<ApiResponse<UsageEvent>>;
745
563
  };
746
- /**
747
- * Subscription methods - delegates to SubscriptionsResource
748
- */
749
564
  subscription: {
750
565
  get: () => Promise<ApiResponse<ActiveSubscription | null>>;
751
566
  };
752
- /**
753
- * Portal methods - delegates to PortalResource
754
- */
755
567
  portal: {
756
568
  getUrl: (options?: RequestOptions) => Promise<ApiResponse<PortalAccess>>;
757
569
  };
@@ -810,7 +622,7 @@ interface CustomerAddress {
810
622
  }
811
623
  interface CreateParams {
812
624
  email: string;
813
- externalId?: string;
625
+ id?: string;
814
626
  fullName?: string;
815
627
  domain?: string;
816
628
  website?: string;
@@ -822,7 +634,6 @@ interface CreateParams {
822
634
  }
823
635
  interface UpdateParams {
824
636
  customerId: CustomerID;
825
- externalId?: string;
826
637
  email?: string;
827
638
  fullName?: string;
828
639
  domain?: string;
@@ -831,9 +642,9 @@ interface UpdateParams {
831
642
  language?: string;
832
643
  industry?: string;
833
644
  metadata?: Record<string, unknown>;
645
+ address?: CustomerAddress;
834
646
  }
835
647
  interface ListCustomersParams extends ListParams {
836
- externalId?: string;
837
648
  isActive?: boolean;
838
649
  search?: string;
839
650
  }
@@ -852,7 +663,7 @@ declare class CustomersResource {
852
663
  private httpClient;
853
664
  constructor(httpClient: CommetHTTPClient);
854
665
  /**
855
- * Create a customer (idempotent with externalId)
666
+ * Create a customer (idempotent when id is provided)
856
667
  */
857
668
  create(params: CreateParams, options?: RequestOptions): Promise<ApiResponse<Customer>>;
858
669
  /**
@@ -1012,7 +823,7 @@ declare class Commet {
1012
823
  * await customer.usage.track("api_call");
1013
824
  * ```
1014
825
  */
1015
- customer(externalId: string): CustomerContext;
826
+ customer(customerId: string): CustomerContext;
1016
827
  getEnvironment(): Environment;
1017
828
  isSandbox(): boolean;
1018
829
  isProduction(): boolean;
@@ -1031,4 +842,4 @@ declare function isProduction(environment: Environment): boolean;
1031
842
  * Commet SDK - Billing and usage tracking for SaaS
1032
843
  */
1033
844
 
1034
- export { type ActiveSubscription, type AddParams as AddSeatsParams, type ApiResponse, type BillingInterval, type CanUseFeatureParams, type CanUseResult, type CancelParams, type CheckFeatureParams, type CheckResult, Commet, CommetAPIError, type CommetConfig, CommetError, type CommetGeneratedTypes, CommetValidationError, type CreateParams as CreateCustomerParams, type CreateSubscriptionParams, type CreatedSubscription, type CreditPack, type Currency, type Customer, type CustomerAddress, CustomerContext, type CustomerID, type BatchResult as CustomersBatchResult, type Environment, type EventID, type FeatureAccess, type FeatureSummary, type FeatureType, type GeneratedFeatureCode, type GeneratedPlanCode, type GeneratedSeatType, type GetAllBalancesParams, type GetBalanceParams, type GetFeatureParams, type GetUrlParams, type ListCustomersParams, type ListPlansParams, type PaginatedList, type PaginatedResponse, type Plan, type PlanDetail, type PlanFeature, type PlanID, type PlanPrice, type PortalAccess, type RemoveParams as RemoveSeatsParams, type RequestOptions, type SeatBalance, type SeatEvent, type SetAllParams as SetAllSeatsParams, type SetParams as SetSeatsParams, type Subscription, type SubscriptionStatus, type TrackModelTokensParams, type TrackParams, type TrackUsageParams, type UpdateParams as UpdateCustomerParams, type BatchResult$1 as UsageBatchResult, type UsageEvent, type UsageEventProperty, type WebhookData, type WebhookEvent, type WebhookPayload, Webhooks, Commet as default, isProduction, isSandbox };
845
+ export { type ActiveSubscription, type AddParams as AddSeatsParams, type ApiResponse, type BillingInterval, type CanUseFeatureParams, type CanUseResult, type CancelParams, type CheckFeatureParams, type CheckResult, Commet, CommetAPIError, type CommetConfig, CommetError, type CommetGeneratedTypes, CommetValidationError, type CreateParams as CreateCustomerParams, type CreateSubscriptionParams, type CreatedSubscription, type CreditPack, type Currency, type Customer, type CustomerAddress, CustomerContext, type CustomerID, type BatchResult as CustomersBatchResult, type Environment, type EventID, type FeatureAccess, type FeatureSummary, type FeatureType, type GeneratedFeatureCode, type GeneratedPlanCode, type GeneratedSeatType, type GetAllBalancesParams, type GetBalanceParams, type GetFeatureParams, type GetUrlParams, type ListCustomersParams, type ListPlansParams, type PaginatedList, type PaginatedResponse, type Plan, type PlanDetail, type PlanFeature, type PlanID, type PlanPrice, type PortalAccess, type RemoveParams as RemoveSeatsParams, type RequestOptions, type SeatBalance, type SeatEvent, type SetAllParams as SetAllSeatsParams, type SetParams as SetSeatsParams, type Subscription, type SubscriptionStatus, type TrackModelTokensParams, type TrackParams, type TrackUsageParams, type UpdateParams as UpdateCustomerParams, type UsageEvent, type UsageEventProperty, type WebhookData, type WebhookEvent, type WebhookPayload, Webhooks, Commet as default, isProduction, isSandbox };