Frameworks Vue

Vue

Add ExtensionBill payments to a Vue 3 browser extension with the useUser composable.

useUser(bill) from @extensionbill/sdk/vue gives every Vue 3.3+ component reactive user, loading and error refs and a hasAccess() check. Shared setup: Pick your stack.

Quick start

  1. Create the project.

    npm create vite@latest my-extension -- --template vue-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 vue from '@vitejs/plugin-vue'
    import { crx } from '@crxjs/vite-plugin'
    
    export default defineConfig({
      plugins: [
        vue(),
        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.vue (the template’s src/main.ts already mounts it):

    <!-- src/App.vue -->
    <script setup lang="ts">
    import { useUser } from '@extensionbill/sdk/vue'
    import { bill } from './bill'
    
    const { loading, hasAccess } = useUser(bill)
    </script>
    
    <template>
      <p v-if="loading">Loading…</p>
      <main v-else-if="hasAccess('pro')">
        <p>Pro feature unlocked.</p>
        <button @click="bill.openPortal()">Manage subscription</button>
      </main>
      <main v-else>
        <button @click="bill.openCheckout()">Upgrade to Pro</button>
        <button @click="bill.openRestore()">Already paid? Restore</button>
      </main>
    </template>
    
  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. Mount it in onMounted and call the returned cleanup function before the component unmounts:

<!-- src/Paywall.vue -->
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue'
import { mountPaywall } from '@extensionbill/sdk/paywall'
import { bill } from './bill'

const host = ref<HTMLDivElement | null>(null)
let unmount: (() => void) | undefined

onMounted(() => {
  unmount = mountPaywall(host.value!, bill, { title: 'Upgrade to Pro', theme: 'auto' })
})
onBeforeUnmount(() => unmount?.())
</script>

<template>
  <div ref="host" />
</template>

Show it to free users with <ProFeatures v-if="hasAccess('pro')" /><Paywall v-else />. Every option: Paywall UI.

What useUser returns

const { user, loading, error, hasAccess, refresh } = useUser(bill)

user, loading and error are read-only refs (meaning of each field): templates unwrap them, script code reads user.value. Keep the ref instead of destructuring user.value, so updates stay reactive. hasAccess() reads user.value, so it is reactive in templates and computed().

Call useUser(bill) in <script setup> or setup(). It subscribes right away and unsubscribes when the component’s scope is disposed. Every component that calls it shares one load.

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 Vue template, then the WXT setup with modules: ['@wxt-dev/module-vue']. App.vue goes in entrypoints/popup/.
  • Plasmo: follow the Plasmo setup and put the component in popup.vue.

Troubleshooting

Background, key and extension ID problems are covered in the shared troubleshooting.

Next steps