> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/reference/dnotifier-class/javascript-typescript.md). Agents can also search and read this documentation through its [MCP server](https://dnotifier.gitbook.io/product-docs/~gitbook/mcp).

# JavaScript / TypeScript

Package: `@dnotifier-realtime/dnotifier`

## Import

```js
import {
  DNotifier,
  Agent,
  defineAgent,
  Workflow,
  WorkflowContext,
  WorkflowRunner,
  WorkflowError,
} from "@dnotifier-realtime/dnotifier";
```

## Instance methods

| Method                     | Signature                                                                                | Returns                                                      | Transport        | Description                                                                |
| -------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------ | ---------------- | -------------------------------------------------------------------------- |
| `connect`                  | `connect()`                                                                              | `Promise<boolean>`                                           | Both             | Authenticate; open WebSocket + handshake if `ws`                           |
| `getPlanLimits`            | `getPlanLimits()`                                                                        | `PlanLimits \| null`                                         | Both             | Plan quotas from last auth                                                 |
| `send`                     | `send({ senderId, receiverId?, receiverIds?, data, saveHistory? })`                      | `Promise<string \| object>`                                  | Both             | Send structured message; WS returns `messageId`, HTTP returns RPC response |
| `sendBinary`               | `sendBinary({ senderId, receiverIds, buffer, type? })`                                   | `void`                                                       | WS only          | Send raw bytes (no HTTP path)                                              |
| `sendAI`                   | `sendAI({ senderId, message, saveHistory?, sessionId?, provider?, model? })`             | `Promise<string \| object \| null>`                          | Both             | AI prompt; HTTP returns response body, WS returns `sessionId`              |
| `fetchAIHistory`           | `fetchAIHistory({ senderId })`                                                           | `Promise<object \| void>`                                    | Both             | Fetch AI conversation history                                              |
| `deleteAIHistoryMessage`   | `deleteAIHistoryMessage({ senderId, messageId })`                                        | `Promise<object \| void>`                                    | Both             | Delete one AI history entry                                                |
| `fetchChatHistory`         | `fetchChatHistory({ senderId, receiverIds })`                                            | `Promise<object \| void>`                                    | Both             | Request chat history; WS delivers via `onMessage`                          |
| `deleteChatHistoryMessage` | `deleteChatHistoryMessage({ senderId, messageId })`                                      | `Promise<object \| void>`                                    | Both             | Delete one chat history entry                                              |
| `search`                   | `search({ senderId, query, limit?, minSimilarity?, filterbySource? })`                   | `Promise<object \| void>`                                    | Both             | Semantic search over knowledge base                                        |
| `addDocument`              | `addDocument({ senderId, recordId?, record_id?, content?, text?, type?, metadata? })`    | `Promise<object \| void>`                                    | Both             | Add RAG document (code 306)                                                |
| `updateDocument`           | `updateDocument({ senderId, recordId?, record_id?, content?, text?, type?, metadata? })` | `Promise<object \| void>`                                    | Both             | Update RAG document (code 307)                                             |
| `deleteDocument`           | `deleteDocument({ senderId, recordId?, record_id? })`                                    | `Promise<object \| void>`                                    | Both             | Delete RAG document (code 308)                                             |
| `listDocuments`            | `listDocuments({ senderId, limit?, offset?, type? })`                                    | `Promise<object \| void>`                                    | Both             | List RAG documents (code 309)                                              |
| `getDocument`              | `getDocument({ senderId, recordId?, record_id? })`                                       | `Promise<object \| void>`                                    | Both             | Get one RAG document (code 310)                                            |
| `runWorkflow`              | `runWorkflow({ workflow, input, senderId? })`                                            | `Promise<{ result, state, executionId? }>`                   | HTTP recommended | Execute a registered workflow                                              |
| `runAgent`                 | `runAgent(agent, { input?, maxToolRounds?, observability?, ... })`                       | `Promise<{ output, executionId, finishReason, lastStepId }>` | HTTP recommended | Standalone tool-agent loop                                                 |
| `resumeWorkflowContext`    | `resumeWorkflowContext(traceCtx, options?)`                                              | `Promise<WorkflowContext>`                                   | —                | Advanced: resume append-only context                                       |
| `sendWithOpenAI`           | `sendWithOpenAI({ senderId, message, openaiApiKey?, model?, baseUrl? })`                 | `Promise<void>`                                              | Both             | **Deprecated** — delegates to `sendAI`                                     |

Instance surfaces for tools: **`notifier.tools`**, **`notifier.mcp`**. See [Tools & MCP](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/tools-and-mcp/javascript-typescript.md).

## Static members

| Member                                               | Description                             |
| ---------------------------------------------------- | --------------------------------------- |
| `DNotifier.defineAgent({ name, run?, tools?, ... })` | Returns `Agent` (classic or tool agent) |
| `DNotifier.Agent`                                    | Agent class                             |
| `DNotifier.Workflow`                                 | Workflow class                          |
| `DNotifier.WorkflowContext`                          | Workflow context class                  |
| `DNotifier.WorkflowRunner`                           | Low-level runner                        |
| `DNotifier.WorkflowError`                            | Error class                             |

## Instance properties

| Property           | Type      | Set when          |
| ------------------ | --------- | ----------------- |
| `appId`            | `string`  | Constructor       |
| `secret`           | `string`  | Constructor       |
| `userId`           | `string`  | Constructor       |
| `url`              | `string`  | Constructor       |
| `isConnected`      | `boolean` | After `connect()` |
| `aiEnabled`        | `boolean` | After auth        |
| `authToken`        | `string`  | After auth        |
| `messageSizeLimit` | `number`  | After auth        |

## Error behavior

| Error                                  | When                                            |
| -------------------------------------- | ----------------------------------------------- |
| `"Not connected"`                      | Any method called before successful `connect()` |
| `"senderId required"`                  | Missing `senderId` on send/history/AI methods   |
| `"receiverId or receiverIds required"` | `send()` without a recipient                    |
| `"query required"`                     | `search()` without `query`                      |
| `"recordId is required"`               | Document methods without id                     |
| `"content or text is required"`        | `addDocument` / `updateDocument` without body   |

## `sendAI` return values

| Transport | Return                                                                  |
| --------- | ----------------------------------------------------------------------- |
| `http`    | Full RPC response object (includes AI reply)                            |
| `ws`      | Session/message id string; reply arrives on `onMessage` with code `300` |

## `runWorkflow` return value

```ts
{
  result: unknown;      // return value of workflow.entry()
  state: object;        // shared mutable state
  executionId?: string; // present when workflow.observability === true
}
```
