SDK JS-Client

Introduction

The Search JS-Client allows you to call an Inbenta API endpoint quickly and easily with javascript.

Note: All examples shown are set in a Production environment.

Setup

Steps to follow:

  1. Integration

    Include the Inbenta Search SDK script somewhere in your page:

     <script src="https://sdk.inbenta.io/search/<version>/inbenta-search-client.js"></script>
  2. Creation

    Next, create a new client instance. To do so, you will need your API Key (public key) and either an API secret or an access token. See Finding your API credentials in the Help Center to obtain your credentials.

    The following example shows a creation using an access token. To get the access token, you should make a server-side call to the /auth endpoint. For more information, see the Authorization section.

      var client = InbentaSearchClient.createFromAccessToken('<your_access_token>', '<your_API_Key>');
  3. Use client

    Before using the client, we need to wait until it is ready:

     client.untilReady().then( function (response) {
       // Do awesome things
     });

Example

The following code generates a complete Inbenta application inside the "inbenta" div, using the build function to build a template.

<!doctype html>
<html lang="en">
<head>
    <script src="https://sdk.inbenta.io/search/<version>/inbenta-search-client.js"></script>
</head>
<body>
  <script type="text/javascript">
    var accessToken = '<your_access_token>';
    var apiKey = 'BVkiK9F7inLwEBEIUVrfpJhpOfjIfj8PyTVCiKu9WUs=';
    var client = InbentaSearchClient.createFromAccessToken(<your_access_token>, <your_API_Key>);
    client.untilReady().then( function (response) {
      client.search('toys').then(function(results) {
        console.log(results[0]);
      });
    });
  </script>
</body>

Versions

When you set up the SDK for your Inbenta product, you need to specify the version like this:

<script src="https://sdk.inbenta.io/search/<version>/inbenta-search-client.js"></script>

Versions are written in MAJOR.MINOR.PATCH format.

Important:

  • Do NOT cache or store these files, otherwise you will not benefit from bug fixing and minor upgrades.
  • Inbenta strongly recommends that you use a MAJOR.MINOR.PATCH version.

Examples

  • If only MAJOR is set, this script will use the highest available MINOR version.
    <script src="https://sdk.inbenta.io/search/1/inbenta-search-sdk.js"></script>
  • If MAJOR.MINOR are set, this script will use the highest available PATCH version.
    <script src="https://sdk.inbenta.io/search/1.2/inbenta-search-sdk.js"></script>
  • If you specify the full version, only this exact version will be used.
    <script src="https://sdk.inbenta.io/search/1.2.3/inbenta-search-sdk.js"></script>

Client Creation explanation

Create a new SDK instance. You will need your API Key and either an API secret or an access token.

Contact Inbenta to receive your API Key and your secret.

Important: We strongly recommend generating the access token on the server side. You should never publish your API secret on your site, or someone might take it and use it.

Create SDK instance with accessToken:

var client = InbentaSearchClient.createFromAccessToken(accessToken: String, apiKey: String, config: Object);

client.untilReady().then( function (response) {
  // ...
});

Configuration

The following table shows the available InbentaSearchSDK parameters:

Name Type Default Description 
userType integer 0 Defines the profiles identifier from the Search App's knowledge base. It uses an x-inbenta-user-type header to make API requests. For more information about this header, see the API Routes.
environment string production Defines the source environment from the Search App's knowledge base. It will be used as x-inbenta-env header. Possible values:
  • production (default): This environment logs the data for the reports displayed in the Inbenta dashboards and retrieves the contents/categories published in live.
  • preproduction: This environment does not log any data and retrieves the contents/categories published in live.
  • development: This environment does not log any data and retrieves the contents/categories published in edit.
For more information about this header, see the API Routes.

Methods

The methods page is available here.

Handling Errors

Click here to see a list of Inbenta error codes.

// Search example
client.search('toys', [
  {
    filters: ['color:red', 'year:2017'],
    attributes: ['name', 'color', 'year']
  }
]).then(function (results) {
  console.log(results[0]);
}).catch(function (error) {
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
  });