> 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/example-ai-assistant/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

Self-contained **Express API** assistant with per-user **session** storage.

## Complete example

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

const app = express();
app.use(express.json());

// In-memory session store — use Redis/DB in production
const sessions = new Map();

const SERVICE_USER = "api-assistant";

const notifier = new DNotifier({
  appId: process.env.DNOTIFIER_APP_ID,
  secret: process.env.DNOTIFIER_SECRET,
  transport: "http",
  userId: SERVICE_USER,
  logs: true,
  onConnected: () => console.log("AI service ready"),
  onMessage: () => {},
  onDisconnected: () => {},
});

await notifier.connect();

function extractContent(response) {
  return (
    response?.data?.content ??
    response?.content ??
    JSON.stringify(response)
  );
}

function sessionIdFromResponse(response) {
  return response?.metadata?.packet?.id ?? response?.metadata?.id;
}

app.post("/api/chat", async (req, res) => {
  const { userId, message } = req.body;
  if (!userId || !message) {
    return res.status(400).json({ error: "userId and message required" });
  }

  const existingSession = sessions.get(userId);

  const response = await notifier.sendAI({
    senderId: userId,
    sessionId: existingSession,
    message: {
      messages: [
        {
          role: "system",
          content:
            "You are a friendly product assistant. Be concise and accurate.",
        },
        { role: "user", content: String(message) },
      ],
    },
    saveHistory: true,
  });

  if (!existingSession) {
    const newSession = sessionIdFromResponse(response);
    if (newSession) sessions.set(userId, newSession);
  }

  res.json({
    answer: extractContent(response),
    sessionId: sessions.get(userId),
  });
});

app.delete("/api/chat/:userId/session", (req, res) => {
  sessions.delete(req.params.userId);
  res.status(204).end();
});

app.listen(3000, () => console.log("Assistant API on :3000"));
```

## Test with curl

```bash
curl -X POST http://localhost:3000/api/chat \
  -H "Content-Type: application/json" \
  -d '{"userId":"user-alice","message":"What plans do you offer?"}'

curl -X POST http://localhost:3000/api/chat \
  -H "Content-Type: application/json" \
  -d '{"userId":"user-alice","message":"Tell me more about the first one."}'
```

The second request reuses the stored `sessionId` for continuity.

## Learn more

* [**Sessions**](https://dnotifier.gitbook.io/product-docs/ai/sessions/javascript-typescript.md)
* [**Example: RAG bot**](https://dnotifier.gitbook.io/product-docs/ai/example-rag-bot/javascript-typescript.md)
