Integrate an SDK · Web

Web & React SDK

A framework-independent browser SDK, thin React helpers, and a Playwright runner the CLI launches per replay attempt. Capsules carry sdk.name: @bugcapsule/web and runtime.name: web.

Package (0.4.0, ESM only)Entry pointsWhat it is
@bugcapsule/web., ./coreThe SDK, no dependencies. /core is the environment-neutral model, codec, redaction and request-key core (also used in Node).
@bugcapsule/react., ./react-routerHooks and components. Peers: @bugcapsule/web 0.4.0 (exact), react ≥18, react-router ≥6.4 (optional)
@bugcapsule/web-replaybin bugcapsule-web-runnerNode ≥20 + playwright-core 1.63 runner. Dev dependency, only where you run bugcapsule run|verify

1. Install

Stable packages are available on npm. Use is governed by the included Bug Capsule Proprietary License; use and distribution require prior written permission.

npm install @bugcapsule/web@0.4.0 @bugcapsule/react@0.4.0
npm install -D @bugcapsule/web-replay@0.4.0
# Optional: source-map upload CLI
npm install -D @bugcapsule/cli@0.4.1

2. Connect with your SDK token

i

SDK tokens and the status API are included in these releases. Use the SDK token from your dashboard for a new integration. Existing explicit API URL and ingest key configuration remains supported.

Generate an SDK token in the dashboard (Project Settings › Keys › SDK tokens; it is shown once), then make init the first thing your app does:

import { BugCapsule } from '@bugcapsule/web';

await BugCapsule.init({ token: 'bcsdk.v1.…' });
renderApp();

That is a complete connection. The token carries your BugCapsule address and an ingest-only credential: it can send Capsules and events and read capture settings, nothing else, so it is safe in a web bundle. Hosted and self-hosted BugCapsule work the same way; the token knows where to go. appVersion defaults to the version part of release (else unknown) and appBuild to 0; set them so Capsules carry your real version.

Check the connection

const status = BugCapsule.status();          // null before init
status.connection.state;                     // 'connected', 'connecting', 'offline', 'rejected', …
status.connection.reason;                    // e.g. 'token_revoked'
status.connection.message;                   // what to do about it
status.connection.configFetchedAt;           // 1st confirmation: configuration fetched
status.connection.firstEventAcceptedAt;      // 2nd confirmation: first event received …
status.connection.firstCapsuleAcceptedAt;    // … or first Capsule received
status.instrumentation.missing;              // e.g. ['network', 'navigation', 'app_state']
connection.stateMeaning
not_configuredNo token and no backend: captures are saved locally only
invalid_token / unsupported_token_versionThe token can't be read (damaged, a key pasted instead, or made for a newer SDK). One [bugcapsule] line in the console says why; the app runs without BugCapsule
connectingNo configuration fetched yet in this page session
connectedConfiguration fetched: address, token and network work
offlineBugCapsule can't be reached; data stays in the outbox and is retried
rejectedThe token was refused: reason is token_revoked, token_expired or token_unknown, message says what to do. Capture and uploads stop for the page session
disabledenabled: false, or capture switched off for the project

The two confirmations are separate on purpose, and the dashboard shows them the same way during onboarding and in the SDK token list: Configuration fetched proves address, token and network; First event or Capsule received proves captured data reaches your project. Neither means replay is fully instrumented: that is instrumentation, and step 4.

3. Get your origin allowed

Browsers only let the SDK reach BugCapsule from origins your project allows. Send your BugCapsule team the exact origins (scheme, host and port) your app runs on, for example:

https://app.example.com
https://staging.app.example.com
http://127.0.0.1:4317        # local QA builds

Until an origin is allowed, the browser console shows CORS errors and nothing is uploaded. The SDK never sends cookies to BugCapsule.

4. Add the progressive adapters

The token connects. Faithful replay also needs the app's network, navigation and state, which you add as you go. Each one you skip is listed in status().instrumentation.missing.

import { BugCapsule, networkIntegration, historyIntegration, localStorageIntegration } from '@bugcapsule/web';

