> How the hosted Plandalf script loads, starts itself, queues your calls until it's ready, and takes config from the page.

Source: https://plandalf.com/docs/developers/sdk

Developers

# The Plandalf SDK

The Plandalf SDK is one script tag. It opens checkouts from buttons on your page, and gives your own code `window.plandalf` for everything else: opening checkouts, identifying buyers, gates, deadline timers and referrals.

## Load the script

Add this before `</body>` on every page that opens a checkout. `YOUR-ORG` is your organization's subdomain; a checkout's **Share** screen gives you the tag with it filled in.

```html
<script async src="https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js"></script>
```

The script talks to the host it loaded from, so load it from your own organization's host. `async` keeps it from holding up your page. Once the page is idle, the SDK downloads the checkout code in the background so the first checkout opens quickly. Add `data-prefetch="false"` to the tag on pages that rarely open a checkout. When a buyer points at, taps or tabs to a buy button, the SDK also fetches that checkout's details ahead of the click, with or without prefetch.

No npm package is needed for the hosted script. If you build with React, see [React](https://plandalf.com/docs/developers/react).

## Buttons work with no code

When the script starts, it finds every element with a `data-plandalf-*` attribute and binds it. It also watches the page, so elements your framework adds later work too.

```html
<button data-plandalf-present="YOUR-CHECKOUT">Buy now</button>
```

See [Script and HTML attributes](https://plandalf.com/docs/developers/attributes) for every attribute.

## Call the SDK before it loads

Because the script is async, `window.plandalf` doesn't exist until it loads. Put this stub above the script tag and you can call `plandalf('command', …)` straight away. The SDK runs the queued calls in order once it starts.

```html
<script>
  window.plandalf = window.plandalf || function () { (plandalf.q = plandalf.q || []).push(arguments); };
</script>
<script async src="https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js"></script>
```

After the script loads, `plandalf` is still callable as a function, and it also has methods: `plandalf.present()`, `plandalf.identify()` and the rest in the [SDK reference](https://plandalf.com/docs/developers/sdk-reference). A few commands only exist in the function form, such as `plandalf('configure', …)`, `plandalf('on', …)` and `plandalf('setRef', …)`.

## Wait until it's ready

Use `ready` to run code once the SDK has started. If it already has, your callback runs straight away. The callback gets the SDK instance.

```js
plandalf('ready', async () => {
  const result = await plandalf.present('YOUR-CHECKOUT');
  if (result.status === 'complete') location.href = '/thanks';
});
```

The SDK also sends two window events: `plandalf:loaded` when the script has run, and `plandalf:ready` when it has started. To catch `ready` with `plandalf('on', 'ready', …)`, queue the listener before the script loads.

## Starting the SDK

By default the SDK starts itself as soon as it loads, using the host it loaded from and any `data-*` attributes on its script tag:

```html
<script async
  src="https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js"
  data-debug="true"
  data-config-ref="email"></script>
```

To start it yourself, queue `init` before the script loads. Only the first `init` does anything, and a queued `init` replaces the automatic start, so the script tag's `data-api-base`, `data-debug` and `data-config-ref` are ignored. Pass `apiBase` yourself:

```js
plandalf('init', {
  apiBase: 'https://YOUR-ORG.plandalf.dev',
  debug: true,
  continuity: 'tab',
});
```

## Change config later

`plandalf('configure', {…})` merges new values into the config. It works before or after the SDK starts. Use it for values that apply to every checkout on the page, such as form prefill and Stripe customer metadata:

```js
plandalf('configure', {
  properties: { email: 'ada@example.com' },
  metadata: { tolt_referral: () => window.tolt_referral },
});
```

`properties` pre-fills checkout fields by their field ID; the buyer can see and change them. `metadata` goes to the Stripe customer when Plandalf creates it, and the buyer never sees it. A function value is read when the checkout session is created, so a tracker that sets a global after page load still gets picked up. Values passed to a single `present()` call win over these. See [Open a checkout](https://plandalf.com/docs/developers/open-a-checkout#options).

Every config field is in [`PlandalfConfig`](https://plandalf.com/docs/developers/sdk-reference#config).

## Commands

Command

What it does

`plandalf('addGate')`

Register a gate. A gate wraps a feature behind an access check. If the check fails (or is absent), the SDK presents the linked flow.

`plandalf('configure')`

Change SDK config after start (or before it, from the command queue): `apiBase`, `ref`, `properties`, `metadata`, `referral` and the other PlandalfConfig fields.

`plandalf('consent')`

Give or withdraw consent for referral capture. Only matters when referral capture waits for consent (`referral.consent: 'required'` or `data-config-referral-consent="required"`).

`plandalf('gate')`

Check a gate. If the check passes, resolves immediately with access: true. If it fails, presents the linked flow. After completion, sync returns fresh entitlements and the same check runs again with those values. Check/sync failures deny access without opening another checkout.

`plandalf('getIdentity')`

Get the full visitor identity record. Contains anonymous\_id, ref, first\_seen\_at, last\_seen\_at.

`plandalf('getRef')`

Get the visitor ref (external identity from ?pf\_ref=), or null.

`plandalf('getSessionAge')`

Get seconds elapsed since this visitor first arrived (first\_seen\_at).

`plandalf('identify')`

Identify the customer with a server-signed customer JWT. Same as `plandalf.identify(token)`, but the command form returns nothing; call `plandalf.identify()` to await the validation result.

`plandalf('init')`

Start the SDK yourself instead of letting it start on load. Only the first call does anything. Queue it before the script loads to pass your own config; without a queued `init` the SDK starts itself from the script tag's `data-*` attributes.

`plandalf('mount')`

Render an offer inline inside an element.

`plandalf('off')`

Stop listening for an event registered with `plandalf('on', …)`.

`plandalf('on')`

Listen for an SDK event. Queue it before the script loads to catch `ready`. Every event is also dispatched on `window` as `plandalf:<event>`.

`plandalf('open')`

Alias of `plandalf('present')`.

`plandalf('present')`

Open an offer in a frame. Also emits `open`, then `complete`, `close` or `error` through `plandalf('on', …)`.

`plandalf('promo')`

Open a live promo handle. Single entry point for everything promo-related on a host page.

`plandalf('ready')`

Run a callback once the SDK has started, or straight away if it already has.

`plandalf('referral')`

The page's referral handle (one per page, cached).

`plandalf('reset')`

Clear the current user identity. The anonymous device ID is preserved — only the user token is removed. Next gate/present call will be anonymous.

`plandalf('setRef')`

Set the visitor ref and store it in localStorage. Works before the SDK has started, so a landing page can queue it ahead of the script.

## Browser storage keys

List these in your cookie or privacy policy if it names what your site stores.

Key

Where

What it holds

`plandalf_anon`

localStorage

The browser's anonymous id, made on the first visit. `reset()` keeps it.

`plandalf_user`

localStorage

The signed buyer token from `identify()`, until `reset()`.

`plandalf_identity`

localStorage

The visitor ref and when the visitor was first and last seen.

`plandalf_promo_<slug>`

localStorage

When this browser first saw a deadline timer, for a visitor without a ref.

`plandalf_session:<slug>`

sessionStorage or localStorage

The unfinished checkout's token, with [session continuity](https://plandalf.com/docs/developers/session-continuity) on.

`plandalf_referral:<host>` and `plandalf_referral_…`

localStorage

A partner referral, the program's referral settings and whether the visitor dismissed the badge.

`pf_rt`

Cookie

The partner referral token, until the program's attribution window ends.

## Check it loaded

Open your page (not a page builder's editor), open the browser console and type `plandalf`. You should see a function. `undefined` means the script tag is missing or blocked; look for `plandalf-sdk.js` in the Network tab. With `data-debug="true"` on the tag, the SDK logs what it does with a `[plandalf]` prefix. More fixes are in [Troubleshooting](https://plandalf.com/docs/help/troubleshooting).

Something wrong or missing? Email [hello@plandalf.com](mailto:hello@plandalf.com).
