• Public
  • Public/Protected
  • All


Sajari Javascript SDK

npm (scoped) Netlify Status build size license

This SDK is a lightweight JavaScript client for querying the Sajari API.

Checkout our React SDK, for a complete set of customisable UI components, and more.

You can also quickly generate search interfaces from the Sajari admin console.

Table of Contents



npm install --save @sajari/sdk-js@next

# or with yarn
yarn add @sajari/sdk-js@next

Usage within your application:

import { Client, DefaultSession, TrackingType, etc... } from "@sajari/sdk-js";

// new Client("<account_id>", "<collection_id>")...


Note that when using the SDK via a <script> tag in a browser, all components will live under window.SajariSDK:

<script src="https://unpkg.com/@sajari/sdk-js@^2/dist/sajarisdk.umd.production.min.js"></script>
  // new SajariSDK.Client("<account_id>", "<collection_id>")...

Getting Started

Create a Client for interacting with our API, and then initialise a pipeline to be used for searching. The pipeline determines how the ranking is performed when performing a search.

If you don't have a Sajari account you can sign up here.

const websitePipeline = new Client("<account_id>", "<collection_id>").pipeline(

Create a session to track the queries being performed via click tracking. In this case we're using q to store the query on the InteractiveSession.

const clickTrackedSession = new InteractiveSession(
  new DefaultSession(TrackingType.Click, "url")

Perform a search on the specified pipeline and handle the results. Here we're searching our collection using the website pipeline with our tracked session.

const values = { q: "FAQ" };
  .search(values, clickTrackedSession.next(values))
  .then(([response, values]) => {
    // Handle response...
  .catch((error) => {
    // Handle error...

Handling results

Now we're going to add a basic rendering of the results to the page with integrated click tracking. This will redirect the user through the Sajari token endpoint to the real page identified by the result, registering their "click" on the way through.

const values = { q: "FAQ" };
  .search(values, clickTrackedSession.next(values))
  .then(([response, values]) => {
    response.results.forEach((r) => {
      const title = document.createElement("a");
      title.textContent = r.values.title;
      title.href = r.values.url;
      title.onmousedown = () => {
        title.href = r.token.click;

  .catch((error) => {
    // Handle error...

Consuming an interaction token

On each result when using TrackingType.Click or TrackingType.PosNeg, there is a set of tokens. These tokens allow you to provide feedback to the ranking system. When a user interacts with a result, you can send back the token with some extra information.

const { Client } = require("@sajari/sdk-js");

const client = new Client("<account_id>", "<collection_id>", {
  key: "<key_id>",
  secret: "<key_secret>",

The following invocation of the consume function, is noting that this particular
interaction was a "purchase" and the user purchasing the item was 20 years old
(this information coming from some system that you operate.)
client.interactionConsume(token, "purchase", 1.0, {
  age: "20",


For full documentation, see https://sajari-sdk-js.netlify.com/.


We use the MIT license

Generated using TypeDoc