React
Add ExtensionBill payments to a React browser extension with the useUser hook.
useUser(bill) from @extensionbill/sdk/react gives every React component the current user and a hasAccess() check, and re-renders it after a purchase. Shared setup: Pick your stack.
Quick start
-
Create the project.
npm create vite@latest my-extension -- --template react-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 react from '@vitejs/plugin-react' import { crx } from '@crxjs/vite-plugin' export default defineConfig({ plugins: [ react(), 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/react' import { bill } from './bill' export default 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 'react'
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, and the StrictMode double mount does not start a second one.
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 React template, then the WXT setup withmodules: ['@wxt-dev/module-react'].App.tsxgoes inentrypoints/popup/. - Plasmo:
npm create plasmo@latest my-extension, then the Plasmo setup. PasteAppintopopup.tsx(it is already the default export).
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