Skip to main content

RevstackClient

Core client class for managing entitlements, checkout, and billing portal sessions.

Constructor

new RevstackClient(config: RevstackConfig)
Parameters
config
RevstackConfig
required

Properties

isInitialized

client.isInitialized: boolean
Returns true if init() completed successfully.

isReady

client.isReady: boolean
Returns true once init() has settled (success or failure). Use this to show loading spinners.

Methods

init()

Resolves identity, fetches entitlements, and caches them locally.
await client.init(): Promise<void>
Usage
const client = new RevstackClient(config);
await client.init();
Call this once on app startup before accessing entitlements.

getEntitlement()

Returns the cached entitlement for a feature key.
client.getEntitlement(key: string): Entitlement
Parameters
  • key (string) - The feature key
Returns
  • Entitlement with { key, hasAccess, value? }
  • Returns { key, hasAccess: false } if key not found
Usage
const entitlement = client.getEntitlement('premium-feature');

if (entitlement.hasAccess) {
  showPremiumUI();
}

hasAccess()

Convenience method that checks if a feature is accessible.
client.hasAccess(key: string): boolean
Parameters
  • key (string) - The feature key
Returns
  • true if user has access, false otherwise
Usage
if (client.hasAccess('advanced-analytics')) {
  renderAnalyticsDashboard();
} else {
  renderUpgradePrompt();
}

startCheckout()

Creates a checkout session and redirects to the Revstack checkout page.
await client.startCheckout(params: CheckoutParams): Promise<void>
Parameters
params
CheckoutParams
required
Usage
const checkoutButton = document.getElementById('subscribe');

checkoutButton.addEventListener('click', async () => {
  await client.startCheckout({
    planId: 'plan_pro_monthly',
    successUrl: 'https://app.example.com/welcome',
    cancelUrl: 'https://app.example.com/pricing',
  });
});

openBillingPortal()

Opens the billing portal where users can manage their subscription.
await client.openBillingPortal(params: BillingPortalParams): Promise<void>
Parameters
params
BillingPortalParams
required
Usage
const manageButton = document.getElementById('manage-subscription');

manageButton.addEventListener('click', async () => {
  await client.openBillingPortal({
    returnUrl: 'https://app.example.com/settings',
  });
});

subscribe()

Subscribes to entitlement changes. Returns an unsubscribe function.
client.subscribe(listener: () => void): () => void
Parameters
  • listener (function) - Callback invoked when entitlements change
Returns
  • Function to unsubscribe
Usage
const unsubscribe = client.subscribe(() => {
  console.log('Entitlements updated!');
  updateUI();
});

// Later: clean up
unsubscribe();

getSnapshot()

Returns the current version number (increments when entitlements change).
client.getSnapshot(): number
Used internally by useSyncExternalStore in React. You typically won’t call this directly.

Complete Example

import { RevstackClient } from '@revstackhq/browser';

const client = new RevstackClient({
  publicKey: 'pk_live_...',
  getToken: async () => localStorage.getItem('auth_token'),
});

// Initialize on page load
await client.init();

// Show loading state while initializing
if (!client.isReady) {
  document.getElementById('app').innerHTML = '<div>Loading...</div>';
} else if (client.hasAccess('premium-feature')) {
  renderPremiumUI();
} else {
  renderFreeUI();
}

// Handle checkout
document.getElementById('upgrade-btn').addEventListener('click', async () => {
  await client.startCheckout({
    planId: 'plan_premium',
    successUrl: window.location.origin + '/success',
    cancelUrl: window.location.origin + '/pricing',
  });
});

// Handle billing portal
document.getElementById('manage-btn').addEventListener('click', async () => {
  await client.openBillingPortal({
    returnUrl: window.location.href,
  });
});

// Listen for entitlement changes
client.subscribe(() => {
  console.log('Entitlements updated');
  if (client.hasAccess('new-feature')) {
    showNewFeatureNotification();
  }
});

Types

RevstackConfig

interface RevstackConfig {
  publicKey: string;
  apiUrl?: string;
  getToken: () => Promise<string | null>;
  getGuestId?: () => Promise<string>;
  disableFingerprint?: boolean;
}

Entitlement

interface Entitlement {
  key: string;
  hasAccess: boolean;
  value?: string | number | boolean;
}

CheckoutParams

interface CheckoutParams {
  planId: string;
  successUrl: string;
  cancelUrl: string;
}

BillingPortalParams

interface BillingPortalParams {
  returnUrl: string;
}

Build docs developers (and LLMs) love