Frameworks Svelte

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

  1. 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
    
  2. 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' },
          },
        }),
      ],
    })
    
  3. 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).

  4. Gate your popup. Replace src/App.svelte (the template’s src/main.ts already mounts it). On Svelte 4, write on:click instead of onclick.

    <!-- 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}
    
  5. Test a purchase. Run npm run dev, load the dist folder 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?) and user.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, write bill.hasAccess($user.user, 'pro') so Svelte re-renders when $user changes.
  • 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-extension with the Svelte template, then the WXT setup with modules: ['@wxt-dev/module-svelte']. App.svelte goes in entrypoints/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