MisoClient

The entry point of Miso SDK. Create an instance with your API key to access Miso APIs.

When loaded from the CDN, the class is available as window.MisoClient. To run code once the SDK is ready, push a function to window.misocmd:

const misocmd = window.misocmd || (window.misocmd = []);
misocmd.push(async () => {
  const client = new MisoClient({ apiKey: "..." });
  const response = await client.api.search.search({ q: "shoes" });
});

With the full SDK, plugins also add features to each instance, such as client.ui (UI elements and layouts) and client.workflows (Ask, Search, etc.).

import MisoClient from "@miso.ai/client-sdk";

const client = new MisoClient({ apiKey: "..." });
client.context.user_id = "user-123";

Constructor

new MisoClient(options)
Parameter Type Description
options Object or string Client options, or just the API key as a string.
options.apiKey string Your Miso publishable API key. Required.
options.apiHost string Shorthand to set both dataEndpoint and eventEndpoint. Optional.
options.dataEndpoint string Base URL for data APIs (Ask, Search, Recommendation). Defaults to https://api-edge.askmiso.com/v1. Optional.
options.eventEndpoint string Base URL for the Interactions API. Defaults to https://api.askmiso.com/v1. Optional.
options.request Object Default settings applied to every API request. Optional.
options.request.timeout number Request timeout in milliseconds. Optional.
options.request.retry number Number of retries on network errors, timeouts, and 5xx responses. Off by default, as retrying a non-idempotent call (such as posting a question) may not be safe. Can also be set per request. Default 0. Optional.

Throws Error: If no API key is given.

Static properties

MisoClient.cmdDone

Resolves after all functions pushed to window.misocmd before the SDK loaded have run.

Type: Promise<void>

MisoClient.helpers

Utility functions.

Type: Object

MisoClient.instances

All client instances created so far.

Type: MisoClient[]

MisoClient.plugins

Manage plugins, which add optional features to the SDK.

  • use(id, options) activates a plugin, or reconfigures it if already active.
  • install(id, options) is like use(), but loads standard (std:) plugins from the CDN if needed, and returns a promise of the installed plugin.
  • register(PluginClass) makes a custom plugin class available by its id.
  • whenInstalled(id) returns a promise that resolves when the plugin is installed.

Type: Object

MisoClient.plugins.use("std:debug");
await MisoClient.plugins.install("std:analytics");

MisoClient.version

The SDK version.

Type: string

Static methods

MisoClient.any()

Get the first client instance, waiting for one to be created if there is none yet.

Returns Promise<MisoClient>.

MisoClient.attach()

Assign MisoClient to window.MisoClient. The default entry point does this automatically; call it only when you import from a detached entry point.

If window.MisoClient already holds a different SDK copy, it is left untouched and a warning is logged.

MisoClient.debug(...args)

Log debug messages. Does nothing unless the std:debug plugin is in use.

Parameter Type Description
...args *

MisoClient.on(name, callback)

Listen to SDK-level events. For example, 'create' fires with each new client instance, including the ones created before you started listening.

Parameter Type Description
name string Event name.
callback function

Returns function: A function to remove the listener.

MisoClient.once(name)

Wait for the next occurrence of an SDK-level event.

Parameter Type Description
name string Event name.

Returns Promise<*>.

Instance properties

client.api

Miso APIs, grouped by ask, search, recommendation, and interactions.

const response = await client.api.recommendation.userToProducts({ rows: 5 });
await client.api.interactions.upload({ type: "click", product_ids: ["P1"] });

client.context

User and site context sent along with API requests, such as user_id, user_hash, anonymous_id, and site. Set these to identify the current user.

Type: Object

client.options

The options passed to the constructor, after normalization. Read-only.

Type: Readonly<Object>

client.version

The SDK version.

Type: string