> 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/semantic-search/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

Query the knowledge base with **vector similarity** over HTTP.

## Basic search

```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();

const hits = await notifier.search({
  senderId: USER_ID,
  query: "how long do refunds take",
  limit: 5,
  minSimilarity: 0.7,
  filterbySource: "help-center",
});

console.log(hits);
```

## Search then answer manually

```js
const hits = await notifier.search({
  senderId: USER_ID,
  query: userQuestion,
  limit: 3,
  minSimilarity: 0.5,
});

const context = JSON.stringify(hits?.data ?? hits);

const response = await notifier.sendAI({
  senderId: USER_ID,
  message: {
    messages: [
      {
        role: "system",
        content: `Answer using only this context:\n${context}`,
      },
      { role: "user", content: userQuestion },
    ],
  },
  saveHistory: true,
});
```

## Workflow context

Inside agents, use `ctx.search`:

```js
const hits = await ctx.search({
  query: "refund policy",
  limit: 10,
  minSimilarity: 0.3,
  filterbySource: "help-center",
  label: "FAQ search",
});
```

## Troubleshooting

| Issue            | Fix                                                  |
| ---------------- | ---------------------------------------------------- |
| Empty results    | Index docs with `addDocument`; lower `minSimilarity` |
| Wrong namespace  | Check `filterbySource` matches `metadata.source`     |
| `query required` | Pass non-empty `query` string                        |

## Learn more

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