Installation
The Latch JavaScript SDK handles access checks, paywall display, identity management, and event tracking in browser environments.
Current availability
Section titled “Current availability”The SDK is intended for bundled JavaScript applications.
- it is not currently published to npm
- the API does not currently expose a hosted
/sdk/latch.jsasset
If you need a browser integration today, start with Plain HTML or WordPress.
Install
Section titled “Install”import { init } from '@latch/sdk';Initialize
Section titled “Initialize”Call init() once when your page loads.
import { init } from '@latch/sdk';
init({ apiKey: 'pk_your_publishable_key', apiUrl: 'https://your-latch-api',});Auto-init
Section titled “Auto-init”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://your-latch-api', };</script>Configuration options
Section titled “Configuration options”| Option | Type | Required | Description |
|---|---|---|---|
apiKey | string | Yes | Your publishable key (pk_...). |
apiUrl | string | No | Base URL of your Latch API. Defaults to "". |
userId | string | No | Known logged-in reader ID. |
anonymousId | string | No | Override the stored anonymous ID. |
debug | boolean | No | Enable console logging. |
onPaywall | function | No | Custom callback for denied access. |
onCheckout | function | No | Called when the built-in Subscribe button is clicked. |
paywallSelector | string | No | CSS selector used by the inline template. |
What happens on init
Section titled “What happens on init”When you call init():
- the SDK creates or loads an anonymous ID
- it queues an initial pageview event
- it starts a 5-second flush timer for queued events
- it registers a
beforeunloadflush