Basic Usage

Prev Next

Basic Usage focuses on how to use the SDK once it’s installed. Below, find example code for importing the client, creating an instance, and calling service methods like search or recommend.

More detailed API docs and copy-paste examples can be found in the API documentation for each service.

Please also reference our XSearch Service, XRecommend Service, Tracking Service section for more examples.

import XGenClient from '@xgenai/sdk-core';

const xg = new XGenClient({
        /* Customer Information */
        key: '<your_api_key>',
        secret: '<your_api_secret>',
        clientId: '<your_client_id>',
        trackerId: '<your_tracker_id>',

        /* Env Functions */
        getEnv: () => 'stage', //optional: function that returns the environment (`stage` or `prod`) - This overrides the env globally
        getTrackerEnv: () => 'stage', //optional: function that returns the tracker env. Can override independently
        getRecommendationEnv: () => 'stage', //optional: function that returns the recommendation env. Can override independently
        getSearchEnv: () => 'stage', //optional: function that returns the search env. Can override independently

        /* custom Auth store */
        authStore: new LocalCookieAuthStore(), //default - can pass in a custom auth store instance
  
         /* Tracking Information */
        locale: () => "", // returns the locale string for event tracking and will be used for all events.
        group: () => "" // returns the current group for tracking to track AB tests
});

// get recommendations by element id
const result = await xg.recommend.getResultsById({elementId: '<element_id>'})

// and much more...