Troubleshooting
Common Web SDK issues, debug mode, and performance notes
Common problems when integrating the Web SDK and how to resolve them. For channel-level behavior, see the WebRTC channel page and Session errors and shutdown.
Common issues
Tokens, microphone, styling, layout, mounting
Debug mode
Turn on verbose logging
Performance
Bundle size and memory
Common issues
"Module not found"
Make sure the package and its peer dependency are both installed:
npm install @touchcastllc/napster-companion-api @reduxjs/toolkit@reduxjs/toolkit is a required peer dependency of the npm package (the ESM and CommonJS builds). The standalone build bundles it. See Installation.
"init() requires a getToken() function"
Since version 2, init takes a function that returns a token, not the token itself. Wrap your existing token fetch in a function:
// Before (1.x)
const { token } = await fetch("/api/avatar-session", { method: "POST" }).then((r) => r.json());
NapsterCompanionApiSdk.init(token, config);
// After (2.x)
NapsterCompanionApiSdk.init(async () => {
const { token } = await fetch("/api/avatar-session", { method: "POST" }).then((r) => r.json());
return token;
}, config);See Upgrading to Web SDK 2.0 for the other changes.
Invalid or expired token
The session fails right after the microphone prompt, and onError reports getToken() returned an empty token, Failed to decode connection token, or WebSocket connection closed. Check these:
- Make sure your endpoint creates a new session on every call. The SDK calls
getTokenagain for each attempt, and a token works only once. - Return the token exactly as your backend received it. Don't decode or modify it.
- Check that
getTokenreturns the token string, not the whole response object.
If getToken throws, the SDK removes the widget and passes your error to onError. You can call init again from there.
Microphone access is blocked
If the visitor blocks the microphone, the SDK shows a card asking them to allow it in the browser's site settings, with a Check again button that retries. If the microphone stops working during a session, a warning badge appears on the mute button. Clicking it asks for the microphone again. If the browser still blocks it, the same card appears instead.
If you embed the page in an iframe, the frame must grant the permission:
<iframe src="https://example.com/agent" allow="microphone"></iframe>If you use screen sharing, add display-capture as well: allow="microphone; display-capture".
The widget doesn't appear where I put it
The floating widget always mounts on document.body, even if you pass mountContainer. To render the agent inside your own element, set layout: { type: "inline", mountContainer }. See Layout.
With persistence enabled, the SDK always uses the floating widget and ignores mountContainer, even with type: "inline". It logs a console warning when you set both.
The controls are cut off
Your inline container is smaller than the controls need. The SDK writes a console warning that says how much room the controls need. See the minimum sizes.
My first command is ignored
init resolves before the session is connected, and the SDK drops commands sent before then. Each dropped command logs a console warning such as Transport not available. Send your commands from onAvatarReady when isReady is true.
"sendCommand is not a function"
With the launcher (button.enabled: true), init resolves with a controller, not an instance. The controller has start, end, getInstance, and destroy. To send commands, use the instance that start() resolves with, or call getInstance() once the session is live:
const launcher = await NapsterCompanionApiSdk.init(getToken, {
button: { enabled: true },
onAvatarReady: (isReady) => {
if (isReady) launcher.getInstance()?.sendCommand(command);
},
});See The controller.
The avatar appears unstyled
The stylesheet isn't loaded. In the ESM build you must import it once:
import "@touchcastllc/napster-companion-api/styles";Or load it via a <link> tag — see Installation. The standalone build bundles the styles, so this doesn't apply there.
The mount container doesn't exist
Initialize the SDK only after the container is in the DOM. In plain HTML, wait for the document to be ready:
document.addEventListener("DOMContentLoaded", async () => {
await NapsterCompanionApiSdk.init(getToken, {
layout: { type: "inline", mountContainer: "#avatar-container" },
});
});If the selector matches no element, the SDK falls back to document.body. It logs a warning about this only when debug: true is set. In React, Vue, and Angular, the framework examples mount inside lifecycle hooks that guarantee the element exists.
"SDK is already initialized"
init rejects if the SDK is already initialized. That means a session is running, or a launcher is still on the page. Call destroy() on the previous instance or controller first. The rejected call reports the error through its own onError and doesn't affect the widget that's already running.
In development, React StrictMode mounts each component twice, which can call init twice and trigger this error. The React example in framework examples shows a pattern that handles the double mount.
The session ends when the user navigates
A full page load tears down the session. To keep one session alive across pages, enable persistence — and note its rules:
- Links to another origin end the session by default. That includes your own subdomains, which are a different origin. To keep a domain inside the frame, list it in
persistence.include.domains. The session then survives the link, but the SDK can't see or act inside pages from that domain. - Pages of your own site listed in
persistence.exclude.urlsend the session on purpose. - With
persistence.iframeOnNavigate: true, the starting page stays unframed until the first link click or form submission. Until then, navigating from code (location.assign) ends the session unless you dispatch the persist-navigate event instead. So does a link whose click handler callsstopPropagation(). The default frames the page wheninitruns, so it has no such gap.
Debug mode
Set debug: true to get verbose SDK logging in the console. Pair it with onError and onData to see what's flowing:
await NapsterCompanionApiSdk.init(getToken, {
debug: true,
onError: error => console.error("SDK error:", error),
onData: data => console.log("SDK data:", data),
});Warnings about configuration mistakes and errors always print, even without debug. Setting debug: true adds the detailed connection, audio, and session logs.
Performance
Bundle size
Use the ESM build. It shares @reduxjs/toolkit with your app instead of bundling its own copy, as the standalone build does.
The SDK ships as a single module, so there are no per-feature imports to trim. Features you turn off in the config are still part of the bundle.
Memory
Always call destroy() when the widget unmounts — it closes the connection and releases the microphone (plus any screen-share or face-tracking camera capture, if used). In component frameworks, call it from the unmount hook:
useEffect(() => {
return () => {
instance?.destroy();
};
}, [instance]);