Svelte
Add ExtensionBill payments to a Svelte browser extension with the userStore store.
userStore(bill) from @extensionbill/sdk/svelte is a standard Svelte store with the current user, so $user works in Svelte 3, 4 and 5 without SvelteKit. Shared setup: Pick your stack.
Quick start
-
Create the project.
npm create vite@latest my-extension -- --template svelte-ts cd my-extension npm install @extensionbill/sdk npm install -D @crxjs/vite-plugin -
Add the manifest. Replace
vite.config.ts; CRXJS builds every file the manifest references:// vite.config.ts import { defineConfig } from 'vite' import { svelte } from '@sveltejs/vite-plugin-svelte' import { crx } from '@crxjs/vite-plugin' export default defineConfig({ plugins: [ svelte(), crx({ manifest: { manifest_version: 3, name: 'My Extension', version: '1.0.0', permissions: ['storage', 'alarms'], background: { service_worker: 'src/background.ts', type: 'module' }, action: { default_popup: 'index.html' }, }, }), ], }) -
Create the client and the background.
# .env.development VITE_EXTENSIONBILL_PUBLISHABLE_KEY=pk_test_…// src/bill.ts import { ExtensionBill } from '@extensionbill/sdk' export const bill = ExtensionBill({ publishableKey: import.meta.env.VITE_EXTENSIONBILL_PUBLISHABLE_KEY, })// src/background.ts import { bill } from './bill' bill.startBackground()Keep
startBackground()at the top level (why). -
Gate your popup. Replace
src/App.svelte(the template’ssrc/main.tsalready mounts it). On Svelte 4, writeon:clickinstead ofonclick.<!-- src/App.svelte --> <script lang="ts"> import { userStore } from '@extensionbill/sdk/svelte' import { bill } from './bill' const user = userStore(bill) </script> {#if $user.loading} <p>Loading…</p> {:else if bill.hasAccess($user.user, 'pro')} <main> <p>Pro feature unlocked.</p> <button onclick={() => bill.openPortal()}>Manage subscription</button> </main> {:else} <main> <button onclick={() => bill.openCheckout()}>Upgrade to Pro</button> <button onclick={() => bill.openRestore()}>Already paid? Restore</button> </main> {/if} -
Test a purchase. Run
npm run dev, load thedistfolder unpacked and buy Pro with a sandbox buyer (steps).
Advanced
Drop-in paywall
Swap your upgrade buttons for the ready-made Paywall UI. mountPaywall returns its cleanup function, so return it from onMount:
<!-- src/Paywall.svelte -->
<script lang="ts">
import { onMount } from 'svelte'
import { mountPaywall } from '@extensionbill/sdk/paywall'
import { bill } from './bill'
let host: HTMLDivElement
onMount(() => mountPaywall(host, bill, { title: 'Upgrade to Pro', theme: 'auto' }))
</script>
<div bind:this={host}></div>
Show it to free users in place of the upgrade buttons: {:else}<Paywall />{/if}. Every option: Paywall UI.
What userStore returns
const user = userStore(bill)
$user.user,$user.loading,$user.error: the current snapshot (meaning of each field).user.hasAccess(feature?)anduser.refresh(): methods on the store.user.hasAccess()reads the latest value but is not reactive, so use it in event handlers and plain code. In markup, writebill.hasAccess($user.user, 'pro')so Svelte re-renders when$userchanges.- Derived values:
const isPro = $derived(bill.hasAccess($user.user, 'pro'))in Svelte 5,$: isPro = bill.hasAccess($user.user, 'pro')in Svelte 4.
Calling userStore(bill) in many components shares one load. You can also export the store once from src/bill.ts.
Keys per build
npm run build reads .env.production: put VITE_EXTENSIONBILL_PUBLISHABLE_KEY=pk_live_… there. Live keys work after going live.
Other build tools
- WXT:
npx wxt@latest init my-extensionwith the Svelte template, then the WXT setup withmodules: ['@wxt-dev/module-svelte'].App.sveltegoes inentrypoints/popup/. - Plasmo: follow the Plasmo setup and put the component in
popup.svelte.
Troubleshooting
Background, key and extension ID problems are covered in the shared troubleshooting.
Next steps
- Checking access in content scripts, the options page and the service worker
- Paywall UI labels and theming
- Free trials
- Going live checklist