# TypeScript

## Install

```bash
npm install scispace
```

Requires Node 18 or later. Ships ESM and CJS builds with bundled type declarations.

## Configure

```typescript
import Scispace from "scispace";

const client = new Scispace({
  apiKey: process.env.SCISPACE_API_KEY, // the default
  timeoutMs: 60_000,
  maxRetries: 4,
  apiVersion: "2026-08-01",
});
```

## Minimal example

```typescript
const search = await client.searches
  .create({ query: "How does climate change affect biodiversity?", depth: "standard" })
  .wait();

console.log(search.answer.text);
```

## Ergonomics

**Auto-pagination**

```typescript
for await (const paper of client.papers.list({ query: "transformer architecture" })) {
  console.log(paper.doi);
}
```

**Streaming**

```typescript
const stream = client.chats.messages.stream({
  chatId: "chat_1r8eaidwoq",
  content: "What datasets were used?",
});

for await (const event of stream) {
  if (event.type === "message.delta") process.stdout.write(event.delta);
  if (event.type === "citation.added") console.log(`\n[${event.citation.index}]`);
}
const message = await stream.finalMessage();
```

**Types**

`depth`, `status`, and `error.type` are union types, not `string`. Response types are exported:

```typescript
import type { Search, Citation, Paper } from "scispace";
```

**Errors**

```typescript
import { InvalidRequestError, RateLimitError, InsufficientCreditsError } from "scispace";

try {
  await client.searches.create({ query: "" });
} catch (e) {
  if (e instanceof InvalidRequestError) console.error(e.code, e.param, e.requestId);
}
```

## Edge and browser

The SDK runs in any Fetch-capable runtime, including edge functions.

> [!danger] Never ship a key to a browser
> There is no browser-safe key. Call the API from your server and proxy the result. See
> authentication.

## Full reference

Generated API surface: `https://docs.scispace.com/sdks/typescript/reference`.

## Related

sdks-overview · errors-and-retries · grounded-answers
