billingsdk

API Reference

Core types, methods, and interfaces in Billing SDK.

createBilling

Creates a billing client with a provider adapter and plan definitions.

createBilling<Plans>(input: {
  adapter: BillingAdapter;
  plans: Plans;
  storage?: StorageAdapter;
}): BillingClient<Plans>

definePlans

Defines plan configurations with typed feature entitlements.

definePlans<Plans>(input: Plans): PlanDefinitions

// Plan shape:
{
  [planId: string]: {
    name: string;
    prices: { id: string; amount: number; currency: string; interval?: string }[];
    features: {
      [featureKey: string]: { limit: number; reset?: "month" | "year" } | boolean;
    };
  };
}

BillingClient

The main client returned by createBilling():

interface BillingClient<Plans> {
  adapter: BillingAdapter;
  plans: Plans;

  customers: {
    create(input): BillingCustomer;
    get(input): BillingCustomer;
    getByExternalId(input): BillingCustomer;
  };

  checkout: {
    create(input): CheckoutSession;
  };

  subscriptions: {
    get(input): BillingSubscription;
    list(input): BillingSubscription[];
    cancel(input): BillingSubscription;
  };

  entitlements: {
    check(input): EntitlementResult;
    list(input): EntitlementListItem[];
  };

  usage: {
    report(input): UsageRecord;
    get(input): UsageRecord;
  };

  webhooks: {
    verify(input): BillingEvent;
  };

  portal: {
    create(input): PortalSession;
  };
}

Error Types

BillingError — Base error class
BillingConfigurationError — Missing or invalid config
BillingProviderError — Provider API error
BillingWebhookError — Webhook verification failed
BillingAuthenticationError — Invalid API key
BillingNotFoundError — Resource not found
BillingCapabilityError — Feature not supported by adapter
BillingValidationError — Input validation failed
BillingIdempotencyError — Idempotency conflict