Megadesk Documentation

Personalized Chat

Provide logged-in user identity to the chatbot from your server.

The chatbot can personalize answers and call your APIs with user context (email, user id, plan, etc.). Expose that context via a server endpoint on your site and point Megadesk to it.

How it works

  • You add an endpoint on your domain, e.g. /api/megadesk-identity.
  • The endpoint encrypts the identity payload with your shared key and returns { identity: "<encrypted-token>" }.
  • The embed passes that encrypted value to Megadesk; the platform decrypts it using the Encryption Key you find in the developer settings.
  • Inside actions, map fields like userDetail.email to {{userEmail}}.

Payload shape (before encryption)

Use these roots; other keys are ignored:

{
  "userDetail": {
    "email": "user@example.com",
    "fullName": "Ada Lovelace",
    "plan": "pro",
    "id": "user-123"
  },
  "integrations": {
    "stripe": {
      "customerId": "cus_123",
      "subscriptionStatus": "active"
    }
  }
}
  • userDetail.* and integrations.* are the allowed roots for identity paths inside actions.
  • If the visitor is logged out, return { identity: null }.

Example (Next.js 16 route with encryption)

app/api/megadesk-identity/route.js

import { NextResponse } from "next/server";
import { cookies } from "next/headers";
import * as jose from "jose";
// import your auth/session helpers
// import { getUserFromSession } from "@/lib/auth";

export async function GET() {
  const cookieStore = await cookies();
  const sessionToken = cookieStore.get("session")?.value;

  // Replace with your auth lookup
  const user = { email: "user@example.com", fullName: "Ada Lovelace", id: "user-123", plan: "pro" }

  const payload = {
    userDetail: {
      email: user.email,
      fullName: user.fullName,
      plan: user.plan,
      id: user.id,
      subscriptionStatus: "active"
    },
    integrations: {
      stripe: {
        customerId: "cus_123",
      }
    }
  };

  const encryptKey = MEGADESK_IDENTITY_SECRET // same key you copy from Megadesk > Developer > Personalized chats.

  const identity = await new jose.EncryptJWT(payload)
    .setProtectedHeader({ alg: "dir", enc: "A128GCM" })
    .setIssuedAt()
    .setExpirationTime("1h")
    .encrypt(Buffer.from(encryptKey, "hex"));

  return NextResponse.json({ identity });
}

Notes:

  • Use your own session/auth lookup; only return data for authenticated users.
  • Keep the route private to your site; it should rely on your existing auth cookies and must not leak sensitive data.

Connect it in Megadesk

  1. Go to Settings > Developer > Personalized chats.
  2. Set the Identity Endpoint to your route, e.g. /api/megadesk-identity.
  3. Copy the Encryption Key and add it to your route as the encryption key. Keep it secret and store it as an env var on your server.
  4. (Optional) Rotate the Identity Secret if you want a new token for encryption.

After this, identity fields like userDetail.email can be used in Actions (headers/body placeholders) and to avoid re-asking known info in chat.