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.
Add the script
Paste the script before </body> in src/app.html. Copy your script address from your checkout's Share screen (pick 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:
<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:
<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.
Show a checkout on the page
Render an empty element with data-plandalf-mount:
<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.
<div data-plandalf-promo="YOUR-PROMO"></div>
<button data-plandalf-present="YOUR-CHECKOUT" data-plandalf-apply-promo="YOUR-PROMO">Buy Now</button>
See Countdowns.
The @plandalf/svelte package
In Svelte 4 or 5, install the package to use Plandalf from your components.
npm install @plandalf/svelte
Add these lines to the instance script in your root layout. Keep the layout markup that renders your child routes.
<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
presentfromcreatePresent(). Callpresent('YOUR-CHECKOUT')from a click handler. - With
const promo = createPromo(slug), read the current tier from$promo.tierand the seconds left from$promo.remaining. - With
const gate = createGate(name, flow, check), read$gate.accessto check access. Callgate.trigger()from your upgrade button. - For a countdown, set
const countdown = createCountdown()and add<div use:countdown={{ slug }}></div>to your markup. Setslugto your deadline timer's slug. - Use
createUser()to set the buyer's token andcreateReferral()to read the visitor's referral. UsegetPlandalf()for the SDK itself.
Check it's working
Run the app
If you used the hosted script, type
typeof window.plandalfin the browser console. Look for"function".Click your button
Plandalf should display your checkout over the page. Navigate to another page and back, and check the button still works.
Make a test purchase
Add
localhostand your staging site in Plandalf under Settings → Advanced → Test domains. Checkouts there use Stripe test cards. See Test mode.
If it isn't working
- A
window is not definederror means code touchedwindowduring server rendering. Move it into an event handler oronMount. - If your app sends a Content Security Policy, allow
https://*.plandalf.devandhttps://js.stripe.cominscript-src,connect-srcandframe-src. SvelteKit'skit.cspsetting insvelte.config.jstakes these directives. - Add
data-debug="true"to the script tag and the console logs each step the script takes.
See Troubleshooting.