• Public
  • Public/Protected
  • All


Search.io Javascript SDK

npm (scoped) Netlify Status build size license

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

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

You can also quickly generate search interfaces from the Search.io 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, SearchIOAnalytics, etc... } from "@sajari/sdk-js";

const client = 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>
const client = 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 Search.io account you can sign up.

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

Create a SearchIOAnalytics instance to track events against the query id that produced a search result.

const searchio = new SearchIOAnalytics("<account_id>", "<collection_id>");

Perform a search on the specified pipeline and handle the results. Here we're searching our collection using the app pipeline and updating the analytics instance with the current query id. The field value you specify must be the name of a field in your schema with the Unique constraint.

const values = { q: "puppies" };
.search(values, { type: "EVENT", field: "id" })
.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 event tracking. This will persist the tracking event against the current query id in localStorage and send the event data to Search.io to track how users use search results and/or move through an ecommerce purchase funnel.

const values = { q: "puppies" };
.search(values, { type: "EVENT", field: "id" })
.then(([response, values]) => {
response.results.forEach((r) => {
const item = document.createElement("a");
item.textContent = r.values.name;
item.href = r.values.url;
item.onclick = () => {
searchio.track("click", r.id);

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

Tracking additional events

When a user moves further through your purchase funnel (e.g. adding an item to their shopping cart, completing a purchase, etc) you'll want to track that also, as it will provide feedback to the ranking system. The correct query id will be preserved throughout the ecommerce funnel as long as an event with type click was already tracked earlier.

document.querySelector(".add-to-cart").addEventListener("click", (e) => {
searchio.track("add_to_cart", e.target.id);

document.querySelector(".complete-purchase").addEventListener("click", (e) => {
document.querySelectorAll(".cart-item").forEach((item) => {
searchio.track("purchase", item.id);


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


We use the MIT license

Generated using TypeDoc