Identity verification proves that a signed-in visitor was identified by your server before Kommander.ai enables customer-specific tools. Use it for order lookups, account data, invoices, tickets, subscriptions, or any integration that depends on who the visitor is.

Without HMAC verification, browser fields are only claims. Visitors can edit JavaScript, request bodies, storage, and chat messages. Kommander.ai treats signed identity as trusted server context and ignores model-supplied identity arguments when calling customer-data tools.

Signing Flow

Generate signatures only on trusted server infrastructure after your app authenticates the visitor.

  1. Build a small JSON payload with userId, userHash, optional email, and optional metadata.
  2. Serialize the payload consistently.
  3. Add a Unix millisecond timestamp for signed-envelope integrations.
  4. Sign the exact string timestamp.payload with HMAC-SHA256 and your widget identity secret.
  5. Send the signed identity to the widget or widget API request.
{ "payload": "{\"userId\":\"user_123\",\"userHash\":\"cust_456\",\"email\":\"customer@example.com\",\"metadata\":{\"plan\":\"pro\"}}", "timestamp": 1779026400000, "signature": "GENERATED_HMAC_SIGNATURE", "signatureEncoding": "hex" }

Hex is recommended for new integrations. base64 and base64url are also accepted when signatureEncoding is sent.

Hosted Widget Format

The hosted widget accepts a compact identity object. The userHash value can be a signed payload token in the format base64url(payload).signature, where signature is the HMAC-SHA256 of the base64url payload.

The payload must include timestamp (Unix milliseconds) or iat (Unix seconds). Tokens without one are rejected, as are tokens older than one hour or more than five minutes in the future, so render a fresh token on each page load.

<script> window.initKommanderChatbot({ userId: "YOUR_KOMMANDER_USER_ID", identity: { id: "user_123", email: "customer@example.com", userHash: "BASE64URL_PAYLOAD.HMAC_SIGNATURE" } }); </script>

For direct widget API calls, send the full signed envelope as identityPayload, identityTimestamp, identitySignature, and identitySignatureEncoding, or as matching fields under identity.

Server Examples

All examples below create the signed-envelope shape. To create the compact hosted-widget token, base64url-encode the same payload and sign that encoded payload.

Node.js

Canonical HMAC Snippets

The snippets below are the canonical examples from the project specification. They show the core HMAC operation for the compact identity hash format and must be generated server-side.

import crypto from 'node:crypto'; // Server-only secret from Kommander → Settings → Identity Verification. const KOMMANDER_IDENTITY_SECRET = process.env.KOMMANDER_IDENTITY_SECRET!; export function signKommanderIdentity(userId: string) { return crypto .createHmac('sha256', KOMMANDER_IDENTITY_SECRET) .update(userId) .digest('hex'); } // In your page handler, pass to the client: const identityHash = signKommanderIdentity(session.user.id);
<script> window.Kommander = window.Kommander || {q:[]}; Kommander.config = { clientId: "YOUR_CLIENT_ID", user: { id: "{{session.user.id}}", email: "{{session.user.email}}", identityHash: "{{identityHash}}" // generated above } }; </script>
import hmac, hashlib, os SECRET = os.environ["KOMMANDER_IDENTITY_SECRET"].encode() def sign(user_id: str) -> str: return hmac.new(SECRET, user_id.encode(), hashlib.sha256).hexdigest()
$identityHash = hash_hmac('sha256', $userId, getenv('KOMMANDER_IDENTITY_SECRET'));
identity_hash = OpenSSL::HMAC.hexdigest('SHA256', ENV['KOMMANDER_IDENTITY_SECRET'], user_id)
mac := hmac.New(sha256.New, []byte(os.Getenv("KOMMANDER_IDENTITY_SECRET"))) mac.Write([]byte(userID)) identityHash := hex.EncodeToString(mac.Sum(nil))

A rotation has a window where both the old and new secret are valid. The server tries new first, then old. Update your application to emit hashes with the new secret, deploy, then revoke the old one in the Kommander UI.

Payload Fields

FieldRequiredNotes
userIdYesStable ID from your application. Do not use a display name.
userHashYesStable ownership key used by customer-data connectors. Prefer an opaque value when possible.
emailNoInclude only when an upstream tool needs email matching.
metadataNoTenant, account, or plan attributes needed for authorization. Keep it small.
attributesNoAdditional non-secret attributes for connector templates.

Secret Rotation

Rotate the widget identity secret when it may have been exposed, when staff or vendor access changes, and on your normal credential rotation schedule. Kommander.ai accepts the previous secret for 7 days after rotation so already-deployed servers can continue signing while your rollout completes.

During rotation:

  • Deploy code that reads the secret from a server-side secret store.
  • Regenerate the secret in Kommander.ai.
  • Update every signing service immediately.
  • Monitor verification failures during rollout.
  • Remove the previous secret from all environments before the grace period ends.

Security Best Practices

  • Never expose KOMMANDER_IDENTITY_SECRET in browser code, mobile apps, tag managers, CMS templates, or model-visible prompts.
  • Generate signatures on demand from your backend.
  • Keep payloads small and avoid access tokens, secrets, or unnecessary PII.
  • Verify your server clock is synchronized before using timestamped signed envelopes.
  • Treat verification failures as security events in logs, but show visitors normal anonymous chat behavior when possible.
  • Reissue identity when a user signs in, signs out, switches account, or changes organization.

Error Reference

Error reasonMeaningFix
missing_secretKommander.ai could not load an identity secret for the widget client.Confirm the correct widget client is configured.
invalid_payloadThe signed payload is not valid JSON or is missing required fields.Send a JSON object containing non-empty userId and userHash.
invalid_signatureThe HMAC does not match the exact signed string.Check the secret, payload bytes, timestamp, and encoding.
invalid_timestampThe timestamp is missing or cannot be parsed.Send a Unix millisecond timestamp, Unix second timestamp, or ISO timestamp.
timestamp_too_oldThe signed envelope is outside the freshness window.Generate the identity at request or render time.
timestamp_in_futureThe timestamp is too far ahead of Kommander.ai server time.Check clock sync and avoid browser-generated timestamps.
previous_secret_expiredThe signature matched the previous secret after the grace period.Deploy the current secret everywhere.

Troubleshooting

  • If verification always fails, log the exact payload string, timestamp, encoding, and a redacted signature prefix. Do not log the secret.
  • If only some users fail, confirm every payload includes non-empty userId and userHash strings.
  • If failures start after deployment, compare JSON serialization between old and new services.
  • If failures start after rotation, check that all app servers use the new secret.
  • If customer-data tools are missing but chat still works, verification likely failed or the verified payload did not include userHash.