# TypeScript SDK

> Install the antarya package from npm and send requests from TypeScript.

## Install

```typescript
npm install antarya
```

## Client

The client reads `ANTARYA_API_KEY`. Pass `{ apiKey: "ak-..." }` to use a different key.

```typescript
import Antarya from "antarya";

const client = new Antarya();

const reply = await client.chat.create({
  messages: [{ role: "user", content: "Summarise this contract's termination clause." }],
});
console.log(reply.content[0].text);
```

## Streaming

Set `stream: true` and loop over the events as they arrive.

```typescript
const stream = await client.chat.create({
  messages: [{ role: "user", content: "Draft a reply that declines the meeting." }],
  stream: true,
});

for await (const event of stream) {
  if (event.type === "content.delta" && typeof event.text === "string") process.stdout.write(event.text);
}
```

## Files and images

A message can carry files and images next to its text. Upload a file once and refer to it by its id in any message, or send a file or an image inside the request, where it is read for that request only and not stored. Files can be PDF, Word, Excel, text, Markdown, CSV or JSON, up to 10 MB each. Images can be PNG, JPEG or WebP, up to 10 MB each and 20 a request. The [files and images guide](https://antarya.ai/docs/guides/files-and-images.md) has more.

```typescript
import fs from "node:fs";

// Upload once, then refer to the file by id.
const file = await client.files.create({ file: fs.createReadStream("agreement.pdf") });

await client.chat.create({
  messages: [{
    role: "user",
    content: [
      { type: "file", file_id: file.id },
      { type: "text", text: "Summarise the termination clause in this agreement." },
    ],
  }],
});

// Or send the file inside the request, without storing it.
const data = fs.readFileSync("agreement.pdf").toString("base64");

await client.chat.create({
  messages: [{
    role: "user",
    content: [
      { type: "file", source: { type: "base64", filename: "agreement.pdf", media_type: "application/pdf", data } },
      { type: "text", text: "Summarise the termination clause in this agreement." },
    ],
  }],
});

// An image goes the same way.
const image = fs.readFileSync("signature-page.png").toString("base64");

await client.chat.create({
  messages: [{
    role: "user",
    content: [
      { type: "image", source: { type: "base64", media_type: "image/png", data: image } },
      { type: "text", text: "Who signed this page?" },
    ],
  }],
});
```

## Sessions

A session keeps the conversation on Antarya's side, so you send only the new turn. See the [sessions guide](https://antarya.ai/docs/guides/sessions.md) for how it works and the [sessions reference](https://antarya.ai/docs/api/sessions.md) for its limits. Versions 0.1.2 and later of the package have these methods.

```typescript
const session = await client.sessions.create({ metadata: { matter: "acme-msa" } });

const reply = await client.chat.create({
  session_id: session.id,
  messages: [{ role: "user", content: "Summarise the termination clause." }],
});

let page = await client.sessions.list({ limit: 20 });
if (page.has_more) page = await client.sessions.list({ limit: 20, after: page.data.at(-1)!.id });

const saved = await client.sessions.get(session.id);
console.log(saved.messages);

await client.sessions.delete(session.id);
```

`create` returns `{ id, object: "session", created_at, updated_at, metadata }`, with ids like `sess_4f2a9c1e7b3d48a6915e0c2d8f7a6b31`. `get` returns the same fields plus `messages`. `list` returns `{ object: "list", data, has_more }`, and `client.files.list` pages the same way. `delete` returns `{ id, deleted: true }`.

## Errors and retries

A failed request throws `Antarya.APIError`. It carries `status`, `type`, `message` and `requestId`. The client retries 429 and 5xx replies twice, with a short wait between tries. Change that with `new Antarya({ maxRetries: 4 })`.

```typescript
try {
  await client.chat.create({ messages: [{ role: "user", content: "Hello" }] });
} catch (e) {
  if (e instanceof Antarya.APIError) console.log(e.status, e.type, e.requestId);
  else throw e;
}
```

## Async

Every method returns a promise, so there is no separate async client. Run requests side by side with `Promise.all`.

```typescript
const questions = ["Summarise the termination clause.", "Draft a reply that declines the meeting."];
const replies = await Promise.all(
  questions.map((q) => client.chat.create({ messages: [{ role: "user", content: q }] })),
);
```

## Next steps

- [Python SDK](https://antarya.ai/docs/sdk/python.md): The same set for Python.
- [CLI](https://antarya.ai/docs/cli.md): Ask from your terminal.
- [Quickstart](https://antarya.ai/docs/quickstart.md): Make a key and send your first request.
