Skip to main content
All CollectionsAssistants
Setup automatic Identity Verification and auto-set Variables using HMAC
Setup automatic Identity Verification and auto-set Variables using HMAC

Personalize user interactions by auto-setting custom variables, making your AI Assistant smarter and more responsive

Alvaro Vargas avatar
Written by Alvaro Vargas
Updated this week

This guide will walk you through how to securely set up Frontline’s AI Assistant with system and custom variables using HMAC (Hash-based Message Authentication Code). HMAC ensures that the variables you send to the AI Assistant are authenticated and secure.

Step 1: Generate the HMAC with System or Custom Variables

First, you need to generate the HMAC hash using the system or custom variables you want to send.

Important: Before proceeding, make sure you have the `crypto` module installed. If you're using Node.js and haven't installed it yet, run the following command:

npm install crypto

Example in Node.js

const crypto = require('crypto');

// Your AI Assistant's secret key
const secretKey = '[YOUR_ASSISTANT_SECRET_KEY]';

// Custom variables you want to send
const variables = {
userId: 'u-123123',
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
// ... add other custom variables here
}

// Generate the HMAC hash
const hash = crypto.createHmac('sha256', secretKey)
.update(JSON.stringify(variables))
.digest('hex');

console.log('Generated HMAC:', hash);

In this script:

• Replace [YOUR_ASSISTANT_SECRET_KEY] with your actual AI Assistant secret key. You can find your secret key by heading to Assistant > Settings > Deployment and generating a secret key.

• The variables object contains the system or custom variables (such as userId, firstName, etc.) that you want to send to the AI Assistant.

• The HMAC is generated using the sha256 algorithm, and the result is a hexadecimal string that will be used in the next steps.

Step 2: Send the System or Custom Variables and HMAC

Once you've generated the HMAC, you can send the system or custom variables to the AI Assistant. There are two methods to do this.

Method 1: Using the Widget (For Widget Integrations Only)

Note: This method is only available if you have integrated the AI Assistant as a widget on your website. If you're not using the widget integration, please skip to Method 2.

Example Script for the Widget

<script>
setTimeout(() => {
window.frontlineFunctions.setCustomVariables({
userId: 'u-123123123',
firstName: 'John',
lastName: 'Doe',
email: '[email protected]'
}, '9a2b96dd470f287e8f0f22b697bc31c6f36daacb2568c1a3f64abe28488611e3'); // Your generated HMAC here
}, 50);
</script>

In this script:

• The first parameter is an object containing the system or custom variables that you want to pass to the AI Assistant.

• The second parameter is the HMAC hash generated in the previous step.

• Make sure to replace the hardcoded values with your actual variables and the corresponding HMAC.

Method 2: Send System or Custom Variables via URL with Query Parameters

This method can be used regardless of how you've integrated the AI Assistant. It's especially useful for non-widget integrations or when you want to provide a direct link to the AI Assistant with pre-set variables.

Before generating the HMAC for this method, you need to create a JSON string with all the properties you're going to send as query parameters. This JSON string should not contain any spaces. For example:

JSON

{"first_name":"John","last_name":"Doe","userId":"u123123","email":"[email protected]"}

Use this JSON string to generate the HMAC as described in Step 1.

Example URL with Query Parameters

https://assistant.getfrontline.ai/[ASSISTANT-ID]?first_name=John&last_name=Doe&userId=u123123&hmac=9a2b96dd470f287e8f0f22b697bc31c6f36daacb2568c1a3f64abe28488611e3

In this URL:

• Replace [ASSISTANT-ID] with your actual AI Assistant ID.

• Add your system or custom variables (like first_name, last_name, userId) as query parameters.

• Append the HMAC hash (hmac) at the end of the URL. This HMAC should be generated using the JSON string that includes all the variables you're passing.

Remember that the order of the parameters in the URL should match the order in which they were included in the JSON string used to generate the HMAC.

Summary

To securely set up Frontline’s AI Assistant with system or custom variables:

1. Generate the HMAC: Use your AI Assistant’s secret key and the system or custom variables.

2. Send via Widget: Pass the system or custom variables and HMAC hash using the setCustomVariables function in the widget.

3. Send via URL: Alternatively, send the variables and HMAC as query parameters in a URL.

This ensures that the data passed to the AI Assistant is secure and validated.

Did this answer your question?