Ziyarn Docs

Signed-in Customer Chat

Give a customer one private chat that follows them across devices.

Signed-in customer chat lets a customer who is already logged in on your site keep one private conversation across devices. Ziyarn does not become their login system: your server tells Ziyarn who the customer is, and the widget receives a short-lived pass.

The feature is available on Business, Enterprise, and Custom plans. Anonymous chat keeps working exactly as before.

How it works

  1. Your server authenticates the customer with your own login.
  2. It exchanges your domain's identity key for a 30-minute customer pass.
  3. The widget sends that pass in the Authorization header.
  4. Ziyarn scopes history, sending, escalation, and owner replies to that customer and domain.

The pass contains only ids. Your customer's email and name stay in Ziyarn's end_users table, not in the pass.

Setup

1. Enable the feature

Go to Dashboard → Domains → [Domain] → Settings → Signed-in customers and turn it on. Only workspace owners and admins can change it.

The first time you enable it, Ziyarn shows the identity key once. Copy it now; it cannot be shown again. Store it in your server's secret manager as ZIYARN_IDENTITY_KEY.

The identity key is private. The embed secret is public and is already in your page HTML. Do not confuse the two.

2. Add a server-only token endpoint

Your browser must never call the identity endpoint directly. Add a route on your own server that checks the customer's session and forwards only their stable id:

// app/api/ziyarn-identity/route.ts
import { NextResponse } from "next/server";

import { auth } from "@/lib/auth";

export async function GET() {
  const session = await auth();
  if (!session?.user) {
    return NextResponse.json({ token: null }, { status: 401 });
  }

  const response = await fetch("https://ziyarn.vercel.app/api/chat/identity", {
    method: "POST",
    headers: {
      authorization: `Bearer ${process.env.ZIYARN_IDENTITY_KEY!}`,
      "content-type": "application/json",
    },
    body: JSON.stringify({
      externalId: session.user.id,
      email: session.user.email ?? undefined,
      name: session.user.name ?? undefined,
    }),
    cache: "no-store",
  });

  if (!response.ok) {
    return NextResponse.json(
      { token: null },
      { status: response.status, headers: { "cache-control": "no-store" } },
    );
  }

  const data = (await response.json()) as { token: string; expiresAt: string };
  return NextResponse.json(data, {
    headers: { "cache-control": "private, no-store" },
  });
}

externalId must be your customer's stable user id. Do not use an email address or a value that changes when the customer edits their profile: changing it creates a new Ziyarn customer and loses their history.

3. Pass the token to the widget

The token stays in memory. Never put it in localStorage, a URL, a data-* attribute, or server-rendered HTML.

Vanilla JavaScript

const widget = document.querySelector("zy-widget");

async function refreshIdentity() {
  const response = await fetch("/api/ziyarn-identity", {
    cache: "no-store",
  });
  if (!response.ok) {
    widget.clearIdentity();
    return;
  }
  const { token } = await response.json();
  widget.identify(token);
}

widget.addEventListener("ziyarn:identity-expired", refreshIdentity);
refreshIdentity();

Call widget.clearIdentity() on logout. The widget drops the identified conversation and returns to the anonymous flow.

React

Use the ZyWidget component from this repository, or the ChatWidget surface directly. Pass the token as a prop; it is kept in memory.

"use client";

import { useEffect, useState } from "react";

import { ZyWidget } from "@/components/widget/zy-widget";

export function SupportWidget() {
  const [identityToken, setIdentityToken] = useState<string | null>(null);

  useEffect(() => {
    let cancelled = false;

    async function refresh() {
      const response = await fetch("/api/ziyarn-identity", {
        cache: "no-store",
      });
      if (cancelled) return;
      if (!response.ok) {
        setIdentityToken(null);
        return;
      }
      const data = (await response.json()) as { token: string };
      setIdentityToken(data.token);
    }

    void refresh();
    return () => {
      cancelled = true;
    };
  }, []);

  return (
    <ZyWidget
      slug="YOUR_DOMAIN_SLUG"
      secret="YOUR_EMBED_SECRET"
      identityToken={identityToken}
      onIdentityExpired={() => setIdentityToken(null)}
    />
  );
}

onIdentityExpired fires when the pass has expired or the server rejected it. Fetch a fresh pass and pass it back; the widget resets the visible conversation first, so one customer's messages never linger for the next.

Modern Sleek

Modern Sleek runs in an iframe served by Ziyarn. The script sends the token to the iframe with postMessage; it is never added to the iframe URL. No extra host code is needed beyond the same identify(token) call.

API

POST /api/chat/identity

Server-to-server only. There is no CORS on this endpoint.

Headers:

authorization: Bearer YOUR_IDENTITY_KEY
content-type: application/json

Body:

{
  "externalId": "customer-123",
  "email": "customer@example.com",
  "name": "Sam"
}

externalId is required. email and name are optional; when omitted, Ziyarn keeps the values it already has.

Response:

{
  "token": "…",
  "expiresAt": "2026-03-04T10:30:00.000Z"
}

Passes last 30 minutes. Cache one pass per customer until it is close to expiry; do not cache a single pass globally or share it between customers.

Chat requests

When a pass is present, send it on every chat request:

authorization: Bearer CUSTOMER_PASS
x-embed-secret: YOUR_EMBED_SECRET

With a pass, history is requested with GET /api/chat?history=1 (no conversation id). The widget does this automatically. The response contains the customer's one open conversation, or conversationId: null and an empty messages array when they have none yet.

Errors

StatusCodeMeaning
400INVALID_BODYexternalId is missing or a field is invalid
401INVALID_IDENTITY_KEYThe identity key is missing or wrong
403IDENTIFIED_CHAT_DISABLEDThe feature is off or the plan no longer includes it
429RATE_LIMITEDMore than 300 identity requests per minute for this domain
401IDENTITY_EXPIREDThe customer pass expired; fetch a new one
401INVALID_IDENTITY_TOKENThe pass is malformed, for another domain, or its customer no longer exists
401IDENTITY_REQUIREDhistory=1 was requested without a pass
403IDENTIFIED_CHAT_DISABLEDA pass was sent while the feature is off or paused

The widget dispatches ziyarn:identity-expired for the two expiry cases. It does not silently continue with an expired pass.

Security notes

  • The identity key is private. The embed secret is public. Rotate the identity key from the settings panel if it is exposed; existing passes expire within 30 minutes, and turning the feature off stops new passes immediately.
  • Use a stable externalId. It is the customer's identity in Ziyarn.
  • Do not cache identity tokens in HTML, a CDN, or localStorage.
  • Keep the token out of logs and analytics.
  • If your site sets a CSP, allow the Ziyarn API origin in connect-src. Modern Sleek also needs it in frame-src.
  • The identity endpoint has no CORS by design. A browser cannot call it, even if it somehow learns the key.

Logout, downgrade, and turning it off

  • Logout: call clearIdentity(). The customer's identified messages disappear from the widget and the anonymous flow is available.
  • Feature off: new passes are refused with 403. Existing passes stop working on the next chat request. Conversations and messages are kept.
  • Plan downgrade: the feature is paused the same way. Upgrading restores it; data is not deleted.
  • Anonymous conversations are never merged into an identified chat. A customer who chatted anonymously before signing in gets a separate identified history.

Deleting customer data

Ziyarn stores the customer in end_users and links their conversations through conversations.end_user_id. Deleting the end user cascades to their conversations and messages. Contact support if you need a deletion procedure for a customer request.