Upgrading to Web SDK 2.0
Update your Web SDK integration from version 1.x to 2.0
Web SDK 2.0 changes how you start the SDK and how you configure it. Most integrations need these changes: pass a token function instead of a token, move the config into layout, style, and agent, and rename a few methods and options. This page walks through each one.
npm install @touchcastllc/napster-companion-api and CDN URLs that use @latest now load 2.0. If your code still passes a token string to init, it fails with init() requires a getToken() function. Pin @1.5.0 in both the script and the stylesheet URL until you've upgraded, or follow the steps below.
Upgrade steps
The changes most integrations need
Config key map
Where each 1.x option moved
Other changes
Behavior that changed without a code change
Upgrade steps
Update the package
npm install @touchcastllc/napster-companion-api@2For the standalone build, change the version in the script URL to @2:
<script src="https://cdn.jsdelivr.net/npm/@touchcastllc/napster-companion-api@2/lib/index.standalone.js"></script>If you load the stylesheet from the CDN with a <link> tag, change its version to @2 as well.
Pass a token function to init
init now takes a function that returns a token. The SDK calls it right before it connects, after the visitor answers the microphone prompt, so the token is always fresh.
Before:
const { token } = await fetch("/api/avatar-session", { method: "POST" }).then((r) => r.json());
const instance = await NapsterCompanionApiSdk.init(token, config);After:
async function getToken() {
const response = await fetch("/api/avatar-session", { method: "POST" });
const { token } = await response.json();
return token;
}
const instance = await NapsterCompanionApiSdk.init(getToken, config);Your backend endpoint doesn't change. If getToken fails, the error goes to onError.
Move the config into layout, style, and agent
Placement options now live in layout, appearance options in style, and the agent's photo in agent.
Before:
NapsterCompanionApiSdk.init(token, {
mountContainer: "#avatar-container",
layout: "inline",
avatarStyle: { view: "rectangle" },
className: "my-agent",
});After:
NapsterCompanionApiSdk.init(getToken, {
layout: { type: "inline", mountContainer: "#avatar-container" },
style: { view: "rectangle", className: "my-agent" },
});The floating widget (layout.type: "fixed", the default) now always mounts on document.body and ignores mountContainer. If you passed mountContainer without layout: "inline", the widget's root is no longer placed inside your element. Set layout: { type: "inline", mountContainer } if you want it there.
2.0 doesn't throw for 1.x config keys at runtime, though TypeScript flags them. Most are ignored. A 1.x layout string leaves the widget floating in the corner instead of inside your container. A 1.x style object replaces the new style block, so the widget loses its shape. Check each key against the key map below.
Rename methods and options
| 1.x | 2.0 |
|---|---|
instance.updateAvatarStyle({ view }) | instance.updateStyle({ view }) |
instance.updateStyles({ ... }) | Removed. Use style.className and your own CSS. |
features.showSDKLoader | features.loadingIndicator: { enabled }. The color, type, and class options are gone. |
features.backgroundRemoval.enabled | Set automatically from style.view: on for "silhouette", off otherwise. |
Send commands once the session is live
init resolves as soon as the widget is on the page, before the session connects. In 2.0 the token is also fetched after that point, so the gap is longer. Commands sent before the session is live are dropped. If you send a greeting or other command right after await init(...), move it into onAvatarReady:
NapsterCompanionApiSdk.init(getToken, {
onAvatarReady: (isReady) => {
if (isReady) {
// send your first command here
}
},
});Config key map
| 1.x option | 2.0 option |
|---|---|
mountContainer | layout.mountContainer (inline layout only) |
position | layout.position |
layout: "fixed" | "inline" | layout.type |
avatarStyle.view | style.view |
avatarStyle.borderWidth, borderColor, borderStyle | Removed. Style the container or style.className with CSS. |
style (inline CSS object) | Removed. style is now the appearance block. The StyleObject type is gone too. |
className | style.className |
button.avatarUrl | agent.photoUrl |
features.showSDKLoader | See Rename methods and options. |
analytics | Removed. |
functions | Removed. It had no effect in 1.x either. Configure functions on the agent. |
style.view is required whenever you pass style. Add view: "round" to keep the 1.x default.
New options in 2.0:
style.accentColor: your brand color for the buttons and loading ring. See Accent color.agent.photoUrl: the agent's photo, shown while loading and on the launcher.button: a launcher button that connects on click. See Start on click.onSessionStatusChange: each stage of the session. See Session status.
Other changes
These need no code change, but you may notice them:
- Inactivity warning. The countdown before an idle session ends is now at least 20 seconds. While the Still there? card shows, the microphone is silenced without changing the mute button. Clicking I'm here adds an "I'm here" user message to the transcript. See Inactivity timeout.
onAvatarReadyfirestrueas soon as the session is live. Under deferred video that's on the audio, before the video appears. It firesfalsewhen the session ends.- Smaller floating widget. The fixed widget is now 240px wide with a 16px margin from the viewport edge.
- Function call results.
DataChannelMessageType.FUNCTION_CALL_OUTPUTnow sendssend_function_output, which the server accepts. In 1.x it sent a value the server ignored. - Styles stay inside the widget. The SDK's CSS no longer affects the rest of your page. If you overrode the SDK's classes in your own CSS, check that your overrides still apply.
- A second
initcall while a session is running is rejected without ending that session.