> 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-rag-bot/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

Index FAQ content, then answer questions with **`useKnowledgeBase: true`**.

## Step 1 — Index documents

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

const INDEXER_ID = "kb-indexer";

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

await notifier.connect();

const faqs = [
  {
    recordId: "faq-refund",
    content: "Refunds are processed within 5 business days after approval.",
  },
  {
    recordId: "faq-password",
    content: "Reset your password under Settings → Security → Reset password.",
  },
  {
    recordId: "faq-shipping",
    content: "Standard shipping takes 3–7 business days domestically.",
  },
];

for (const faq of faqs) {
  await notifier.addDocument({
    senderId: INDEXER_ID,
    recordId: faq.recordId,
    content: faq.content,
    type: "faq",
    metadata: { source: "help-center" },
  });
}

console.log("Indexed", faqs.length, "documents");
```

## Step 2 — RAG Q\&A endpoint

```js
import express from "express";

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

const sessions = new Map();

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

app.post("/api/support", async (req, res) => {
  const { userId, question } = req.body;

  const response = await notifier.sendAI({
    senderId: userId,
    sessionId: sessions.get(userId),
    message: {
      useKnowledgeBase: true,
      messages: [
        {
          role: "system",
          content:
            "You are a support bot. Answer only from retrieved knowledge. If the answer is not in the knowledge base, say you do not know and offer to connect the user with a human.",
        },
        { role: "user", content: question },
      ],
    },
    saveHistory: true,
  });

  const sid = response?.metadata?.packet?.id ?? response?.metadata?.id;
  if (sid && !sessions.has(userId)) sessions.set(userId, sid);

  res.json({ answer: extractContent(response) });
});

app.listen(3001);
```

## Step 3 — Manual search variant

```js
app.post("/api/support/manual-rag", async (req, res) => {
  const { userId, question } = req.body;

  const hits = await notifier.search({
    senderId: userId,
    query: question,
    limit: 5,
    minSimilarity: 0.5,
    filterbySource: "help-center",
  });

  const response = await notifier.sendAI({
    senderId: userId,
    message: {
      messages: [
        {
          role: "system",
          content: `Context:\n${JSON.stringify(hits?.data ?? hits, null, 2)}`,
        },
        { role: "user", content: question },
      ],
    },
  });

  res.json({
    answer: extractContent(response),
    sources: hits,
  });
});
```

## Test

```bash
curl -X POST http://localhost:3001/api/support \
  -H "Content-Type: application/json" \
  -d '{"userId":"user-1","question":"How long do refunds take?"}'
```

## Learn more

* [**Semantic search**](https://dnotifier.gitbook.io/product-docs/ai/semantic-search/javascript-typescript.md)
* [**Workflow intent router**](https://dnotifier.gitbook.io/product-docs/workflows-and-agents/example-intent-router/javascript-typescript.md)
