SDKsWeb SDK

Installation

Add the Web SDK to your project with the ESM or standalone build

The Web SDK ships in two builds. Use the ESM build for any project with a bundler. Use the standalone build to add the SDK to a plain HTML page with a single script tag.

Choose a build

ESM buildStandalone build
Installnpm install<script> tag
Best forReact, Vue, Angular, any bundlerPlain HTML, no build step
Dependencies@reduxjs/toolkit peer dependencyAll bundled
StylesheetImport separately (/styles)Included automatically
Entry pointNapsterCompanionApiSdk importwindow.napsterCompanionApiSDK

This page covers version 2. Upgrading from 1.x? See Upgrading to Web SDK 2.0.

ESM build

Install the SDK and its peer dependency:

npm install @touchcastllc/napster-companion-api@2 @reduxjs/toolkit

@reduxjs/toolkit is a required peer dependency. Import the SDK and its stylesheet in your application:

import { NapsterCompanionApiSdk } from "@touchcastllc/napster-companion-api";
import "@touchcastllc/napster-companion-api/styles";

You can also load the stylesheet from CSS instead of importing it in JavaScript:

@import "@touchcastllc/napster-companion-api/styles";

Or load it from the CDN with a <link> tag. Pin the CDN URL to the version you installed from npm, so the styles always match the SDK code:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@touchcastllc/[email protected]/lib/index.css" />

The SDK can also load two optional packages, @tensorflow/tfjs and @tensorflow-models/face-landmarks-detection. It loads them only for a feature that needs them, so you don't need to install them. Some bundlers, such as esbuild and Angular's builder, fail the build when they can't resolve these packages, and webpack shows a "Module not found" warning. Mark both packages as external in your bundler:

esbuild src/main.js --bundle --external:@tensorflow/tfjs --external:@tensorflow-models/face-landmarks-detection

Standalone build

Add a single script tag to your HTML. All dependencies are bundled, so no build tools are needed.

<script src="https://cdn.jsdelivr.net/npm/@touchcastllc/napster-companion-api@2/lib/index.standalone.js"></script>

The SDK attaches itself to window.napsterCompanionApiSDK. The stylesheet is included in the standalone bundle, so you don't need a separate CSS import.

The global is the SDK object itself. Named exports such as Position and DataChannelMessageType are only available in the ESM build. With the standalone build, pass their string values instead, such as "bottom-right".

The @2 in the URL loads the latest 2.x release, so a future major version can't break your page. Pin an exact version, such as @2.0.0, if you want to control every update.

Add a token endpoint

The SDK connects with a short-lived connection token, which your backend creates with your API key. Never put the API key in the browser.

Add an endpoint on your server that creates a session and returns its token. For example, in Node.js with Express:

app.post("/api/avatar-session", async (req, res) => {
  const response = await fetch(
    "https://napster-api.napster.com/public/agents/agent_abc123/connections",
    {
      method: "POST",
      headers: {
        "X-Api-Key": process.env.NAPSTER_API_KEY,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ channelType: "webrtc" }),
    },
  );
  if (!response.ok) {
    return res.status(response.status).json(await response.json());
  }
  const { token } = await response.json();
  res.json({ token });
});

This example uses the global fetch, which needs Node.js 18 or later.

Then give the SDK a function that calls it. The SDK calls the function itself once microphone permission settles, right before it connects. It calls it again for every new session attempt, so return a fresh token each time and don't cache it. If the function throws or returns an empty token, the SDK reports the error through onError.

async function getToken() {
  const response = await fetch("/api/avatar-session", { method: "POST" });
  const { token } = await response.json();
  return token;
}

NapsterCompanionApiSdk.init(getToken);
// With the standalone build: napsterCompanionApiSDK.init(getToken);

For all request options, such as externalClientId for memory across sessions, see Create an agent connection. For the full WebRTC flow, see Connecting to an agent. To authenticate your backend, see Authentication.

Next steps

On this page