# TypeScript SDK

> Install the core TypeScript client, search and read code, and ask Corfedo AI with streaming, reattach and your own tools.

Source: https://docs.corfedo.ai/connect/sdks/typescript/

The SDK uses ES modules on Node.js 22.12 or later. `@corfedo/client` is framework-independent. `@corfedo/ai-sdk` and `@corfedo/langchain` add Corfedo to the [Vercel AI SDK](https://docs.corfedo.ai/connect/frameworks/vercel-ai-sdk/) and [LangChain](https://docs.corfedo.ai/connect/frameworks/langchain/). The packages use Apache-2.0.

```sh
npm install @corfedo/client
```

`client.search.search` and `client.code.readFile` search and read code, see [Search and read code](https://docs.corfedo.ai/connect/api/search-and-read/). `CorfedoAuthentication.signIn` signs a person in, see [Ask for Corfedo AI](https://docs.corfedo.ai/connect/authentication/#request-access-to-corfedo-ai) for an example.

## Ask Corfedo AI

```ts
import { CorfedoClient } from '@corfedo/client';

const client = CorfedoClient.fromApiKeyFile(process.env.CORFEDO_API_KEY_FILE!);
try {
  const run = client.ai.stream({
    clientMessageId: crypto.randomUUID(),
    text: 'How are search requests routed?',
  });
  for await (const event of run) {
    if (event.type === 'text.delta') process.stdout.write(event.text);
  }
  console.log(run.outcome, run.answer, run.citations);
} finally {
  await client.close();
}
```

The key needs **Search and read code** and **Ask Corfedo AI**, see [API keys](https://docs.corfedo.ai/connect/authentication/#api-keys). `send` waits without streaming. Continue with `conversationId` and a new `clientMessageId`. `listProfiles`, `listConversations`, `getConversation` and `deleteConversation` expose profiles and history.

`reattach(runId, after)` returns an observer stream. SDK sequences are `bigint`, while wire sequences are decimal strings. Use `getRun` for a snapshot and `cancel` to request run cancellation. Abort signals end a subscription without cancelling the run.

`streamToolResults` and `submitToolResults` return the results of your own tools. Run the calls in `requiredCalls` of the stream that sent the message. A reattached stream only lists `pendingCalls`. See [Client tools](https://docs.corfedo.ai/connect/api/ai/client-tools/).

## Errors and recovery

The core uses `CorfedoError` and `CorfedoFailure`, including `AccessDenied`, `Conflict`, `SpendingLimitReached` and `StreamFailed`.

If the connection drops, the SDK reconnects for up to 30 seconds, then throws the last error. The run keeps going. Human sessions need the [renewal callback](https://docs.corfedo.ai/connect/authentication/#keep-a-human-session-alive).
