Frameworks Angular

Angular

Turn an Angular CLI app into an extension popup and gate features with injectUser signals from @extensionbill/sdk/angular.

injectUser(bill) from @extensionbill/sdk/angular gives your component signals for the current user, so templates update by themselves after a purchase. You need Angular CLI 18+ and Node.js 20+. Shared setup: Pick your stack.

Quick start

  1. Create the project.

    npx @angular/cli@latest new my-extension --routing=false --style=css --ssr=false
    cd my-extension
    npm install @extensionbill/sdk
    npm install -D esbuild
    
  2. Add the manifest. In angular.json, under projects.my-extension.architect.build, set:

    {
      "options": {
        "outputPath": { "base": "dist/extension", "browser": "" }
      },
      "configurations": {
        "production": {
          "optimization": {
            "scripts": true,
            "styles": { "minify": true, "inlineCritical": false },
            "fonts": true
          }
        }
      }
    }
    

    inlineCritical: false matters: critical CSS inlining adds an inline onload handler that the extension CSP blocks. Keep the default AOT build (no eval). Then add public/manifest.json, which is copied to the output as is:

    {
      "manifest_version": 3,
      "name": "My Extension",
      "version": "1.0.0",
      "permissions": ["storage", "alarms"],
      "background": { "service_worker": "background.js", "type": "module" },
      "action": { "default_popup": "index.html" }
    }
    
  3. Create the client and the background.

    // src/bill.ts
    import { ExtensionBill } from '@extensionbill/sdk'
    
    export const bill = ExtensionBill({
      publishableKey: 'pk_test_…', // your sandbox key from Developers → Integrate
    })
    
    // src/background.ts
    import { bill } from './bill'
    
    bill.startBackground()
    

    Keep startBackground() at the top level (why). The Angular CLI does not build the service worker, so bundle it with esbuild after the popup (ng build empties the folder first). In package.json:

    "scripts": {
      "build": "ng build && npm run build:background",
      "build:background": "esbuild src/background.ts --bundle --format=esm --outfile=dist/extension/background.js"
    }
    
  4. Gate your popup. Call injectUser(bill) in a field initializer (it needs an injection context):

    // src/app/app.component.ts (src/app/app.ts in Angular 20+, keep the generated class name)
    import { Component } from '@angular/core'
    import { injectUser } from '@extensionbill/sdk/angular'
    import { bill } from '../bill'
    
    @Component({
      selector: 'app-root',
      standalone: true,
      template: `
        @if (account.loading()) {
          <p>Loading…</p>
        } @else if (account.hasAccess('pro')) {
          <p>Pro feature unlocked.</p>
          <button type="button" (click)="bill.openPortal()">Manage subscription</button>
        } @else {
          <button type="button" (click)="bill.openCheckout()">Upgrade to Pro</button>
          <button type="button" (click)="bill.openRestore()">Already paid? Restore</button>
        }
      `,
    })
    export class AppComponent {
      protected readonly account = injectUser(bill)
      protected readonly bill = bill
    }
    
  5. Test a purchase. Run npm run build, load the dist/extension 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 on init and call the returned cleanup function on destroy:

// src/app/paywall.component.ts
import { Component, ElementRef, OnDestroy, OnInit, inject } from '@angular/core'
import { mountPaywall } from '@extensionbill/sdk/paywall'
import { bill } from '../bill'

@Component({
  selector: 'app-paywall',
  standalone: true,
  template: '',
  host: { style: 'display: block' },
})
export class PaywallComponent implements OnInit, OnDestroy {
  private readonly host: ElementRef<HTMLElement> = inject(ElementRef)
  private unmount?: () => void

  ngOnInit(): void {
    this.unmount = mountPaywall(this.host.nativeElement, bill, { title: 'Upgrade to Pro', theme: 'auto' })
  }

  ngOnDestroy(): void {
    this.unmount?.()
  }
}

Add imports: [PaywallComponent] to AppComponent and use @else { <app-paywall /> }. Every option: Paywall UI.

What injectUser returns

protected readonly account = injectUser(bill)

account.user(), account.loading() and account.error() are read-only signals (meaning of each field). account.hasAccess() reads user(), so templates and computed() track it. account.refresh() reloads from the server.

Call injectUser(bill) in a field initializer or the constructor. It unsubscribes when the component is destroyed.

Keys per build

Replace the inline key with a build-time constant:

// src/bill.ts
import { ExtensionBill } from '@extensionbill/sdk'

declare const EXTENSIONBILL_KEY: string

export const bill = ExtensionBill({ publishableKey: EXTENSIONBILL_KEY })

Set it with the define option in angular.json (build.options, or per configuration) and pass the same value to esbuild:

"define": { "EXTENSIONBILL_KEY": "'pk_live_…'" }
"build:background": "esbuild src/background.ts --bundle --format=esm --define:EXTENSIONBILL_KEY=\"'pk_live_…'\" --outfile=dist/extension/background.js"

Live keys work after going live.

Other build tools

The Angular CLI plus esbuild is the recommended setup. Any bundler that emits the popup and background.js into one folder with the manifest also works.

Troubleshooting

  • Popup unstyled: inlineCritical is still on (step 2). Right-click the popup, click Inspect, and check the console for CSP errors.
  • NG0203 inject() error: call injectUser(bill) in a field initializer or the constructor, not in ngOnInit or a handler.
  • Stuck on “Loading…” or background_unavailable: dist/extension/background.js is missing. Run the full npm run build and reload the extension.
  • chrome is not defined in ng serve: test the built extension in Chrome, not the dev server.

Everything else: shared troubleshooting.

Next steps