Preact
Add ExtensionBill payments to a Preact browser extension with the useUser hook.
useUser(bill) from @extensionbill/sdk/preact gives every Preact component the current user and a hasAccess() check, using preact/hooks directly (no preact/compat). Shared setup: Pick your stack.
Quick start
-
Create the project.
npm create vite@latest my-extension -- --template preact-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 preact from '@preact/preset-vite' import { crx } from '@crxjs/vite-plugin' export default defineConfig({ plugins: [ preact(), 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.tsx(the template’ssrc/main.tsxalready renders it):// src/app.tsx import { useUser } from '@extensionbill/sdk/preact' import { bill } from './bill' export function App() { const { loading, hasAccess } = useUser(bill) if (loading) return <p>Loading…</p> return hasAccess('pro') ? ( <main> <p>Pro feature unlocked.</p> <button onClick={() => bill.openPortal()}>Manage subscription</button> </main> ) : ( <main> <button onClick={() => bill.openCheckout()}>Upgrade to Pro</button> <button onClick={() => bill.openRestore()}>Already paid? Restore</button> </main> ) } -
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 the effect:
// src/Paywall.tsx
import { useEffect, useRef } from 'preact/hooks'
import { mountPaywall } from '@extensionbill/sdk/paywall'
import { bill } from './bill'
export function Paywall() {
const host = useRef<HTMLDivElement>(null)
useEffect(() => {
if (!host.current) return
return mountPaywall(host.current, bill, { title: 'Upgrade to Pro', theme: 'auto' })
}, [])
return <div ref={host} />
}
Show it to free users with hasAccess('pro') ? <ProFeatures /> : <Paywall />. Every option: Paywall UI.
What useUser returns
const { user, loading, error, hasAccess, refresh } = useUser(bill)
Plain values, re-rendered on every change (meaning of each field). Call it in any number of components: they share one load. Already on preact/compat? The React hook works too and returns the same shape; pick one and use it everywhere.
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 has no Preact template. Start from the Vanilla one and add the Preact preset:
npx wxt@latest init my-extension # choose the Vanilla template
cd my-extension
npm install preact @extensionbill/sdk
npm install -D @preact/preset-vite
// wxt.config.ts
import { defineConfig } from 'wxt'
import preact from '@preact/preset-vite'
export default defineConfig({
vite: () => ({ plugins: [preact()] }),
manifest: { permissions: ['storage', 'alarms'] },
})
Add "jsx": "react-jsx" and "jsxImportSource": "preact" to compilerOptions in tsconfig.json. Then follow the WXT setup for utils/bill.ts and the background. For the popup, create entrypoints/popup/index.html with <div id="app"></div> and <script type="module" src="./main.tsx"></script>, a main.tsx that calls render(<App />, document.getElementById('app')!) (render comes from preact), and app.tsx from step 4.
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