Skip to content

Getting Thread IDs

Thread IDs are generated by Docuworm when a conversation thread is established. Use helper functions or element methods to read them.

Before waiting for a thread ID, trigger thread creation by opening the widget or queueing uploads/sources.

Use waitForThreadId() or getThreadId().

import {
openWidget,
waitForThreadId,
getThreadId,
getDocuwormThreadId,
} from "https://cdn.docuworm.ai/docuworm.js";
const widget = document.querySelector("docuworm-chat");
openWidget(widget);
const a = await waitForThreadId({ element: widget, timeoutMs: 30000 });
const b = await getThreadId({ element: widget });
const c = await getDocuwormThreadId({ element: widget });

Notes:

  • getThreadId() and getDocuwormThreadId() are equivalent aliases.
  • If the widget is not ready before timeout, an error is thrown.

Use this when you need a non-blocking check:

import {
getThreadIdSync,
getDocuwormThreadIdSync,
} from "https://cdn.docuworm.ai/docuworm.js";
const widget = document.querySelector("docuworm-chat");
const id1 = getThreadIdSync({ element: widget });
const id2 = getDocuwormThreadIdSync({ element: widget });
if (!id1) {
console.log("Thread not ready yet");
}

The web component also exposes methods directly.

const widget = document.querySelector("docuworm-chat");
const immediate = widget?.getThreadIdSync?.() ?? null;
const awaited = await widget?.waitForThreadId?.(30000);
const widget = document.querySelector("docuworm-chat");
openWidget(widget);
const threadId = await waitForThreadId({ element: widget, timeoutMs: 30000 });
await fetch("/api/docuworm/thread", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ threadId }),
});

Use the same helper API and pass the framework-held element reference.

  • React: useRef and useEffect
  • Vue: ref and onMounted
  • Svelte: bind:this and onMount