Truework.js library reference

The Truework.js JavaScript library allows Truework Direct to be embedded directly into your frontend application.

Truework.credentials.init(config)

Initializes the Truework Direct widget. Accepts one required argument, a config object. Returns a Truework.credentials object

1<head>
2 <script src="https://js.truework.com/v1"></script>
3</head>
4...
5<script>
6 const { token } = await getToken(); // depends on implementation
7
8 var credentials = Truework.credentials.init({
9 publishableKey: 'tw_pk_gJbHD7tEwWUoZ8H_KQWKn4rEESHeNhabiosvcij9i0Q', // example key
10 sessionToken: token,
11 env: Truework.credentials.Env.Production
12 });
13</script>

config object

publishableKey
stringRequired

Publishable key that provides the configuration of this instance of the Truework Direct widget.

sessionToken
stringRequired

Applicant-specific token referencing the Truework Direct session created using the backend API.

env
string enumDefaults to Truework.credentials.Env.production

Environment the session should be initialized in, one of:

ValueEnum object propertyDescription
productionTruework.credentials.Env.productionFor production publishable keys (tw_pk_), and session tokens created by api.truework.com
sandboxTruework.credentials.Env.sandboxFor test publishable keys (tw_pk_test_), and session tokens created by api.truework-sandbox.com

Truework.credentials object

Represents an initialized instance of the Truework Direct widget. This object has a number of methods to control the widget and subscribe to widget events.

1var credentials = Truework.credentials.init(...);
2//...
3credentials.onOpen(function () { console.log("The widget is open!"); })
4credentials.onClose(function () { console.log("The widget is closed."); })
5credentials.onSuccess(function () { console.log("The widget succeeded!"); })
6credentials.onError(function (e) { console.log("The widget encountered an error.", e); })
7
8credentials.open();
9// some other application logic here...
10credentials.close()
Truework.credentials.open()

Opens the widget modal window over the current page.

Truework.credentials.close()

Closes the widget modal window.

Truework.credentials.onOpen(fn)

Registers a callback function to be called after the modal window is opened.

Truework.credentials.onClose(fn)

Registers a callback function to be called after the modal window is closed.

Truework.credentials.onSuccess(fn)

Registers a callback function to be called after valid user credentials have successfully been collected. This does not necessarily mean the verification has completed.

Truework.credentials.onError(fn)

Registers a callback function that accepts an ErrorCode enum value, to be called after an error occurs.

Error handling

Truework.credentials.onError callbacks will be called when the following events occur:

  • Any API errors coming from our API
  • Any handled/unhandled errors from our credentials partners
  • Any Truework.js internal errors (e.g. session creation, react errors, etc)

There are two types of Truework Direct errors:

  • Critical errors: where the widget closes, and then the onError callbacks are called.
  • Errors: where the widget remains open while the onError callbacks are called.

The ErrorCode enum value passed to the callback obscures the root cause and details of the error (preventing PII exposure) while enabling integrators to handle errors as gracefully as possible.

ErrorCode
integer enum
ValueEnum object propertyDescription
0Truework.credentials.ErrorCode.CriticalCritical error
1Truework.credentials.ErrorCode.ErrorError

For example:

1credentials.onError(function (e) {
2 if (e.code === Truework.credentials.ErrorCode.Critical) {
3 console.log("A critical Truework Direct error occurred");
4 // do something
5 }
6});

Browser support

We do our best to support all recent versions of major browsers. For the sake of security and providing the best experience to the majority of customers, we do not support browsers that are no longer receiving security updates and represent a small minority of traffic.

  • We support the latest three versions of the following desktop and mobile browsers: Chrome, Firefox, Safari, and Edge.
  • Internet Explorer 11 (IE11) support ended on May 15, 2022.

Visit our Help Center for more information.