> 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/ai/sessions/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

Continue a multi-turn AI conversation by passing **`sessionId`** from the first response into follow-up `sendAI` calls.

## Complete multi-turn example

```js
import { DNotifier } from "@dnotifier-realtime/dnotifier";

const USER_ID = "user-123";

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "http",
  userId: USER_ID,
  onConnected: () => {},
  onMessage: () => {},
  onDisconnected: () => {},
});

await notifier.connect();

// Turn 1 — new session
const first = await notifier.sendAI({
  senderId: USER_ID,
  message: { text: "I need help with billing for order #4821." },
});

const sessionId =
  first?.metadata?.packet?.id ?? first?.metadata?.id;

console.log("Session:", sessionId);
console.log("Reply:", first?.data?.content ?? first);

// Turn 2 — same session
const second = await notifier.sendAI({
  senderId: USER_ID,
  sessionId,
  message: { text: "What documents do I need to submit?" },
});

console.log("Follow-up:", second?.data?.content ?? second);
```

## Persist session per ticket

```js
// After first turn, save to your DB:
await db.tickets.update(ticketId, { dnotifierSessionId: sessionId });

// On later HTTP requests:
const ticket = await db.tickets.get(ticketId);
const response = await notifier.sendAI({
  senderId: USER_ID,
  sessionId: ticket.dnotifierSessionId,
  message: { text: userMessage },
});
```

## Chat-style messages in a session

```js
const first = await notifier.sendAI({
  senderId: USER_ID,
  message: {
    messages: [
      { role: "system", content: "You are a billing specialist." },
      { role: "user", content: "I was charged twice." },
    ],
  },
});

const sessionId = first?.metadata?.packet?.id ?? first?.metadata?.id;

await notifier.sendAI({
  senderId: USER_ID,
  sessionId,
  message: {
    messages: [{ role: "user", content: "Here is my receipt number: R-9912." }],
  },
});
```

## TypeScript helper

```ts
function sessionIdFromResponse(response: unknown): string | undefined {
  const r = response as {
    metadata?: { packet?: { id?: string }; id?: string };
  };
  return r?.metadata?.packet?.id ?? r?.metadata?.id;
}
```

## Troubleshooting

| Issue                   | Check                                             |
| ----------------------- | ------------------------------------------------- |
| Follow-up lacks context | Verify `sessionId` is passed and not regenerated  |
| New session each time   | Ensure you store and reuse id from first response |
| Wrong user context      | `senderId` must match across turns                |

## Learn more

* [**AI history**](https://dnotifier.gitbook.io/product-docs/ai/ai-history/javascript-typescript.md) — Inspect stored turns
* [**Session logging**](https://dnotifier.gitbook.io/product-docs/ai/session-logging/javascript-typescript.md) — Dashboard logs
