Developers

SDK JS-Client

Introduction

The Knowledge 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

There are two ways you can set up the JS Client: when a SDK is already initialized, or standalone.

With a SDK initialized

If a SDK is already initialized, this means that you already have a client that you can use. As a consequence, there is no need to initialize it again. To access the JS-Client, insert the following before the closing <body> tag of your HTML:

var client = sdk.client

Standalone

Insert the following code in the <head> section of your HTML. If you need compatibility with IE11 browsers, use the version with polyfills:

<script src="https://sdk.inbenta.io/km/<version>/inbenta-km-client.js" integrity="<hash-type>-<hash>"  crossorigin="anonymous"></script>

If you do not need compatibility with IE11 browsers, you can use the version without polyfills:

<script src="https://sdk.inbenta.io/km/<version>/inbenta-km-client-no-polyfills.js" integrity="<hash-type>-<hash>"  crossorigin="anonymous"></script>

Then, insert the following before your closing <body> tag (See full example in Demonstration below):

var client = InbentaKmClient.createFromDomainKey(<your_Domain_key>, <your_API_Key>);

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/km/<version>/inbenta-km-client.js" integrity="<hash-type>-<hash>" crossorigin="anonymous"></script>

Versions are written in MAJOR.MINOR.PATCH format.

Important

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/km/1/inbenta-km-client.js"></script>
  • If MAJOR.MINOR are set, this script will use the highest available PATCH version.
    <script src="https://sdk.inbenta.io/km/1.2/inbenta-km-client.js"></script>
  • If you specify the full version, only this exact version will be used.
    <script src="https://sdk.inbenta.io/km/1.2.3/inbenta-km-client.js" integrity="sha384-549ni4+Y7QvqPcjqufrsbnQALdf0r62sYlmL15ZqIQBkwoWM9NKPEf6uDOBwIXmu" crossorigin="anonymous"></script>
Note

An SRI check is available for this SDK. For more information, see the SDK Subresource Integrity page. Remember that SRI only works with full versions.

Demonstration

<body>
  <div id="inbenta">
  </div>
  <script type="text/javascript">
    var access_token = "<your_access_token>"; //Get it from /auth endpoint with the secret and API Key
    var options = {
        userType:'2',
        environment:'preproduction'
    };
    var inbentaKey = "<your_API_Key>";
    var client = InbentaKmClient.createFromAccessToken(<your_access_token>, <your_API_Key>, options);
    client.search('This is my test query', {}).then( function (response) {
      // Do something with the results
    });
  </script>
</body>

Options

Name Type Default Description
userType integer 0 Defines the profile identifier from the Knowledge instance knowledge base. It will be used in client as x-inbenta-user-type header to make API requests. For more information about this header, see the API Routes.
environment String production Defines source environment from the Knowledge instance knowledge base. It will be used in client as x-inbenta-env header. For more information about this header, see the API docs.

Methods

All JS Client methods are described in the following page: JS Client Methods.

Handling Errors

Click here to see a list of API error codes.

// Search example
client.search('movies').then(function(response){
  response.data.results.forEach(function(result) {
    console.log(`Result(${result.id}): ${result.title}`);
  });
}).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);
  });

Libraries and Browser Support

The JavaScript SDK depends on:

  • vue 2.5.17
  • axios 0.26.1
  • CORS

The following browsers are supported:

  • IE 11+ (only in the Knowledge SDK version with polyfills)
  • Last version of self-updating browsers:
    • Chrome
    • Firefox
    • Safari
    • Edge