RWS Logo
Show / Hide Table of Contents

Setup

The following sections demonstrate how to setup the Language Weaver Client before using the product capabilities.

Setting API Credentials

In order to be able to use the SDK, you need a valid set of Language Weaver API credentials. There are two ways to define the API credentials that will be used by the SDK for authentication:

1. Explicitly Specifying Credentials

An instance of CredentialsConfiguration must be created, that receives the client identifier and the client secret as constructor arguments.

const credentialsConfiguration = new CredentialsConfiguration("clientId", "clientSecret");

2. Using Environment Variables

The following environment variables must be set your environment file in order to be used by the SDK:
LW_CLIENT_ID - represents the client identifier
LW_CLIENT_SECRET - represents the client secret
These will be automatically loaded when the LanguageWeaverClient is built.

Note

LW_CLIENT_SECRET is Cloud specific. Edge uses only LW_CLIENT_ID.
See here how the values can be obtained for each product.

Setting ClientConfiguration

Before creating the LanguageWeaverClient, a ClientConfiguration must be defined. This object holds the connection information: CredentialsConfiguration and RetryConfiguration.
Use the RetryConfiguration to customize the retry mechanism in case of failed requests.

const retryConfiguration = new RetryConfiguration();
retryConfiguration.attempts = 3;
retryConfiguration.delay = 5000; // milliseconds
const clientConfiguration = new ClientConfiguration();
clientConfiguration.credentialsConfiguration = credentialsConfiguration;
clientConfiguration.retryConfiguration = retryConfiguration;

If CredentialsConfiguration or RetryConfiguration are not specified, the default values will be used:

  • Credentials will be automatically loaded from environment variables when the LanguageWeaverClient is built
  • In case of failures, the requests are retried 3 times, with a delay of 5 seconds.

Besides the CredentialsConfiguration and RetryConfiguration, the product can also be specified on the ClientConfiguration level.

const retryConfiguration = new RetryConfiguration();
retryConfiguration.attempts = 3;
retryConfiguration.delay = 5000; // milliseconds
const clientConfiguration = new ClientConfiguration();
clientConfiguration.credentialsConfiguration = credentialsConfiguration;
clientConfiguration.retryConfiguration = retryConfiguration;
clientConfiguration.product = Product.CLOUD;

The product can also be defined using environment variables. This offers the possibility to easily switch between products, with no code changes as described here.

Setting TokenConfiguration

Before creating the LanguageWeaverClient, a TokenConfiguration must be defined. This object holds the connection information: Token.

The Token has the following attributes:

Name Type Description
accessToken string Valid access token in JSON Web Token (JWT) format
validityInSeconds number The time the token is valid
tokenType string The type of the token (e.g. Bearer, Basic)
expiresAt number A timestamp (epoch) representing the expiration time of the token
const token = new Token("accessToken", 86400, "tokenType", 1663409635000);
const tokenConfiguration = new TokenConfiguration();
tokenConfiguration.token = token;

Besides the token, the product can also be specified on the TokenConfiguration level.

const token = new Token("accessToken", 86400, "tokenType", 1663409635000);
const tokenConfiguration = new TokenConfiguration();
tokenConfiguration.token = token;
tokenConfiguration.product = Product.CLOUD;

The product can also be defined using environment variables. This offers the possibility to easily switch between products, with no code changes as described here.

Setting API URL

For Cloud product, the URL cannot be overridden. The API is by default set to the Europe region using https://api.languageweaver.com URL. The connection region can be overridden when using a CloudLanguageWeaverClient or by defining the LW_CLOUD_REGION environment variable with "US" value. For Europe, the environment variable value must be set to "EUROPE".

For Edge product, by default, the URL is set to http://localhost:8001. This can be overridden using one of the following methods:

  • Set the LW_EDGE_OVERWRITE_URL environment variable with the needed URL.
  • Using a EdgeLanguageWeaverClient and setting the overwriteUrl on the client object.

Building the LanguageWeaverClient

The LanguageWeaverClient is the core object that is used for all interactions with the product. This object requires the previously created ClientConfiguration or TokenConfiguration.

const languageWeaverClient = await SdkFactory.getLanguageWeaverClient(clientConfiguration);
// perform requests
const languageWeaverClient = await SdkFactory.getLanguageWeaverClient(tokenConfiguration);
// perform requests

By default, the used product is Cloud with the URL set to the Europe region.

Switch between products

Switching from default Cloud product to Edge product, can be done in two ways:

  • By defining an environment variable, LW_PRODUCT with "EDGE" value. In this way, without code changes, you can easily switch between implementations. For Cloud, the variable's value must be set to "CLOUD".
  • By setting the product directly on the ClientConfiguration object or on the TokenConfiguration object.
const clientConfiguration = new ClientConfiguration();
clientConfiguration.product = Product.CLOUD;
const tokenConfiguration = new TokenConfiguration();
tokenConfiguration.product = Product.CLOUD;

Product Specific LanguageWeaverClient

More customized operations that are product specific can be done by creating a CloudLanguageWeaverClient or EdgeLanguageWeaverClient.

In this page
Back to top