await BugCapsule.init({
  token: 'bcsdk.v1.…',
  appVersion: '1.4.0',
  appBuild: '52',
  mode: 'qa',                                   // DEFAULT IS 'production' (10% sampled) — set it
  gitSha: import.meta.env.VITE_BUGCAPSULE_GIT_SHA,
  integrations: [
    networkIntegration({ rules: [{ method: 'POST', path: '/api/checkout', ignoreBodyFields: ['timestamp', 'nonce'] }] }),
    historyIntegration(),
    localStorageIntegration({ allowPrefixes: ['shop.'] }),
  ],
  allowReplay: import.meta.env.DEV || import.meta.env.VITE_BUGCAPSULE_ALLOW_REPLAY === 'true',
});

renderApp();
Step (missing entry)Gives replayAdd
networkRecorded fetch/XHR responses served in the pagenetworkIntegration() (or fetchIntegration() / xhrIntegration())
navigationRoute events; the runner opens the checkpoint's locationhistoryIntegration(), or <BugCapsuleRouteListener /> with React Router
app_stateStorage and app state restored before renderlocalStorageIntegration / sessionStorageIntegration, indexedDbIntegration, registerState or useBugCapsuleState

Then name actions, track inputs and add assertions (next section), and pin the commit with gitSha: set VITE_BUGCAPSULE_GIT_SHA=$(git rev-parse HEAD) (or your bundler's equivalent) in every build, production included. Without it capsules are partial.

Self-hosted or existing setups (advanced)

A self-hosted BugCapsule issues tokens that carry its own address, so the token setup above works there unchanged. Apps set up before tokens, with an explicit address and ingest key, keep working:

await BugCapsule.init({
  …,
  backend: { url: 'https://api.bugcapsule.com', ingestKey: 'bci_…' },   // never a bcs_ secret key
});

When both are set, backend wins over token. status().connection.source says which one is in use ('token' or 'backend').

5. Make more bugs replayable

HookWhy
onClick={BugCapsule.action('cart.checkout', checkout)} + data-bugcapsule-action="cart.checkout"Semantic replay by id. The attribute is also the stable selector for click replay. Call action() after init.
BugCapsule.trackInput('login.email', inputEl)Typed text is replayed. Password inputs, data-bugcapsule-sensitive, { sensitive: true } and sensitive-looking ids record [REDACTED].
BugCapsule.check('cart_total_non_negative', total >= 0) / fail(id)Wrong behaviour that doesn't throw becomes a checkable failure
BugCapsule.reportError(e) / <BugCapsuleErrorBoundary>Errors your code or React catches never reach window.onerror
BugCapsule.registerState('cart', { serialize, restore })App-owned state (Redux, Zustand, a store) in checkpoints, restored in replay
indexedDbIntegration([{ name, snapshot, restore }])IndexedDB stores through adapters you write; the SDK never reads IndexedDB itself
BugCapsule.now() / date() instead of Date.now()Replay returns the recorded wall clock
BugCapsule.random() instead of Math.random()Seeded per session (mulberry32), same seed in replay

React

import { useBugCapsuleAction, useTrackedState, useBugCapsuleState,
         BugCapsuleErrorBoundary, BugCapsuleCaptureButton } from '@bugcapsule/react';
import { BugCapsuleRouteListener } from '@bugcapsule/react/react-router';

function Cart() {
  const [coupon, setCoupon] = useTrackedState('cart.coupon', '');
  const [cart, setCart] = useState(emptyCart);
  useBugCapsuleState('cart', cart, setCart);                  // checkpoint + restore
  const placeOrder = useBugCapsuleAction('cart.checkout', async () => { … });
  return <>
    <input value={coupon} onChange={(e) => setCoupon(e.target.value)} />
    <button data-bugcapsule-action="cart.checkout" onClick={() => void placeOrder()}>Checkout</button>
  </>;
}

<BrowserRouter>
  <BugCapsuleRouteListener />                         {/* route events with push/pop/replace */}
  <BugCapsuleErrorBoundary fallback={<Oops />}><Routes>…</Routes></BugCapsuleErrorBoundary>
  <BugCapsuleCaptureButton />                         {/* renders nothing in production or replay */}
</BrowserRouter>
ExportSignature
useBugCapsuleAction(id, handler) => handler
useTrackedInput(id, { sensitive? }) => ref callback for an uncontrolled input
useTrackedState(id, initial: string, { sensitive? }) => [value, setValue]
useBugCapsuleState(key, value, restore: (data) => void)
BugCapsuleErrorBoundaryfallback?: ReactNode | (error, reset) => ReactNode
BugCapsuleCaptureButtonlabel?, className?
BugCapsuleRouteListenerRender inside the router; combine with historyIntegration() for checkpoints

Integrations

Network: fetch and XMLHttpRequest

networkIntegration({
  fetch: true, xhr: true,                       // or fetchIntegration() / xhrIntegration()
  rules: [{ method: 'POST', path: '/api/orders/:id', ignoreBodyFields: ['nonce'] }],
  ignoreQuery: ['_', 'ts', 'timestamp', 'nonce', 'cachebust'],   // default
  maxBinaryBytes: 64 * 1024, maxKeyBodyBytes: 64 * 1024, maxBodyWaitMs: 30_000,
  exclude: ({ method, url }) => url.includes('/api/payments'),   // keeps method, URL, status, timing only
});

WebSockets

i

Available from 0.4.0-beta.3. Capsules with WebSockets are written as capsule spec v2, which older CLIs and tools reject: update the CLI everywhere it runs, CI included, before you turn the integration on. Capsules without sockets stay v1.

WebSocket capture is opt-in. Add the integration next to the others:

import { webSocketIntegration, rawBinarySerializer } from '@bugcapsule/web';

integrations: [
  networkIntegration(),
  webSocketIntegration({
    ignoreQuery: ['_', 'ts', 'timestamp', 'nonce', 'cachebust'],     // default; left out of the connection key
    // binary: { decode: (bytes, frame) => …, encode: (value, frame) => … },   // keep binary frames
    // binary: rawBinarySerializer,                                   // bytes as they are, not redacted
    // redactedReplayable: true,                                      // the app doesn't branch on redacted fields
  }),
],

Storage, history, IndexedDB

localStorageIntegration({ allowPrefixes: ['shop.'], denyPrefixes: ['shop.token'] })   // allowPrefixes is required
sessionStorageIntegration({ allowPrefixes: ['checkout.'] })
historyIntegration({ sanitizeLocation: (l) => l.replace(/\/reset\/[^/]+/, '/reset/:token') })
indexedDbIntegration([{ name: 'offline-cart', snapshot: async () => readCart(), restore: async (d) => writeCart(d) }])

historyIntegration wraps pushState/replaceState and listens to popstate/hashchange. Cookies, DOM content, input values (except tracked inputs) and storage outside the allowlist are never captured.

Configuration reference

FieldDefaultNotes
tokennoneThe SDK token (bcsdk.v1.…) from the dashboard: all a connection needs
appVersion, appBuildversion part of release, else unknown / 0Set them so Capsules and releases carry your real version
appId, appName, flavornullMax 200 chars
mode'production''debug' | 'qa' | 'production'
enabledtruefalse = no capture setup at all
backendnone{ url, ingestKey }: the advanced alternative to token, and wins over it. With neither, captures are saved locally only
gitSha, gitDirtynullMissing SHA or dirty tree → build.gitSha gap
stateSchemaVersion1
integrations[]A state integration is always appended
checkpointIntervalMs / windowMs / maxEvents30 000 / 300 000 / 20 000
productionSampleRate0.1Your project's sample rate can only lower it
autoCaptureOnFailure / maxCapsulesPerSessiontrue / 3Limit applies to auto-capture
captureNetworkBodies / maxBodyBytesoff in production; on in debug/QA / 256 KiBoff in production mode unless set to true; binary bodies need networkIntegration({ captureBinaryBodies: true }) (What's new)
collectionEnabled / maxRecorderBytestrue / 8 MiBconsent default and in-memory recorder budget; see consent and local data
redactKeys, redactHeaders, redactPatterns[]redactPatterns: RegExp[]
beforeRecord(type, data)—After built-in redaction; return null to drop
beforeCheckpoint(integration, data)—Return null/undefined to omit that snapshot
outboxLimits10 / 10 MiB / 7 days{ maxCount, maxBytes, maxAgeMs }
maxStaleConfigMs7 days
ignoreError(error)—Return true if not a failure
labels{}At most 100
capturePointerstrue
sensitiveSelector'[data-bugcapsule-sensitive]'Password inputs are always sensitive
allowReplayfalseWire to a dev/test build flag only
storageName'bugcapsule'IndexedDB database name
setupTimeoutMs5000

Methods

BugCapsule.action(id, handler)                    // wraps handler
BugCapsule.actionWithPayload(id, (payload) => …)
BugCapsule.trackInput(id, el, { sensitive })      // returns dispose()
BugCapsule.trackValue(id, set, { sensitive })     // { changed(value), dispose() } for custom widgets
BugCapsule.fail(id, message?); BugCapsule.check(id, condition, message?); BugCapsule.reportError(e)
BugCapsule.log(message, 'debug' | 'info' | 'warn' | 'error')
BugCapsule.route(location, source?, action?)      // explicit route events
BugCapsule.registerState(key, { serialize, restore }) // returns unregister; stateChanged(key)
await BugCapsule.capture({ note })                // CaptureResult | null
BugCapsule.now(); BugCapsule.date(); BugCapsule.random()
await BugCapsule.refreshRemoteConfig(); await BugCapsule.flushUploads()
await BugCapsule.pendingUploads(); await BugCapsule.savedCaptures()
BugCapsule.status()                               // connection + instrumentation, see step 2
BugCapsule.isInitialized; BugCapsule.isRecording; BugCapsule.isReplaying; BugCapsule.mode; BugCapsule.session

Replay with the CLI

{
  "runtime": "web",
  "web": {
    "url": "http://127.0.0.1:4317/",
    "installCommand": "npm ci",
    "buildCommand": "npm run build",
    "serveCommand": "npm run preview -- --port 4317 --strictPort",
    "env": { "VITE_BUGCAPSULE_ALLOW_REPLAY": "true" }
  }
}
bugcapsule run <id> -n 3                                  # builds (on the capsule's commit when pinned), serves, replays
bugcapsule verify <id> -n 3                               # builds the working tree
bugcapsule run <id> --app-url http://127.0.0.1:4317/      # against an already running build

The CLI passes BUGCAPSULE_GIT_SHA/BUGCAPSULE_GIT_DIRTY in the build environment; map them to your bundler's env (the shop demo's vite.config.ts maps them to VITE_BUGCAPSULE_GIT_SHA with define). The CLI refuses to start when something already serves web.url (it might be an old build).

Per attempt, bugcapsule-web-runner:

  1. Fetches /settings and /capsule from the CLI's loopback server.
  2. Launches Chromium with the recorded viewport, scale factor and locale, service workers blocked, a fresh browser context.
  3. Blocks every request to an origin other than the app's (and web.allowOrigins) and reports network.escaped.
  4. Restores localStorage/sessionStorage in an init script before any page script, exposes the Playwright bindings, and opens the checkpoint's location.
  5. The app's own SDK (with allowReplay: true) runs the engine: real pointer input through Playwright at recorded coordinates (hit-tested against the recorded target), semantic actions, tracked inputs, synthetic lifecycle; fetch/XHR served from the capsule.
  6. Posts the report to /result and prints a BUGCAPSULE_RESULT:: line. Default timeout 120 s.
✓

Production protection. Replay activates only when allowReplay: true and the page was opened by the runner. With allowReplay false nothing (URLs, storage, messages, scripts) can turn replay on. A script already in a page with allowReplay: true could imitate the runner bindings, one more reason to keep such builds out of production.

Uploads and storage

Shop demo

examples/shop: React app with four planted bugs (race, legacy cart, discontinued item, coupon) and a local API (api-server.mjs, port 4318). Environment: VITE_BUGCAPSULE_TOKEN (or, advanced, VITE_BUGCAPSULE_BACKEND_URL + VITE_BUGCAPSULE_INGEST_KEY, which win when set), VITE_BUGCAPSULE_ALLOW_REPLAY, VITE_BUGCAPSULE_DEMO_FIX (race|legacy|discontinued|coupon), VITE_SHOP_API_URL.

node examples/shop/api-server.mjs                       # shop API on 127.0.0.1:4318
cd examples/shop
VITE_BUGCAPSULE_TOKEN=bcsdk.v1.… \
VITE_BUGCAPSULE_ALLOW_REPLAY=true npx vite build && npx vite preview --port 4317 --strictPort

Browsers and limitations

BrowserCaptureReplay
Chromium / Chrome / Edgetestedtested
Firefox, SafariPlaywright Firefox/WebKit tested; released Safari/iOS coverage remains limitednot implemented

Monitoring and release readiness

This SDK also sends error events, release-health sessions, traces and feedback. Configure those separately from capsule recording using Monitoring, upload release symbols, and complete the integration checklist. For approved artifacts, versions and upgrade rules, start with Installation and upgrades.