Setup web SDK
The SensibillInstance represents a single Sensibill Instance. It creates an iframe with the Sensibill UI loaded. The Instance consists of a logic layer to control the iframe and a reference to the iframe.
Warning
The SensibillInstance should not be created or destroyed manually. It is meant to be managed through the SDK.
Minimum setup
This will initialize the SDK with the minimum possible options.
const instance = await SensibillSDK.create(
document.getElementById('your-element'),
{ clientID: 'your-client-id'},
{ authentication: { accessToken: 'a-valid-access-token' } }
);
For full documentation on how to create instances, view the Spend Manager Quickstart .
Using the instance
After creation, the instance add an iframe to your page with the Sensibill UI loaded inside.
SensibillSDK.create
returns a promise. Use await to ensure instance
promise is resolved before referencing instance
later.
The instance provides methods to send events into the iframe, and methods to register listeners for events sent from the iframe.
Receiving events
const myEventLogger = (event) => console.log("Received an event!", event);
instance.registerListener(myEventLogger);
Once registered, any events coming from this instance will call your listener.
You can also only listen for events of a specific type.
// Only log set token events
instance.registerListener(myEventLogger, GetNewTokenEvent.type);
When you would like to stop listening to an event, remember to unregister your listener.
instance.unregisterListener(myEventLogger)
Example - managing authentication
This pattern will allow you to pass new access tokens into your instance when the old one is no longer valid.
async function refreshAuthentication() {
// Do what you need to get your new token
const myNewToken = await getNewToken();
// Once you have it, set the new token on the instance
instance.setToken(myNewToken);
}
// Run the refreshAuthentication function whenever the instance asks for a new token
instance.registerListener(refreshAuthentication, GetNewTokenEvent.type)
Instance members
id
A randomly generated or user supplied ID to uniquely identify this instance. The ID is also present in the DOM as the id
attribute on the iframe in the formatsensibill-instance-ID
element
Points to the iframe element
configuration
A record of the configuration object that was sent to the SPA during initialization
Instance methods
registerListener
Register a function to be called whenever an event is sent from instance. If the eventType is not set, all events will call the listener.
unregisterListener
Removes a listener from the instance. If the handler is listening to multiple eventTypes, it will be removed from all.
setToken
Send a new access token into the instance.