> 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/send-prompt/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

Send a **single AI prompt** over HTTP and read the response from the returned promise.

## Complete 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: () => console.log("Authenticated"),
  onMessage: () => {},
  onDisconnected: () => {},
});

await notifier.connect();

if (!notifier.aiEnabled) {
  throw new Error("AI is not enabled for this plan");
}

const response = await notifier.sendAI({
  senderId: USER_ID,
  message: {
    text: "Summarize our refund policy in two sentences.",
  },
});

const answer =
  response?.data?.content ??
  response?.content ??
  JSON.stringify(response);

console.log("AI:", answer);
```

## Browser / Vite

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

const notifier = new DNotifier({
  appId: import.meta.env.VITE_DNOTIFIER_APP_ID,
  secret: import.meta.env.VITE_DNOTIFIER_SECRET,
  transport: "http",
  userId: "user-123",
  onConnected: () => {},
  onMessage: () => {},
  onDisconnected: () => {},
});

await notifier.connect();

const response = await notifier.sendAI({
  senderId: "user-123",
  message: { text: "What are three benefits of HTTP transport for AI?" },
});
```

## Express API route

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

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

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

await notifier.connect();

app.post("/api/ask", async (req, res) => {
  const { userId, question } = req.body;
  const response = await notifier.sendAI({
    senderId: userId,
    message: { text: question },
    saveHistory: true,
  });
  res.json({ answer: response?.data?.content ?? response });
});

app.listen(3000);
```

## Without saving history

```js
await notifier.sendAI({
  senderId: USER_ID,
  message: { text: "Classify this ticket as billing or technical." },
  saveHistory: false,
});
```

## Choose provider and model

Set the connection up in the portal, then name it in the call:

```js
await notifier.sendAI({
  senderId: USER_ID,
  provider: "open_ai",
  model: "gpt-4o",
  message: {
    useKnowledgeBase: false,
    messages: [{ role: "user", content: "Reply with exactly one word: ok" }],
  },
  saveHistory: true,
});
```

Other providers use the same shape with a different `provider` string — see [multi-models](https://dnotifier.gitbook.io/product-docs/ai/multi-models.md).

## TypeScript

```ts
type AiTextMessage = { text: string };

const response = await notifier.sendAI({
  senderId: USER_ID,
  message: { text: "Hello" } satisfies AiTextMessage,
});
```

## Troubleshooting

| Issue              | Fix                                       |
| ------------------ | ----------------------------------------- |
| `Not connected`    | `await notifier.connect()`                |
| Response is `null` | Check credentials; confirm HTTP transport |
| Empty `content`    | Inspect full `response` object shape      |

## Learn more

* [Providers & models](https://dnotifier.gitbook.io/product-docs/ai/multi-models.md)
* [Sessions](https://dnotifier.gitbook.io/product-docs/ai/sessions/javascript-typescript.md)
* [Chat-style messages](https://dnotifier.gitbook.io/product-docs/ai/chat-style-messages/javascript-typescript.md)
