> ## Documentation Index
> Fetch the complete documentation index at: https://docs.talqui.chat/llms.txt
> Use this file to discover all available pages before exploring further.

# Identify users

> Tell the chat who your logged-in user is, keep their history across devices with a userHash, and forget them on logout.

## Anonymous or identified

| | Anonymous (default) | Identified with `userHash` |
| - | - | - |
| Who the visitor is | A random ID created by the chat on this browser or device | Your user, computed from your own user ID |
| History | Stays on this browser or device | Follows the user to every browser, device and reinstall |
| Pre-chat form | Shown, unless you send the required fields | Skipped when you send the required fields |
| You need | Nothing | A secret on your backend |

You can always send the name, email and phone without a `userHash`. Your team sees who is talking, but the history stays on that device.

## Send who the user is

```js Website theme={null}
$talqui.identify({ userHash, name: "Ana Souza", email: "ana@example.com", phone: "+5511999990000" });
```

```ts React Native theme={null}
await talqui.identify({ userHash, name: 'Ana Souza', email: 'ana@example.com', phone: '+5511999990000' });
```

```dart Flutter theme={null}
await talqui.identify(TalquiUser(userHash: userHash, name: 'Ana Souza', email: 'ana@example.com', phone: '+5511999990000'));
```

Call it after login, and again whenever the user changes their details. With the same `userHash`, the call only updates the details.

## Compute the `userHash` on your backend

```text theme={null}
userHash = HMAC_SHA256(secret, "<tenantId>:<userId>")   →  lowercase hex, 64 characters
```

* `secret`: a random string of your own, at least 32 characters, kept on your server (an environment variable). Talqui never sees it.
* `tenantId`: your [tenant ID](https://docs.talqui.chat/guides/chat-sdks/get-your-ids.md).
* `userId`: your user's ID in your system, the same one every time.

Return the hash to your app or page in the response you already send after login.

```js Node.js theme={null}
import { createHmac } from 'node:crypto';

const userHash = createHmac('sha256', process.env.TALQUI_IDENTITY_SECRET)
  .update(`${TENANT_ID}:${user.id}`)
  .digest('hex');
```

```python Python theme={null}
import hashlib, hmac, os

user_hash = hmac.new(
    os.environ["TALQUI_IDENTITY_SECRET"].encode(),
    f"{TENANT_ID}:{user.id}".encode(),
    hashlib.sha256,
).hexdigest()
```

```php PHP theme={null}
$userHash = hash_hmac('sha256', TENANT_ID . ':' . $user->id, getenv('TALQUI_IDENTITY_SECRET'));
```

```ruby Ruby theme={null}
require 'openssl'

user_hash = OpenSSL::HMAC.hexdigest('SHA256', ENV['TALQUI_IDENTITY_SECRET'], "#{TENANT_ID}:#{user.id}")
```

```go Go theme={null}
mac := hmac.New(sha256.New, []byte(os.Getenv("TALQUI_IDENTITY_SECRET")))
mac.Write([]byte(tenantID + ":" + userID))
userHash := hex.EncodeToString(mac.Sum(nil))
```

> [!WARNING]
>
> **Never compute the hash in the app or in the browser, and never use the raw user ID as the hash.** Anyone holding a user's `userHash` can open that user's conversation. Treat it like a session token: send it only to the logged-in user it belongs to. The chat rejects values shorter than 32 characters, which catches a raw ID passed by mistake.

> [!WARNING]
>
> **Changing the secret changes every hash.** Each user then arrives as a new visitor and no longer sees their previous conversations. Pick the secret once and keep it.

## Send your own user ID

If you use [webhook notifications](https://docs.talqui.chat/guides/push-notifications/webhook.md), also send your user ID as contact metadata. The webhook returns it, so your backend knows which of your users received the reply:

```js Website theme={null}
$talqui.setContactMeta({ user_id: user.id });
```

```ts React Native theme={null}
talqui.setContactMeta({ user_id: user.id });
```

```dart Flutter theme={null}
talqui.setContactMeta({'user_id': user.id});
```

## Logout

When the user logs out, forget them on this device. Otherwise the next person who logs in sees the previous user's conversation.

```js Website theme={null}
$talqui.reset();
```

```ts React Native theme={null}
await talqui.logout();
```

```dart Flutter theme={null}
await talqui.logout();
```

It clears the identity, the contact details and the metadata, unlinks the device from push notifications and starts a fresh anonymous visitor.

## Good to know

* A conversation started anonymously doesn't move to the identified user. After `identify` with a new `userHash`, the chat shows that user's history.
* Calling `identify` with a different `userHash` on the website switches users on the spot: the previous visitor's conversation is dropped from the page first.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.