# Connect a live Node app to Cortad

Add @cortad/sdk to a Node app with one line, choose whether message text is sent, and see every production conversation as it happens.

One line where your app starts connects it to Cortad. Each user is placed in one of your personas, and their conversations are [asked the same questions as your automated audits](https://cortad.com/docs/how-it-works.md#the-same-questions-in-production).

## Get your key

Copy the setup from **Your users** into your coding agent, or run `npx -y cortad@2.1.1 intelligence` in your app's folder. It writes `CORTAD_INGEST_KEY` into your git-ignored env file and asks before it adds the key to your production host. Where it cannot, put `CORTAD_INGEST_KEY` in your production environment the way you add any secret.

## Install

The SDK needs Node 20.16 or later, and works with `require`, `import` and TypeScript.

```sh
npm i @cortad/sdk
```

Add one line where your app starts:

```js
import { cortad } from "@cortad/sdk";

cortad({ key: process.env.CORTAD_INGEST_KEY, captureContent: true }).install();
```

That is the install. Every call your process makes through `fetch` to OpenAI, Anthropic, Fireworks, Groq, OpenRouter or Google is observed, streamed replies included. A conversation is recognised by the system prompt and first user message it keeps resending, so nothing has to be wrapped.

## Choose whether message text leaves

Message text stays in your process unless you pass `captureContent: true`.

## Say which conversation a call belongs to

When your app keeps its own conversation id, wrap each conversation in `run` with that id, and say what the reply was built from. Use this in place of the install line, so the client that observes the calls is the one that wraps them:

```js
const cortadClient = cortad({ key: process.env.CORTAD_INGEST_KEY, captureContent: true });
cortadClient.install();

await cortadClient.run({ id: conversationId, user: customerId }, async () => {
  const docs = await retriever.invoke(question);
  cortadClient.context({ passages: docs.map((d) => ({ id: d.id, text: d.pageContent, score: d.score })) });
  const reply = await model.invoke(question);
  cortadClient.outcome("resolved");
});
```

## Sent as each conversation ends

When the work inside `run` ends, its conversation is sent before `run` returns, waiting at most two seconds, because a serverless host freezes the process as soon as the response is out. If a send has taken longer than two seconds, later conversations go on without waiting until one gets through.

```js
export async function POST(request) {
  const { id, messages } = await request.json();
  return cortadClient.run({ id }, async () => Response.json(await openai.chat.completions.create({ model: "gpt-4o", messages })));
}
```

## Hand over a reply the SDK cannot see

Inside `run`, a chat request to any address is captured, so Azure, Vertex, a gateway or a model on your own machine need nothing more. A reply that never goes through `fetch` or `node:http`, such as one over gRPC or a stream you finish in a callback, you hand over yourself, inside `run`. `messages` is what the model was given, as `{ role, content }`, and `answer` is what it said; the rest is optional.

```js
await cortadClient.run({ id: conversationId }, async () => {
  const text = await callModelOverGrpc(messages);
  cortadClient.reply({ messages, answer: text, model: "claude-sonnet-4", tokens: { in: 120, out: 40 }, ms: 812 });
});
```

## When only some calls answer a person

Your app may also make model calls that are not replies to a person: choosing a route, rewriting a query, naming a chat. Pass `onlyRun: true` and wrap the answering call alone. Only calls inside `run` are then observed.

## Next.js

The line goes in `instrumentation.ts`, behind `process.env.NEXT_RUNTIME === "nodejs"`. On the edge runtime the SDK switches itself off and your app keeps running.

## See it arrive

Open **Your users** in the Cortad app. They appear there as their conversations arrive.

For what people did around each answer, add [the page tag](https://cortad.com/docs/page-tag.md). For a Python app, see [the Python SDK](https://cortad.com/docs/production-python.md). An app in any other language sends [one signed request](https://cortad.com/docs/send-conversations.md) after each reply.
