Skip to content

Use additional SDK methods#

This page lists the properties and methods of Storyteller.sharedInstance, the SDK object you initialize and use to open players, count content, and read SDK state. For methods on a view, such as reloadData and destroy, see Configure views. The API reference lists every public member, including theme, currentTheme, currentApiKey, and customInstanceHost.

Examples that use await must run inside an async function or a JavaScript module.

Set the global delegate#

Storyteller.sharedInstance.delegate = {
  // Your callbacks here
};

Learn more

To set the global delegate, see Handle global callbacks.

Instance properties#

isInitialized#

readonly isInitialized: boolean

true after an initialize call succeeds.

const initialized = Storyteller.sharedInstance.isInitialized;

isPlayerVisible#

readonly isPlayerVisible: boolean

true while a Story or Clips player is open, and false after it is dismissed.

const isStoryPlayerVisible = Storyteller.sharedInstance.isPlayerVisible;

version#

version: string

The SDK version.

const storytellerVersion = Storyteller.sharedInstance.version;

eventTrackingOptions#

The privacy and tracking options. Each assignment replaces every option: options you leave out return to their defaults. See Control privacy and tracking.

Instance methods#

initialize#

initialize(apiKey: string, userInput?: { externalId?: string | null }): Promise<void>

Initializes the SDK with your API key and, optionally, your ID for the current user. Call it before you use other methods, and wait for the promise.

  • apiKey: your Storyteller API key
  • userInput.externalId: your ID for the signed-in user, or null for an anonymous user. See Setting a user ID.
try {
  await Storyteller.sharedInstance.initialize('demo-api-key', {
    externalId: 'your-user-id',
  });
} catch (error) {
  console.error('Storyteller could not initialize.', error);
}
  • Calls with the same API key and externalId made while an earlier call is still running share that call's promise. React Strict Mode's repeated effects therefore initialize the SDK once.
  • With the default privacy options, initialize also loads the user's viewing history from Storyteller, and rejects if that request fails.
  • Each call resets Storyteller.sharedInstance.theme to its defaults. Set the global theme after the promise resolves.
  • The promise rejects with an Error whose message starts with the error type, such as InvalidApiKeyError, NetworkError, or NetworkTimeoutError. A call with no API key rejects with a string. See Handle initialization errors.

dismissPlayer#

dismissPlayer(animated: boolean): void

Closes the open Story or Clips player. If no player is open, it does nothing. It doesn't close a StorytellerEmbeddedClipsPlayerView.

  • animated: true plays the close animation.
Storyteller.sharedInstance.dismissPlayer(true);

disablePlayback#

disablePlayback(): void

Pauses the open Story player and the current Clip in every Clips player on the page. Stories and Clips that open while playback is disabled stay paused until you call enablePlayback. Use it when your page shows something over Storyteller content, such as your own video or a dialog.

Storyteller.sharedInstance.disablePlayback();

enablePlayback#

enablePlayback(): void

Allows playback again after disablePlayback, and resumes the open Story and the current Clip. Playback is enabled by default.

Storyteller.sharedInstance.enablePlayback();

enableLogging#

enableLogging(): void

Turns on the SDK's info, warning, and log messages in the browser console. The SDK always logs errors. Use it for debugging and monitoring, and call it before initialize to see initialization logs.

Storyteller.sharedInstance.enableLogging();

The Storyteller Web Showcase turns on logging in its debug mode with this enableLogging call.

getStoriesCount#

getStoriesCount(categoryIds: string[]): Promise<number>

Returns the total number of available Stories in the given Category IDs. The SDK sends one small count request for each Category and adds the results. An empty array returns 0 without a request.

The SDK sends the count requests only after initialize succeeds. A count call made before or during initialization waits for it. If initialization never starts or fails, the promise stays pending until a later initialize call succeeds. A failed count request rejects the promise.

Use the count method for an optional row that should appear only when it has content. Load a primary row directly, without a count, to avoid an extra request.

await Storyteller.sharedInstance.initialize('demo-api-key');

const count = await Storyteller.sharedInstance.getStoriesCount(['category-id']);

if (count > 0) {
  // Show the row only when the Category has Stories
  new Storyteller.StorytellerStoriesRowView('stories-row-id', ['category-id']);
}

getClipsCount#

getClipsCount(collectionId: string): Promise<number>

Returns the number of available Clips in the collection. It waits for initialization and handles errors in the same way as getStoriesCount. Use it before you create an optional Clips view.

const clipsCount = await Storyteller.sharedInstance.getClipsCount('collection-id');

openStory#

openStory(id: string): Promise<void>

Opens the Story with this ID. The promise rejects if the SDK can't open the Story, for example because it can't be found. See Open a player programmatically.

await Storyteller.sharedInstance.openStory('story-id');

openStoryByExternalId#

openStoryByExternalId(externalId: string): Promise<void>

Opens the Story with this external ID. The promise rejects if the SDK can't open the Story, for example because it can't be found.

await Storyteller.sharedInstance.openStoryByExternalId('story-external-id');

openPage#

openPage(pageId: string): Promise<void>

Opens the Story that contains this Page, starting at the Page. The promise rejects if the SDK can't open the Page, for example because it can't be found.

await Storyteller.sharedInstance.openPage('page-id');

openCategory#

openCategory(categoryId: string, storyId?: string): Promise<void>

Opens the Story player for the Category, at the Story with the ID storyId. If you leave out storyId, or it isn't in the Category, the first Story in the Category opens. The promise rejects if the SDK can't open the Category, for example because it can't be found.

await Storyteller.sharedInstance.openCategory('category-id', 'story-id');

openCollection#

openCollection(
  collectionId: string,
  destination?: { categoryId?: string; clipId?: string },
  openedReason?: OpenedReason.deepLink
): Promise<void>

Opens the Clips player for the collection. It accepts the following parameters:

  • collectionId: the ID of the collection to open.
  • destination: the clipId or categoryId to show when the collection opens. If you leave it out, or the Clip or Category isn't in the collection, the first Clip in the collection opens.
  • openedReason: the reason reported in analytics events. It accepts only Storyteller.OpenedReason.deepLink: pass it when the user arrived through a deep link. If you leave it out, the SDK sets the reason.

The promise rejects if the SDK can't open the collection, for example because it can't be found.

await Storyteller.sharedInstance.openCollection(
  'collection-id',
  { clipId: 'clip-id' },
  Storyteller.OpenedReason.deepLink
);

openClipByExternalId#

openClipByExternalId(collectionId: string, externalId: string): Promise<void>

Opens the Clips player for the collection, at the Clip with this external ID. The promise rejects if the SDK can't open the Clip, for example because the collection has no Clip with that external ID.

await Storyteller.sharedInstance.openClipByExternalId(
  'collection-id',
  'clip-external-id'
);

Deprecated members#

These members still work in version 11, but don't use them in new code:

Member Use instead
openClip(id, onError?) openCollection or openClipByExternalId
enableEventTracking() eventTrackingOptions
disableEventTracking() eventTrackingOptions
currentUserId None. It always returns an empty string.