Authenticate your users in HubSpot when they login in your app /
Integrating HubSpot's Visitor Identification API: A Step-by-Step Guide


HubSpot's Visitor Identification API provides a powerful way to identify and authenticate visitors to your website using your own external authentication system. This integration allows for more personalized interactions and access to previous chat history across devices. In this article, we'll walk you through the process of integrating this feature into your web application.


In the upcoming video, we’ll start by explaining the fundamental concept behind the Visitor Identification API. Then, we’ll walk you through the code that brings this concept to life. Whether you’re an experienced developer or just getting started, this video will provide valuable insights into leveraging the full potential of HubSpot’s Visitor Identification API

The visitor identification API doc



Before you get started, ensure that you meet the following prerequisites:

Integration Steps

1. User Authentication

Begin by allowing users to log in with your current login form on your website. Ensure that your authentication system is in place.

As an example, the auth form could look like this :

This HTML form with the id « login » contains an email input field where users can enter their email address. When they click the « Send » button, the form will be submitted.

<form id="login"> <input type="email" id="email" > <input type="password" id="password" > <button type="submit"> Login </button> </form>

2. Generate an Identification Token

To generate an identification token, you will need to use the Visitor Identification API. This should be done on the backend of your web application. You can pass in the email address of the authenticated visitor.

Here’s the router which handldes the POST request to login the user :"/login", async function(request, reply) {

  const user = {

  const authResult = await hubSpotAPI.authVisitor(;




As you can see I call a function I created :

 const authResult = await hubSpotAPI.authVisitor(;

The function looks like this :

In this function I call ( POST ) the HubSpot API on the following endpoint

curl --request POST \
  --url \
  --header 'authorization: Bearer YOUR_ACCESS_TOKEN' \
  --header 'content-type: application/json' \
  --data '{
  "email": "",
  "firstName": "Gob",
  "lastName": "Bluth"

NB : you need to use your private app token to make this call.

exports.authVisitor = async (email) => {

    if (!email) throw new Error('you need to set an email ');

    const url = '';

    const postData = {

    const response = await, postData, axiosConfig).catch(axiosErrorHandler)

    if(!response) throw new Error(`API didn't respond...`)

    if(! throw new Error(`API didn't respond with data ...`)

    if(! throw new Error(`API didn't respond with a token ...`)



Once I get the token I return the token to my controller, to pass it to the front-end.

3. Set Properties in hsConversationsSettings

Using the token generated in Step 2, you should set specific properties on the hsConversationsSettings object on the window. These properties include the identificationEmail and identificationToken. Here’s an example:

window.hsConversationsSettings = {
   identificationEmail: "",
   identificationToken: "<TOKEN FROM STEP 2>"

My JavaScript code executed for the login and the auth with HubSpot looks like this

document.querySelector('#login')?.addEventListener("submit", async (event) => {


    const emailInput = document.querySelector('#email');

      try {

            const response = await fetch("/login",  {
                method: 'POST',
                headers: {
                'Content-Type': 'application/json',
                body: JSON.stringify({
                    email : emailInput.value

            if (!response.ok) throw new Error(`Request failed with status: ${response.status}`);

            const data = await response.json();

            if(!data.token) throw new Error(`No token received by the backend`);

            window.hsConversationsSettings = {
                identificationEmail: emailInput.value,
                identificationToken: data.token



    } catch (error) {

            console.error('An error occurred:', error);


The important part is :

In this part, I use the token to set the right cookie in the user browser. Thanks to this cookie the user will not have to put his email address in the forms / chat

window.hsConversationsSettings = {
    identificationEmail: emailInput.value,
    identificationToken: data.token