Skip to content

Installation

The Latch JavaScript SDK handles access checks, paywall display, identity management, and event tracking in browser environments.

The simplest way to use the SDK — add two lines to your HTML:

<meta name="latch:key" content="pk_your_publishable_key" />
<script src="https://latch-api.v3ck.com/sdk/latch.js"></script>

The SDK auto-initializes, checks access, and shows paywalls automatically. See the HTML integration guide for details.

The SDK can also be imported as an ES module in bundled applications. It is not currently published to npm — import it from your Latch monorepo workspace.

import { init } from '@latch/sdk';

Call init() once when your page loads.

import { init } from '@latch/sdk';
init({
apiKey: 'pk_your_publishable_key',
apiUrl: 'https://latch-api.v3ck.com',
});

The SDK will auto-init if your bundle sets window.__LATCH_CONFIG__ before loading the SDK code:

<script>
window.__LATCH_CONFIG__ = {
apiKey: 'pk_your_publishable_key',
apiUrl: 'https://latch-api.v3ck.com',
};
</script>
OptionTypeRequiredDescription
apiKeystringYesYour publishable key (pk_...).
apiUrlstringNoBase URL of your Latch API. Defaults to "".
userIdstringNoKnown logged-in reader ID.
anonymousIdstringNoOverride the stored anonymous ID.
debugbooleanNoEnable console logging.
onPaywallfunctionNoCustom callback for denied access.
onCheckoutfunctionNoCalled when the built-in Subscribe button is clicked.
paywallSelectorstringNoCSS selector used by the inline template.

When you call init():

  1. the SDK creates or loads an anonymous ID
  2. it queues an initial pageview event
  3. it starts a 5-second flush timer for queued events
  4. it registers a beforeunload flush