Overview
This documentation describes how to integrate Miso's client-side SDK for Javascript. This SDK allows you to:
- Configure and create a client instance
- Retrieve search and recommendation results using APIs
- Track user interactions with APIs, with automatically managed context information
The latest version of the Miso client-side SDK for Javascript is 1.4.2 and can be downloaded via npm.
Installation
npm module
To install the SDK as an npm module, run the following in your project directory:
npm install --save @miso.ai/client-sdk
Script tag
This SDK is also served by jsDelivr. You can include the SDK in your webpage with the following script tag:
<script src="https://cdn.jsdelivr.net/npm/@miso.ai/client-sdk"></script>
Configuration
Creating a Miso client
If you are using an npm module, create the client instance using the following example:
const MisoClient = require('@miso.ai/client-sdk');
const apiKey = '...';
const miso = new MisoClient(apiKey);
If you are accessing the SDK via script tag, create the client instance using the following example:
var apiKey = '...';
var miso = new MisoClient(apiKey);
If the SDK is loaded using an async
script tag, it will be ready in the following callback:
var apiKey = '...';
var misocmd = window.misocmd || (window.misocmd = []);
misocmd.push(function () {
var miso = new MisoClient(apiKey);
});
Context Variables
This is a quality-of-life module. You can save shared variables with the context API, so they will be passed into other API calls automatically.
Anonymous IDs
The SDK takes care of anonymous_id
automatically using session storage. It will be managed and passed into API calls automatically.
You can also set anonymous_id
by yourself:
miso.context.anonymous_id = 'my_anonymous_id';
User ID and hash
Set user_id
and user_hash
:
miso.context.user_id = 'user_id';
miso.context.user_hash = 'user_hash';
See REST API for more details about user_hash
.
Request Options
Timeout
You can specify a timeout in milliseconds:
try {
const { products } = await miso.api.recommendation.userToProducts(payload, { timeout: 5000 });
} catch (e) {
// ...
}
Bulk
You can call APIs in bulk mode. API calls in bulk mode from the same event loop will be packed into a single HTTP request to save communication resources:
const [ responseA, responseB ] = await Promise.all([
miso.api.recommendation.userToProducts(payload, { bulk: true }),
miso.api.recommendation.userToTrending(payload, { bulk: true })
]);
Be aware of asynchrony. In the following example, the bulk mode will NOT be in effect:
const responseA = await miso.api.recommendation.userToProducts(payload, { bulk: true });
const responseB = await miso.api.recommendation.userToProducts(payload, { bulk: true });