Typescript SDK

The eGain API client SDK for TypeScript supports both Node.js and browser environments, offering a modern, type-safe interface for integrating with eGain’s Knowledge Portal Manager, AI Services, and Ingestion APIs.

License

The following licenses are required to use the SDK:

  • If the user is an agent, then the Knowledge + AI license is required.
  • If the user is a customer, the Self-Service and Advanced Self-Service licenses must be available.

API Resource Limits

The following Resources have predefined limits for specific access attributes for Enterprise use.

ResourceAttributeEnterprise
Article Reference LimitsNumber of attachments used in any article50
Number of custom attributes in an article15
Number of publish views used in an article version20
Topic Reference LimitsUser-defined topics in a department50000
Depth of topics20
Topics at any level2500
Number of custom attributes in a topic15
Portal Reference LimitsTag categories in a portal15
Topics to be included in a portal2500
Number of articles to display in announcements25
Maximum related articles in portal setting100
Usage links and link groups setup for a portal25

SDK Example Usage

Portal API

Copy
Copied
import { Egain } from "@egain/egain-api-typescript";

const egain = new Egain({
  accessToken: process.env["EGAIN_ACCESS_TOKEN"] ?? "",
});

async function run() {
  const result = await egain.portal.article.getArticleById({
    acceptLanguage: "en-US",
    portalID: "PROD-1000",
    articleID: "PROD-2996",
    langauge: "en-US",
    articleAdditionalAttributes: [
      "averageRating",
    ],
    customAdditionalAttributes: "country_name",
    publishViewId: "PROD-3203",
    workflowMilestone: "publish",
  });

  console.log(result);
}

run();

Retrieve API

Copy
Copied
import { Egain } from "@egain/egain-api-typescript";

const egain = new Egain({
  accessToken: process.env["EGAIN_ACCESS_TOKEN"] ?? "",
});

async function run() {
  const result = await egain.aiservices.retrieve.retrieveChunks({
    q: "fair lending",
    portalID: "PROD-1000",
    dollarFilterUserProfileID: "PROD-3210",
    language: "en-US",
    dollarFilterTags: {
      "PROD-1234": [
        "PROD-2000",
        "PROD-2003",
      ],
      "PROD-2005": [
        "PROD-2007",
      ],
    },
    retrieveRequest: {
      channel: {
        name: "Eight Bank Website",
      },
    },
  });

  console.log(result);
}

run();

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

NameTypeSchemeEnvironment Variable
accessTokenhttpHTTP BearerEGAIN_ACCESS_TOKEN

To authenticate with the API the accessToken parameter must be set when initializing the SDK client instance. For example:

Copy
Copied
import { Egain } from "@egain/egain-api-typescript";

const egain = new Egain({
  accessToken: process.env["EGAIN_ACCESS_TOKEN"] ?? "",
});

async function run() {
  const result = await egain.portal.article.getArticleById({
    acceptLanguage: "en-US",
    portalID: "PROD-1000",
    articleID: "PROD-2996",
    langauge: "en-US",
    articleAdditionalAttributes: [
      "averageRating",
    ],
    customAdditionalAttributes: "country_name",
    publishViewId: "PROD-3203",
    workflowMilestone: "publish",
  });

  console.log(result);
}

run();

Error Handling

EgainError is the base class for all HTTP error responses. It has the following properties:

PropertyTypeDescription
error.messagestringError message
error.statusCodenumberHTTP response status code eg 404
error.headersHeadersHTTP response headers
error.bodystringHTTP body. Can be empty string if no body is returned.
error.rawResponseResponseRaw HTTP response
error.data$Optional. Some errors may contain structured data. See Error Classes.

Example

Copy
Copied
import { Egain } from "@egain/egain-api-typescript";
import * as errors from "egain-api-client/models/errors";

const egain = new Egain({
  accessToken: process.env["EGAIN_ACCESS_TOKEN"] ?? "",
});

async function run() {
  try {
    const result = await egain.portal.article.getArticleById({
      acceptLanguage: "en-US",
      portalID: "PROD-1000",
      articleID: "PROD-2996",
      langauge: "en-US",
      articleAdditionalAttributes: [
        "averageRating",
      ],
      customAdditionalAttributes: "country_name",
      publishViewId: "PROD-3203",
      workflowMilestone: "publish",
    });

    console.log(result);
  } catch (error) {
    // The base class for HTTP error responses
    if (error instanceof errors.EgainError) {
      console.log(error.message);
      console.log(error.statusCode);
      console.log(error.body);
      console.log(error.headers);

      // Depending on the method different errors may be thrown
      if (error instanceof errors.WSErrorCommon) {
        console.log(error.data$.code); // string
        console.log(error.data$.developerMessage); // string
        console.log(error.data$.details); // Detail[]
        console.log(error.data$.userMessage); // string
      }
    }
  }
}

run();

Server Selection

Override Server URL Per-Client

The default server can be overridden globally by passing a URL to the serverURL: string optional parameter when initializing the SDK client instance. For example:

Copy
Copied
import { Egain } from "@egain/egain-api-typescript";

const egain = new Egain({
  serverURL: "https://${API_DOMAIN}/knowledge/portalmgr/v4",
  accessToken: process.env["EGAIN_ACCESS_TOKEN"] ?? "",
});

async function run() {
  const result = await egain.portal.article.getArticleById({
    acceptLanguage: "en-US",
    portalID: "PROD-1000",
    articleID: "PROD-2996",
    langauge: "en-US",
    articleAdditionalAttributes: [
      "averageRating",
    ],
    customAdditionalAttributes: "country_name",
    publishViewId: "PROD-3203",
    workflowMilestone: "publish",
  });

  console.log(result);
}

run();