> Add Plandalf to SvelteKit with a hosted script or use Svelte stores and actions to open checkouts and display deadline timers.

Source: https://plandalf.com/docs/frameworks/sveltekit

Frameworks

# SvelteKit

Use the hosted script in `src/app.html` to open checkouts from template attributes. To work with stores and actions in your components, use the [Svelte package](#the-plandalf-svelte-package).

## Add the script

Paste the script before `</body>` in `src/app.html`. Copy your script address from your checkout's **Share** screen (pick **HTML**).

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    %sveltekit.head%
  </head>
  <body data-sveltekit-preload-data="hover">
    <div style="display: contents">%sveltekit.body%</div>
    <script async src="https://YOUR-ORG.plandalf.dev/js/plandalf-sdk.js"></script>
  </body>
</html>
```

## Open a checkout from a button

Add the attribute to any element:

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

### From your own click handler

Click handlers run in the browser, so `window` is safe there. Queue the call with the stub, so it works before the script has loaded:

```html
<script lang="ts">
  function upgrade() {
    const w = window as any;
    w.plandalf = w.plandalf || function () { (w.plandalf.q = w.plandalf.q || []).push(arguments); };
    w.plandalf("present", "YOUR-CHECKOUT");
  }
</script>

<button onclick={upgrade}>Upgrade</button>
```

On Svelte 4, write `on:click={upgrade}`. See [Open a checkout from code](https://plandalf.com/docs/developers/open-a-checkout).

## Show a checkout on the page

Render an empty element with `data-plandalf-mount`:

```html
<div data-plandalf-mount="YOUR-CHECKOUT" style="min-height: 480px"></div>
```

## Show a deadline timer

Render an empty element with your deadline timer's slug. The script draws the live countdown inside it. To charge the deadline timer's current price, add `data-plandalf-apply-promo` with the same slug to your buy button.

```html
<div data-plandalf-promo="YOUR-PROMO"></div>
<button data-plandalf-present="YOUR-CHECKOUT" data-plandalf-apply-promo="YOUR-PROMO">Buy Now</button>
```

See [Countdowns](https://plandalf.com/docs/promos/countdowns).

## The @plandalf/svelte package

In Svelte 4 or 5, install the package to use Plandalf from your components.

```bash
npm install @plandalf/svelte
```

Add these lines to the instance script in your root layout. Keep the layout markup that renders your child routes.

```html
<script>
  import { setPlandalf } from '@plandalf/svelte';

  setPlandalf({ apiBase: 'https://YOUR-ORG.plandalf.dev' });
</script>
```

Import the helpers from `@plandalf/svelte` and call them while a child component initialises.

-   Get `present` from `createPresent()`. Call `present('YOUR-CHECKOUT')` from a click handler.
-   With `const promo = createPromo(slug)`, read the current tier from `$promo.tier` and the seconds left from `$promo.remaining`.
-   With `const gate = createGate(name, flow, check)`, read `$gate.access` to check access. Call `gate.trigger()` from your upgrade button.
-   For a countdown, set `const countdown = createCountdown()` and add `<div use:countdown={{ slug }}></div>` to your markup. Set `slug` to your deadline timer's slug.
-   Use `createUser()` to set the buyer's token and `createReferral()` to read the visitor's referral. Use `getPlandalf()` for the SDK itself.

## Check it's working

1.  ### Run the app
    
    If you used the hosted script, type `typeof window.plandalf` in the browser console. Look for `"function"`.
    
2.  ### Click your button
    
    Plandalf should display your checkout over the page. Navigate to another page and back, and check the button still works.
    
3.  ### Make a test purchase
    
    Add `localhost` and your staging site in Plandalf under **Settings → Advanced → Test domains**. Checkouts there use Stripe test cards. See [Test mode](https://plandalf.com/docs/get-started/test-mode).
    

## If it isn't working

-   A `window is not defined` error means code touched `window` during server rendering. Move it into an event handler or `onMount`.
-   If your app sends a Content Security Policy, allow `https://*.plandalf.dev` and `https://js.stripe.com` in `script-src`, `connect-src` and `frame-src`. SvelteKit's `kit.csp` setting in `svelte.config.js` takes these directives.
-   Add `data-debug="true"` to the script tag and the console logs each step the script takes.

See [Troubleshooting](https://plandalf.com/docs/help/troubleshooting).

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