Developer

iFrame

The simplest way to embed a Trulience avatar in your website

Adding an Avatar iFrame to a Web Page

Trulience avatars can be embedded in a web page using an iFrame:

<iframe 
    height="600px"
    src="https://www.trulience.com/avatar/<your-avatar-id>"
    allow="camera; microphone; fullscreen; accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
    frameborder="0" 
    allowfullscreen>
</iframe>

Here’s how this embed code looks in action:

Authentication

By default, newly created avatars are public. To restrict access to your avatar, you can make it private and require a JWT token for authentication.

To set up private avatars:

  1. Make your avatar private in the dashboard (Avatar Creator → Edit → ADVANCED tab → Privacy dropdown)
  2. Create an API key with the “Generate Tokens” permission
  3. Generate tokens from your backend using the /auth/generate-token endpoint

See the Security documentation for full setup instructions.

Once you have a token, append it to the iFrame’s src attribute:

<iframe src="https://www.trulience.com/avatar/<your-avatar-id>?token=<your-token>"></iframe>

If your avatar is public, you can omit the token from the URL.

Configuring the UI

You will notice that the avatar embed comes with some inbuilt UI. You can configure this UI by appending query parameters to the avatar URL or by editing your avatars’ client JSON configuration via the dashboard.

Configuration Parameters

The following parameters can be used to configure the behaviour of the iframe:

ParameterDescription
connectIf set to true, the client will connect automatically when visiting the avatar link - bypassing the ‘dial’ screen
micOffIf set to true, the microphone will be muted by default
speakerOffIf set to true, the speaker will be muted by default
hideFSIf set to true, hides the full screen button
hideChatInputIf set to true, hides the chat input box
hideChatHistoryIf set to true, hides the chat history
hideLetsChatBtnIf set to true, hides the dial button
hideMicButtonIf set to true, hides the microphone button preventing the users from being able to mute/unmute
hideHangUpButtonIf set to true, hides the hang up button
hideSpeakerButtonIf set to true, hides the speaker button
dialButtonTextText that appears on the dial button
msgOnConnectMessage (string) sent to the avatar on connection
screenAspectRatioSets the aspect ratio of the visible video area e.g. 16:9, 4:3, 1:1 etc.
chatInputBoxWidthCan be set to the width of the visible video area above, using window
showLogoIf set to true, the client will show either the default Trulience logo or the provided logoSrc
logoPositionCan be right or left
logoSrcThe https location of the logo image to be displayed in top right or left of visible video window
registerTrlEventsList of events that should be notified to iframe’s parent as and when they occur
fullscreenIf set to true, the avatar will be displayed in fullscreen mode
disableDraggingIf set to true, dragging to resize the avatar will be disabled
tokenProvide a JWT token to authenticate access to private avatars. See Security for setup.
controlButtonPositionCan be center, right or left
hideToastIf set to true, the client will not display toast alerts

Adjusting Colour

Here’s a list of colour attributes you can adjust using CSS colour strings, e.g. #ffffff or pink:

  • dialPageBackground
  • dialButtonTextColor
  • dialButtonBackground
  • chatScreenBGColor
  • userChatBubbleBGColor
  • avatarChatBubbleBGColor
  • userChatBubbleBorderColor
  • avatarChatBubbleBorderColor
  • userChatBubbleTextColor
  • avatarChatBubbleTextColor
  • inputBoxBGColor
  • inputBoxBorderColor
  • inputBoxTextColor
  • sendButtonBGColor
  • sendButtonArrowColor
  • sendButtonBorderColor
  • borderColorBetweenInputAndScreen
  • overlayButtonColor
  • loadingBarColor

Example

Here’s an example, demonstrating styling using query params:

<iframe 
    height="700px" 
    allow="camera; microphone; fullscreen" 
    src="https://www.trulience.com/avatar/<id>?chatScreenBGColor=#e7e7e7&userChatBubbleBGColor=#ff6200&avatarChatBubbleBGColor=#000000&userChatBubbleBorderColor=none&avatarChatBubbleBorderColor=none&userChatBubbleTextColor=white&avatarChatBubbleTextColor=white&inputBoxBGColor=#fff&inputBoxBorderColor=#fff&inputBoxTextColor=inherit&sendButtonBGColor=white&sendButtonArrowColor=#000000&sendButtonBorderColor=none&borderColorBetweenInputAndScreen=#f0f0f0" 
    frameborder="0"
    allow="camera; microphone; fullscreen; accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
    allowfullscreen>
</iframe>

iFrame messaging

Behind the scenes, the iFrame embed is loading the Trulience SDK. You can interact with the SDK through iFrame messaging.

Listening to SDK Events

You can register which SDK events you want to listen to by appending them to your iFrame’s src attribute via a query param, for example:

<iframe src="https://www.trulience.com/avatar/<id>?registerTrlEvents=auth-success,auth-fail,mic-update,speech-recognition-start,speech-recognition-end,speech-recognition-final-transcript" />

Extra iFrame Events

On top of the existing SDK events, the iframe also dispatches the following events:

Event NameDescriptionParams
trl-chatFired when a chat message is received from the server.{message, agentName, sttResponse, senderType}
trl-respond-avatar-photo-urlFired in response to the trl-request-avatar-photo-url request from the iframe’s parent.URL pointing to the photo of the avatar.

Please refer to our SDK documentation to find out more about events like auth-success.

Example

You can listen to iFrame events like this:

window.addEventListener("message", (event) => {
    
    // Verify event.origin as necessary.
    if (event.origin) {
        // Test the origin here and return if it does not match the expected value.
    }

    if (event.data !== null && event.data !== undefined) {
        let eventData = event.data;
        console.log("Event name = " + eventData.name + " | Event parameters = " + eventData.params);
    }
}, false);

Sending Events

The iframe listens and reacts to the following events:

Message NameDescriptionParams
trl-request-avatar-photo-urlRequest for a link to the avatar photo that is set in the avatar configN/A
trl-unregister-eventsSend this to unregister for notifications of various events registered in the iframe Source URL.Comma-separated list of event names to unregister, ideally matching those registered.
trl-chatSend a message for Trulience to process and make the avatar speak the responseText to be processed by Trulience.
trl-mic-statusSend this message to mute/unmute the mictrue to unmute, false to mute the mic.
trl-set-speaker-statusSend this message to mute/unmute the speakertrue to unmute, false to mute the speaker.
start-callStart the session with the avatar. Optionally pass a JWT token for private avatars.token (optional): JWT token for authentication
end-callMessage to terminate the ongoing session with the avatarN/A

Example

You can define functions to send iFrame events like this:

const sendChatMessage = (message) => {
    const dataToSend = { "command" : "trl-chat", "message" : message};
    const iframe = document.getElementById('iframeId');
    iframe.contentWindow.postMessage(dataToSend, "*");
}

Start a call with a JWT token (for private avatars):

const iframe = document.getElementById('iframeId');
iframe.contentWindow.postMessage({ command: 'start-call', token: jwt }, '*');

End a call:

const iframe = document.getElementById('iframeId');
iframe.contentWindow.postMessage({ command: 'end-call' }, '*');